fixed some errors preventing request logging

This commit is contained in:
2025-12-31 10:13:57 -05:00
parent 42b96d58a0
commit 46988f1921
3 changed files with 8 additions and 5 deletions

View File

@@ -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'],
},
}

View File

@@ -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));