logger factory.

This commit is contained in:
Miroslav Šedivý 2021-09-12 17:38:23 +02:00
parent 94a62e4846
commit b7ebbcbdf5
2 changed files with 54 additions and 30 deletions

View File

@ -3,13 +3,14 @@ import EventEmitter from 'eventemitter3'
import * as EVENT from '../types/events'
import { NekoWebSocket } from './websocket'
import { NekoLogger } from './logger'
import { NekoLoggerFactory } from './logger'
import { NekoWebRTC } from './webrtc'
import { Connection, WebRTCStats } from '../types/state'
import { Reconnector } from './reconnector'
import { WebsocketReconnector } from './reconnector/websocket'
import { WebrtcReconnector } from './reconnector/webrtc'
import { Logger } from '../utils/logger'
const WEBRTC_RECONN_MAX_LOSS = 25
const WEBRTC_RECONN_FAILED_ATTEMPTS = 5
@ -25,7 +26,8 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
private _open = false
public websocket = new NekoWebSocket()
public webrtc = new NekoWebRTC(new NekoLogger(this.websocket, 'webrtc'))
public logger = new NekoLoggerFactory(this.websocket)
public webrtc = new NekoWebRTC(this.logger.new('webrtc'))
private _reconnector: {
websocket: Reconnector
@ -155,6 +157,10 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
this.websocket.send(EVENT.SIGNAL_VIDEO, { video })
}
public getLogger(scope?: string): Logger {
return this.logger.new(scope)
}
public open(video?: string) {
if (this._open) {
throw new Error('connection already open')
@ -194,6 +200,8 @@ export class NekoConnection extends EventEmitter<NekoConnectionEvents> {
}
public destroy() {
this.logger.destroy()
// TODO: Use server side congestion control.
this.webrtc.off('stats', this._webrtcCongestionControlHandle)

View File

@ -7,15 +7,13 @@ const MAX_LOG_MESSAGES = 25
const FLUSH_TIMEOUT_MS = 250
const RETRY_INTERVAL_MS = 2500
export class NekoLogger extends Logger {
export class NekoLoggerFactory {
private _ws: NekoWebSocket
private _logs: message.SystemLog[] = []
private _timeout: number | null = null
private _interval: number | null = null
constructor(websocket: NekoWebSocket, scope?: string) {
super(scope)
constructor(websocket: NekoWebSocket) {
this._ws = websocket
}
@ -26,13 +24,7 @@ export class NekoLogger extends Logger {
}
}
protected _send(level: string, message: string, fields?: Record<string, any>) {
if (!fields) {
fields = { submodule: this._scope }
} else {
fields['submodule'] = this._scope
}
private _send(level: string, message: string, fields?: Record<string, any>) {
for (const key in fields) {
const field = fields[key]
@ -76,24 +68,16 @@ export class NekoLogger extends Logger {
}
}
public error(message: string, fields?: Record<string, any>) {
this._console('error', message, fields)
this._send('error', message, fields)
public new(submodule?: string): Logger {
return new NekoLogger((level: string, message: string, fields?: Record<string, any>) => {
if (!fields) {
fields = { submodule }
} else {
fields['submodule'] = submodule
}
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)
this._send(level, message, fields)
}, submodule)
}
public destroy() {
@ -112,3 +96,35 @@ export class NekoLogger extends Logger {
}
}
}
type NekoLoggerMessage = (level: string, message: string, fields?: Record<string, any>) => void
export class NekoLogger extends Logger {
private _on_send: NekoLoggerMessage
constructor(onSend: NekoLoggerMessage, scope?: string) {
super(scope)
this._on_send = onSend
}
public error(message: string, fields?: Record<string, any>) {
this._console('error', message, fields)
this._on_send('error', message, fields)
}
public warn(message: string, fields?: Record<string, any>) {
this._console('warn', message, fields)
this._on_send('warn', message, fields)
}
public info(message: string, fields?: Record<string, any>) {
this._console('info', message, fields)
this._on_send('info', message, fields)
}
public debug(message: string, fields?: Record<string, any>) {
this._console('debug', message, fields)
this._on_send('debug', message, fields)
}
}