send postponed logs.

This commit is contained in:
Miroslav Šedivý 2021-09-12 16:37:45 +02:00
parent 6140c2a578
commit b2f92a86bb

View File

@ -3,11 +3,14 @@ import * as EVENT from '../types/events'
import * as message from '../types/messages' import * as message from '../types/messages'
import { Logger } from '../utils/logger' import { Logger } from '../utils/logger'
const RETRY_INTERVAL = 2500 const MAX_LOG_MESSAGES = 25
const FLUSH_TIMEOUT_MS = 250
const RETRY_INTERVAL_MS = 2500
export class NekoLogger extends Logger { export class NekoLogger extends Logger {
private _ws: NekoWebSocket private _ws: NekoWebSocket
private _logs: message.SystemLog[] = [] private _logs: message.SystemLog[] = []
private _timeout: number | null = null
private _interval: number | null = null private _interval: number | null = null
constructor(websocket: NekoWebSocket, scope?: string) { constructor(websocket: NekoWebSocket, scope?: string) {
@ -16,6 +19,13 @@ export class NekoLogger extends Logger {
this._ws = websocket this._ws = websocket
} }
private _flush() {
if (this._logs.length > 0) {
this._ws.send(EVENT.SYSTEM_LOGS, this._logs)
this._logs = []
}
}
protected _send(level: string, message: string, fields?: Record<string, any>) { protected _send(level: string, message: string, fields?: Record<string, any>) {
if (!fields) { if (!fields) {
fields = { scope: this._scope } fields = { scope: this._scope }
@ -24,36 +34,38 @@ export class NekoLogger extends Logger {
} }
const payload = { level, message, fields } as message.SystemLog const payload = { level, message, fields } as message.SystemLog
if (!this._ws.connected) {
this._logs.push(payload) this._logs.push(payload)
// rotate if exceeded maximum
if (this._logs.length > MAX_LOG_MESSAGES) {
this._logs.shift()
}
// postpone logs sending // postpone logs sending
if (this._interval == null) { if (!this._timeout && !this._interval) {
this._timeout = window.setTimeout(() => {
if (!this._timeout) {
return
}
if (this._ws.connected) {
this._flush()
} else {
this._interval = window.setInterval(() => { this._interval = window.setInterval(() => {
if (!this._ws.connected || !this._interval) { if (!this._ws.connected || !this._interval) {
return return
} }
if (this._logs.length > 0) { this._flush()
this._ws.send(EVENT.SYSTEM_LOGS, this._logs)
}
window.clearInterval(this._interval) window.clearInterval(this._interval)
this._interval = null this._interval = null
}, RETRY_INTERVAL) }, RETRY_INTERVAL_MS)
} }
return window.clearTimeout(this._timeout)
this._timeout = null
}, FLUSH_TIMEOUT_MS)
} }
// 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>) { public error(message: string, fields?: Record<string, any>) {
@ -75,4 +87,20 @@ export class NekoLogger extends Logger {
this._console('debug', message, fields) this._console('debug', message, fields)
this._send('debug', message, fields) this._send('debug', message, fields)
} }
public destroy() {
if (this._ws.connected) {
this._flush()
}
if (this._interval) {
window.clearInterval(this._interval)
this._interval = null
}
if (this._timeout) {
window.clearTimeout(this._timeout)
this._timeout = null
}
}
} }