forked from noxious/server
Continuation of refactor
This commit is contained in:
@ -1,73 +1,51 @@
|
||||
import pino from 'pino'
|
||||
|
||||
import { getRootPath } from './storage'
|
||||
export enum LoggerType {
|
||||
HTTP = 'http',
|
||||
GAME = 'game',
|
||||
GAME_MASTER = 'gameMaster',
|
||||
APP = 'app',
|
||||
QUEUE = 'queue',
|
||||
COMMAND = 'command',
|
||||
REPOSITORY = 'repository',
|
||||
ENTITY = 'entity'
|
||||
}
|
||||
|
||||
export class Logger {
|
||||
private static readonly LOG_TYPES = ['http', 'game', 'gameMaster', 'app', 'queue', 'command', 'repository', 'entity'] as const
|
||||
private readonly logger: ReturnType<typeof pino>
|
||||
class Logger {
|
||||
private instances: Map<LoggerType, ReturnType<typeof pino>> = new Map()
|
||||
|
||||
constructor(type: (typeof Logger.LOG_TYPES)[number]) {
|
||||
this.logger = pino({
|
||||
level: process.env.LOG_LEVEL || 'debug',
|
||||
transport: {
|
||||
target: 'pino/file',
|
||||
options: {
|
||||
destination: `./logs/${type}.log`,
|
||||
mkdir: true
|
||||
}
|
||||
},
|
||||
formatters: {
|
||||
level: (label) => ({ level: label.toUpperCase() })
|
||||
},
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
base: null
|
||||
})
|
||||
}
|
||||
|
||||
info(message: string) {
|
||||
this.logger.info(message)
|
||||
}
|
||||
|
||||
error(message: string) {
|
||||
this.logger.error(message)
|
||||
}
|
||||
|
||||
warn(message: string) {
|
||||
this.logger.warn(message)
|
||||
}
|
||||
|
||||
debug(message: string) {
|
||||
this.logger.debug(message)
|
||||
}
|
||||
|
||||
static watch() {
|
||||
const fs = require('fs')
|
||||
|
||||
this.LOG_TYPES.forEach((type) => {
|
||||
const logFile = getRootPath('logs', `${type}.log`)
|
||||
const stats = fs.statSync(logFile)
|
||||
let lastPosition = stats.size
|
||||
|
||||
fs.watch(logFile, (eventType: string) => {
|
||||
if (eventType !== 'change') return
|
||||
|
||||
fs.stat(logFile, (err: Error, stats: { size: number }) => {
|
||||
if (err) return
|
||||
|
||||
if (stats.size > lastPosition) {
|
||||
const stream = fs.createReadStream(logFile, {
|
||||
start: lastPosition,
|
||||
end: stats.size
|
||||
})
|
||||
|
||||
stream.on('data', (chunk: Buffer) => {
|
||||
console.log(`[${type}]\n${chunk.toString()}`)
|
||||
})
|
||||
|
||||
lastPosition = stats.size
|
||||
}
|
||||
private getLogger(type: LoggerType): ReturnType<typeof pino> {
|
||||
if (!this.instances.has(type)) {
|
||||
this.instances.set(
|
||||
type,
|
||||
pino({
|
||||
level: process.env.LOG_LEVEL || 'debug',
|
||||
transport: {
|
||||
target: 'pino/file',
|
||||
options: {
|
||||
destination: `./logs/${type}.log`,
|
||||
mkdir: true
|
||||
}
|
||||
},
|
||||
formatters: {
|
||||
level: (label) => ({ level: label.toUpperCase() })
|
||||
},
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
base: null
|
||||
})
|
||||
})
|
||||
})
|
||||
)
|
||||
}
|
||||
return this.instances.get(type)!
|
||||
}
|
||||
|
||||
type(type: LoggerType) {
|
||||
return {
|
||||
info: (message: string, ...args: any[]) => this.getLogger(type).info(message, ...args),
|
||||
error: (message: string, ...args: any[]) => this.getLogger(type).error(message, ...args),
|
||||
warn: (message: string, ...args: any[]) => this.getLogger(type).warn(message, ...args),
|
||||
debug: (message: string, ...args: any[]) => this.getLogger(type).debug(message, ...args)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
export default new Logger()
|
||||
|
Reference in New Issue
Block a user