mirror of
https://codeberg.org/SafeTwitch/safetwitch.git
synced 2024-12-22 21:33:01 -05:00
39 lines
No EOL
1,018 B
TypeScript
39 lines
No EOL
1,018 B
TypeScript
import { randomUUID } from 'crypto'
|
|
import { createLogger, format, transports } from 'winston'
|
|
import { NextFunction, Request, Response } from 'express'
|
|
import util from 'util'
|
|
|
|
const logLevels = {
|
|
fatal: 0,
|
|
error: 1,
|
|
warn: 2,
|
|
info: 3,
|
|
debug: 4,
|
|
trace: 5,
|
|
};
|
|
|
|
|
|
export const logger = createLogger({
|
|
format: format.combine(format.timestamp(), format.json()),
|
|
transports: [new transports.Console({}), new transports.File({ filename: './serverLog.log' })],
|
|
levels: logLevels
|
|
});
|
|
|
|
export const errorHandler = (err: Error, req: Request, res: Response, next: NextFunction) => {
|
|
if (res.headersSent) {
|
|
return next(err)
|
|
}
|
|
|
|
res.status(500).send({ status: 'error', message: err.message, code: res.locals.uuid })
|
|
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) => {
|
|
res.locals.uuid = randomUUID()
|
|
next()
|
|
} |