fixed some errors preventing request logging
This commit is contained in:
@@ -22,8 +22,8 @@ app.use(morgan((tokens: morgan.TokenIndexer, req: express.Request, res: express.
|
||||
path: tokens.url(req, res),
|
||||
status: Number(tokens.status(req, res)),
|
||||
response_time_ms: Number(tokens['response-time'](req, res)),
|
||||
user_id: req.user.id,
|
||||
user_name: req.user.name,
|
||||
user_id: req.user?.id,
|
||||
user_name: req.user?.name,
|
||||
user_agent: req.headers['user-agent'],
|
||||
},
|
||||
}
|
||||
|
||||
@@ -20,12 +20,14 @@ const CURRENT_DEPTH: LogDepth = (process.env.LOG_DEPTH as LogDepth) || 'normal';
|
||||
|
||||
const DEPTH_ORDER: Record<LogDepth, number> = { normal: 0, verbose: 1 };
|
||||
|
||||
function shouldLog(level: LogLevel, depth: LogDepth) {
|
||||
return DEPTH_ORDER[depth] >= DEPTH_ORDER[CURRENT_DEPTH];
|
||||
function shouldLog(depth: LogDepth) {
|
||||
let should = DEPTH_ORDER[depth] <= DEPTH_ORDER[CURRENT_DEPTH]
|
||||
return should;
|
||||
}
|
||||
|
||||
function emitLog(header: LogHeader, payload: LogPayload = {}) {
|
||||
if (!shouldLog(header.level, header.depth)) return;
|
||||
if (!shouldLog(header.depth)) return;
|
||||
console.log(header, payload);
|
||||
|
||||
const logLine = { ...header, ...payload };
|
||||
console.log(JSON.stringify(logLine));
|
||||
|
||||
Reference in New Issue
Block a user