forked from noxious/server
#140 : Individual log files
This commit is contained in:
@ -9,9 +9,9 @@ import tileRepository from '../repositories/tileRepository'
|
||||
import objectRepository from '../repositories/objectRepository'
|
||||
import spriteRepository from '../repositories/spriteRepository'
|
||||
import fs from 'fs'
|
||||
import logger from './logger'
|
||||
import zoneRepository from '../repositories/zoneRepository'
|
||||
import zoneManager from '../managers/zoneManager'
|
||||
import { httpLogger } from './logger'
|
||||
|
||||
async function addHttpRoutes(app: Application) {
|
||||
/**
|
||||
@ -118,13 +118,13 @@ async function addHttpRoutes(app: Application) {
|
||||
}
|
||||
|
||||
if (!fs.existsSync(assetPath)) {
|
||||
logger.error(`File not found: ${assetPath}`)
|
||||
httpLogger.error(`File not found: ${assetPath}`)
|
||||
return res.status(404).send('Asset not found')
|
||||
}
|
||||
|
||||
res.sendFile(assetPath, (err) => {
|
||||
if (err) {
|
||||
logger.error('Error sending file:', err)
|
||||
httpLogger.error('Error sending file:', err)
|
||||
res.status(500).send('Error downloading the asset')
|
||||
}
|
||||
})
|
||||
@ -179,7 +179,7 @@ async function addHttpRoutes(app: Application) {
|
||||
return res.status(400).json({ message: 'Failed to register user' })
|
||||
})
|
||||
|
||||
logger.info('Web routes added')
|
||||
httpLogger.info('Web routes added')
|
||||
}
|
||||
|
||||
export { addHttpRoutes }
|
||||
|
@ -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 }
|
Reference in New Issue
Block a user