mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
add remote logger implementation.
This commit is contained in:
parent
f28bc1184a
commit
ca6bc69630
78
src/component/internal/logger.ts
Normal file
78
src/component/internal/logger.ts
Normal 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)
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,6 @@
|
|||||||
export const SYSTEM_INIT = 'system/init'
|
export const SYSTEM_INIT = 'system/init'
|
||||||
export const SYSTEM_ADMIN = 'system/admin'
|
export const SYSTEM_ADMIN = 'system/admin'
|
||||||
|
export const SYSTEM_LOGS = 'system/logs'
|
||||||
export const SYSTEM_DISCONNECT = 'system/disconnect'
|
export const SYSTEM_DISCONNECT = 'system/disconnect'
|
||||||
|
|
||||||
export const SIGNAL_REQUEST = 'signal/request'
|
export const SIGNAL_REQUEST = 'signal/request'
|
||||||
|
@ -23,6 +23,14 @@ export interface SystemAdmin {
|
|||||||
broadcast_status: BroadcastStatus
|
broadcast_status: BroadcastStatus
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export type SystemLogs = SystemLog[]
|
||||||
|
|
||||||
|
export interface SystemLog {
|
||||||
|
level: "debug" | "info" | "warn" | "error"
|
||||||
|
fields: Record<string, string>
|
||||||
|
message: string
|
||||||
|
}
|
||||||
|
|
||||||
export interface SystemDisconnect {
|
export interface SystemDisconnect {
|
||||||
message: string
|
message: string
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user