paperless-ngx/src-ui/src/app/components/admin/logs/logs.component.ts

106 lines
2.3 KiB
TypeScript
Raw Normal View History

import {
2024-12-13 00:27:30 -08:00
ChangeDetectorRef,
Component,
ElementRef,
2024-12-13 00:27:30 -08:00
OnDestroy,
OnInit,
ViewChild,
} from '@angular/core'
import { filter, takeUntil, timer } from 'rxjs'
import { LogService } from 'src/app/services/rest/log.service'
2024-12-06 00:26:38 -08:00
import { LoadingComponentWithPermissions } from '../../loading-component/loading.component'
2020-10-27 01:10:18 +01:00
@Component({
2023-09-14 14:03:28 -07:00
selector: 'pngx-logs',
2020-10-27 01:10:18 +01:00
templateUrl: './logs.component.html',
styleUrls: ['./logs.component.scss'],
2020-10-27 01:10:18 +01:00
})
2024-12-06 00:26:38 -08:00
export class LogsComponent
extends LoadingComponentWithPermissions
implements OnInit, OnDestroy
{
constructor(
private logService: LogService,
private changedetectorRef: ChangeDetectorRef
2024-12-06 00:26:38 -08:00
) {
super()
}
public logs: string[] = []
2020-11-02 15:56:14 +01:00
public logFiles: string[] = []
public activeLog: string
public autoRefreshEnabled: boolean = true
2021-03-15 09:56:17 -07:00
@ViewChild('logContainer') logContainer: ElementRef
2021-02-06 17:07:25 +01:00
ngOnInit(): void {
this.logService
.list()
.pipe(takeUntil(this.unsubscribeNotifier))
.subscribe((result) => {
this.logFiles = result
2024-12-06 00:26:38 -08:00
this.loading = false
if (this.logFiles.length > 0) {
this.activeLog = this.logFiles[0]
this.reloadLogs()
}
timer(5000, 5000)
.pipe(
filter(() => this.autoRefreshEnabled),
takeUntil(this.unsubscribeNotifier)
)
.subscribe(() => {
this.reloadLogs()
})
})
2020-10-27 01:10:18 +01:00
}
ngOnDestroy(): void {
2024-12-06 00:26:38 -08:00
super.ngOnDestroy()
}
2021-02-06 17:07:25 +01:00
reloadLogs() {
2024-12-06 00:26:38 -08:00
this.loading = true
this.logService
.get(this.activeLog)
.pipe(takeUntil(this.unsubscribeNotifier))
.subscribe({
next: (result) => {
this.logs = result
2024-12-06 00:26:38 -08:00
this.loading = false
this.scrollToBottom()
},
error: () => {
this.logs = []
2024-12-06 00:26:38 -08:00
this.loading = false
},
})
2020-11-02 15:56:14 +01:00
}
2021-02-06 17:07:25 +01:00
getLogLevel(log: string) {
if (log.indexOf('[DEBUG]') != -1) {
2021-02-06 17:07:25 +01:00
return 10
} else if (log.indexOf('[WARNING]') != -1) {
2021-02-06 17:07:25 +01:00
return 30
} else if (log.indexOf('[ERROR]') != -1) {
2021-02-06 17:07:25 +01:00
return 40
} else if (log.indexOf('[CRITICAL]') != -1) {
2021-02-06 17:07:25 +01:00
return 50
} else {
return 20
}
2020-11-02 15:56:14 +01:00
}
2021-03-15 09:56:17 -07:00
scrollToBottom(): void {
this.changedetectorRef.detectChanges()
2021-03-15 09:56:17 -07:00
this.logContainer?.nativeElement.scroll({
top: this.logContainer.nativeElement.scrollHeight,
left: 0,
behavior: 'auto',
})
2021-03-15 09:56:17 -07:00
}
2020-10-27 01:10:18 +01:00
}