errorlog
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
import chalk from 'chalk';
|
||||
import { isEmpty, prettyJson } from '../../util/prettyJson.js';
|
||||
import { writeErrorLog } from '../../util/errorLog.js';
|
||||
|
||||
function logField(label, value, { skipEmpty = false } = {}) {
|
||||
if (skipEmpty && isEmpty(value)) return;
|
||||
@@ -56,6 +57,60 @@ export const loggerMiddleware = (req, res, next) => {
|
||||
}
|
||||
logField('body', req.body ?? {}, { skipEmpty: true });
|
||||
logField('response', getBody());
|
||||
|
||||
// Dump full request/response/error details to a log file on 5xx.
|
||||
const statusCode = res.statusCode;
|
||||
if (statusCode >= 500) {
|
||||
const body = getBody();
|
||||
writeErrorLog(
|
||||
`${req.method} ${req.originalUrl} ${statusCode}`,
|
||||
{
|
||||
request: {
|
||||
method: req.method,
|
||||
url: req.originalUrl,
|
||||
headers: req.headers,
|
||||
params: req.params,
|
||||
query: req.query,
|
||||
},
|
||||
body: req.body,
|
||||
files: Array.isArray(req.files)
|
||||
? req.files.map((f) => ({
|
||||
fieldname: f.fieldname,
|
||||
originalname: f.originalname,
|
||||
mimetype: f.mimetype,
|
||||
size: f.size,
|
||||
encoding: f.encoding,
|
||||
bufferHeadHex: f.buffer
|
||||
? [...f.buffer.subarray(0, 32)]
|
||||
.map((b) => b.toString(16).padStart(2, '0'))
|
||||
.join(' ')
|
||||
: null,
|
||||
}))
|
||||
: undefined,
|
||||
response: {
|
||||
status: statusCode,
|
||||
body,
|
||||
},
|
||||
error: res.locals?.error
|
||||
? {
|
||||
message: res.locals.error.message,
|
||||
code: res.locals.error.code,
|
||||
number: res.locals.error.number,
|
||||
state: res.locals.error.state,
|
||||
class: res.locals.error.class,
|
||||
lineNumber: res.locals.error.lineNumber,
|
||||
procName: res.locals.error.procName,
|
||||
stack: res.locals.error.stack,
|
||||
precedingErrors: res.locals.error.precedingErrors?.map((e) => ({
|
||||
message: e.message,
|
||||
number: e.number,
|
||||
state: e.state,
|
||||
})),
|
||||
}
|
||||
: undefined,
|
||||
},
|
||||
);
|
||||
}
|
||||
});
|
||||
|
||||
next();
|
||||
|
||||
Reference in New Issue
Block a user