#140 : Individual log files

This commit is contained in:
2024-09-21 15:39:50 +02:00
parent 90ac7728d9
commit 798bfac643
24 changed files with 136 additions and 135 deletions

View File

@ -1,11 +1,17 @@
import pino from 'pino'
import fs from 'fs'
import path from 'path'
const logger = pino({
// Array of log types
const LOG_TYPES = ['http', 'game', 'gameMaster', 'app'] as const
type LogType = typeof LOG_TYPES[number]
const createLogger = (name: LogType) => pino({
level: process.env.LOG_LEVEL || 'debug',
transport: {
target: 'pino/file',
options: {
destination: './logs/app.log',
destination: `./logs/${name}.log`,
mkdir: true
}
},
@ -15,7 +21,28 @@ const logger = pino({
}
},
timestamp: pino.stdTimeFunctions.isoTime,
base: null // This will prevent hostname and pid from being included
base: null
})
export default logger
// Create logger instances
const loggers = Object.fromEntries(
LOG_TYPES.map(type => [type, createLogger(type)])
) as Record<LogType, ReturnType<typeof createLogger>>
const watchLogs = () => {
LOG_TYPES.forEach(type => {
const logFile = path.join(__dirname, '../../logs', `${type}.log`)
fs.watchFile(logFile, (curr, prev) => {
if (curr.size > prev.size) {
const stream = fs.createReadStream(logFile, { start: prev.size, end: curr.size })
stream.on('data', (chunk) => {
console.log(`[${type}] \n ${chunk.toString()}`)
})
}
})
})
}
export const { http: httpLogger, game: gameLogger, gameMaster: gameMasterLogger, app: appLogger } = loggers
export { watchLogs }