mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
send postponed logs.
This commit is contained in:
parent
6140c2a578
commit
b2f92a86bb
@ -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)
|
|
||||||
|
|
||||||
// postpone logs sending
|
// rotate if exceeded maximum
|
||||||
if (this._interval == null) {
|
if (this._logs.length > MAX_LOG_MESSAGES) {
|
||||||
this._interval = window.setInterval(() => {
|
this._logs.shift()
|
||||||
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
|
// postpone logs sending
|
||||||
if (this._interval != null) {
|
if (!this._timeout && !this._interval) {
|
||||||
window.clearInterval(this._interval)
|
this._timeout = window.setTimeout(() => {
|
||||||
this._interval = null
|
if (!this._timeout) {
|
||||||
}
|
return
|
||||||
|
}
|
||||||
|
|
||||||
this._ws.send(EVENT.SYSTEM_LOGS, [...this._logs, payload])
|
if (this._ws.connected) {
|
||||||
this._logs = []
|
this._flush()
|
||||||
|
} else {
|
||||||
|
this._interval = window.setInterval(() => {
|
||||||
|
if (!this._ws.connected || !this._interval) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
this._flush()
|
||||||
|
window.clearInterval(this._interval)
|
||||||
|
this._interval = null
|
||||||
|
}, RETRY_INTERVAL_MS)
|
||||||
|
}
|
||||||
|
|
||||||
|
window.clearTimeout(this._timeout)
|
||||||
|
this._timeout = null
|
||||||
|
}, FLUSH_TIMEOUT_MS)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
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
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user