Continuation of refactor
This commit is contained in:
@ -1,5 +1,8 @@
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
|
||||
export abstract class BaseCommand {
|
||||
protected readonly logger = Logger.type(LoggerType.COMMAND)
|
||||
constructor(readonly io: Server) {}
|
||||
}
|
||||
|
@ -1,9 +1,9 @@
|
||||
import { Request, Response } from 'express'
|
||||
|
||||
import { Logger } from '#application/logger'
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
|
||||
export abstract class BaseController {
|
||||
protected readonly logger: Logger = new Logger('http')
|
||||
protected readonly logger = Logger.type(LoggerType.HTTP)
|
||||
|
||||
protected sendSuccess(res: Response, data?: any, message?: string, status: number = 200) {
|
||||
return res.status(status).json({
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { EntityManager } from '@mikro-orm/core'
|
||||
|
||||
import Database from '#application/database'
|
||||
import { Logger } from '#application/logger'
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
|
||||
export abstract class BaseEntity {
|
||||
protected readonly logger: Logger = new Logger('entity')
|
||||
protected readonly logger = Logger.type(LoggerType.ENTITY)
|
||||
|
||||
private getEntityManager(): EntityManager {
|
||||
return Database.getEntityManager()
|
||||
|
@ -1,10 +1,10 @@
|
||||
import { Server } from 'socket.io'
|
||||
|
||||
import { Logger } from '#application/logger'
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
import { TSocket } from '#application/types'
|
||||
|
||||
export abstract class BaseEvent {
|
||||
protected readonly logger: Logger = new Logger('game')
|
||||
protected readonly logger = Logger.type(LoggerType.GAME)
|
||||
|
||||
constructor(
|
||||
readonly io: Server,
|
||||
|
@ -2,10 +2,10 @@ import { EntityManager } from '@mikro-orm/core'
|
||||
|
||||
import Database from '../database'
|
||||
|
||||
import { Logger } from '#application/logger'
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
|
||||
export abstract class BaseRepository {
|
||||
protected readonly logger: Logger = new Logger('repository')
|
||||
protected readonly logger = Logger.type(LoggerType.REPOSITORY)
|
||||
|
||||
protected get em(): EntityManager {
|
||||
return Database.getEntityManager()
|
||||
|
@ -1,5 +1,5 @@
|
||||
import { Logger } from '#application/logger'
|
||||
import Logger, { LoggerType } from '#application/logger'
|
||||
|
||||
export abstract class BaseService {
|
||||
protected readonly logger: Logger = new Logger('game')
|
||||
protected readonly logger = Logger.type(LoggerType.GAME)
|
||||
}
|
||||
|
@ -1,20 +1,21 @@
|
||||
import { EntityManager } from '@mikro-orm/core'
|
||||
import { MikroORM } from '@mikro-orm/mysql'
|
||||
|
||||
import { appLogger } from './logger'
|
||||
import Logger, { LoggerType } from './logger'
|
||||
import config from '../../mikro-orm.config'
|
||||
|
||||
class Database {
|
||||
private static orm: MikroORM
|
||||
private static em: EntityManager
|
||||
private static logger = Logger.type(LoggerType.APP)
|
||||
|
||||
public static async initialize(): Promise<void> {
|
||||
try {
|
||||
Database.orm = await MikroORM.init(config)
|
||||
Database.em = Database.orm.em.fork()
|
||||
appLogger.info('Database connection initialized')
|
||||
this.logger.info('Database connection initialized')
|
||||
} catch (error) {
|
||||
appLogger.error(`MikroORM connection failed: ${error}`)
|
||||
this.logger.error(`MikroORM connection failed: ${error}`)
|
||||
throw error
|
||||
}
|
||||
}
|
||||
|
@ -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