add remote logger implementation.

This commit is contained in:
Miroslav Šedivý 2021-09-09 23:35:26 +02:00
parent f28bc1184a
commit ca6bc69630
3 changed files with 87 additions and 0 deletions

View File

@ -0,0 +1,78 @@
import { NekoWebSocket } from './websocket'
import * as EVENT from '../types/events'
import * as message from '../types/messages'
import { Logger } from '../utils/logger'
const RETRY_INTERVAL = 2500
export class NekoLogger extends Logger {
private _ws: NekoWebSocket
private _logs: message.SystemLog[] = []
private _interval: number | null = null
constructor(websocket: NekoWebSocket, scope?: string) {
super(scope)
this._ws = websocket
}
protected _send(level: string, message: string, fields?: Record<string, any>) {
if (!fields) {
fields = { scope: this._scope }
} else {
fields['scope'] = this._scope
}
const payload = { level, message, fields } as message.SystemLog
if (!this._ws.connected) {
this._logs.push(payload)
// postpone logs sending
if (this._interval == null) {
this._interval = window.setInterval(() => {
if (!this._ws.connected || !this._interval) {
return
}
if (this._logs.length > 0) {
this._ws.send(EVENT.SYSTEM_LOGS, this._logs)
}
window.clearInterval(this._interval)
this._interval = null
}, RETRY_INTERVAL)
}
return
}
// abort postponed logs sending
if (this._interval != null) {
window.clearInterval(this._interval)
this._interval = null
}
this._ws.send(EVENT.SYSTEM_LOGS, [...this._logs, payload])
this._logs = []
}
public error(message: string, fields?: Record<string, any>) {
this._console('error', message, fields)
this._send('error', message, fields)
}
public warn(message: string, fields?: Record<string, any>) {
this._console('warn', message, fields)
this._send('warn', message, fields)
}
public info(message: string, fields?: Record<string, any>) {
this._console('info', message, fields)
this._send('info', message, fields)
}
public debug(message: string, fields?: Record<string, any>) {
this._console('debug', message, fields)
this._send('debug', message, fields)
}
}

View File

@ -1,5 +1,6 @@
export const SYSTEM_INIT = 'system/init'
export const SYSTEM_ADMIN = 'system/admin'
export const SYSTEM_LOGS = 'system/logs'
export const SYSTEM_DISCONNECT = 'system/disconnect'
export const SIGNAL_REQUEST = 'signal/request'

View File

@ -23,6 +23,14 @@ export interface SystemAdmin {
broadcast_status: BroadcastStatus
}
export type SystemLogs = SystemLog[]
export interface SystemLog {
level: "debug" | "info" | "warn" | "error"
fields: Record<string, string>
message: string
}
export interface SystemDisconnect {
message: string
}