0
Fork 0
mirror of https://codeberg.org/SafeTwitch/safetwitch.git synced 2024-12-22 13:22:58 -05:00

Optimise logger

This commit is contained in:
dragongoose 2023-03-07 11:20:22 -05:00
parent 500485c2e3
commit f118de2038

View file

@ -12,14 +12,9 @@ const logLevels = {
trace: 5, trace: 5,
}; };
let currentUUID: string;
const addReqId = format((info) => {
info.reqId = currentUUID
return info
})
export const logger = createLogger({ export const logger = createLogger({
format: format.combine(addReqId(), format.timestamp(), format.json()), format: format.combine(format.timestamp(), format.json()),
transports: [new transports.Console({}), new transports.File({ filename: './serverLog.log' })], transports: [new transports.Console({}), new transports.File({ filename: './serverLog.log' })],
levels: logLevels levels: logLevels
}); });
@ -29,12 +24,16 @@ export const errorHandler = (err: Error, req: Request, res: Response, next: Next
return next(err) return next(err)
} }
currentUUID = res.locals.uuid
res.status(500).send({ status: 'error', message: err.message, code: res.locals.uuid }) res.status(500).send({ status: 'error', message: err.message, code: res.locals.uuid })
logger.warn(err.message) logger.warn({
message: err.message,
endpoint: req.originalUrl,
reqId: res.locals.uuid,
origin: req.headers.origin,
})
} }
export const uuid = (req: Request, res: Response, next: NextFunction) => { export const uuid = (req: Request, res: Response, next: NextFunction) => {
res.locals.uuid = randomUUID() res.locals.uuid = randomUUID()
next(res) next()
} }