Enrich 500 error logs with request context and actual error messages
- pino-http customErrorMessage now includes the real error message - customProps includes reqBody, reqParams, reqQuery, and routePath for 4xx/5xx - Error handler logs full request context (body, params, query) for both HttpError 500s and unhandled errors Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -52,10 +52,28 @@ export const httpLogger = pinoHttp({
|
||||
customSuccessMessage(req, res) {
|
||||
return `${req.method} ${req.url} ${res.statusCode}`;
|
||||
},
|
||||
customErrorMessage(req, res) {
|
||||
return `${req.method} ${req.url} ${res.statusCode}`;
|
||||
customErrorMessage(req, res, err) {
|
||||
const errMsg = err?.message || (res as any).err?.message || "unknown error";
|
||||
return `${req.method} ${req.url} ${res.statusCode} — ${errMsg}`;
|
||||
},
|
||||
customProps() {
|
||||
customProps(req, res) {
|
||||
if (res.statusCode >= 400) {
|
||||
const props: Record<string, unknown> = {};
|
||||
const { body, params, query } = req as any;
|
||||
if (body && typeof body === "object" && Object.keys(body).length > 0) {
|
||||
props.reqBody = body;
|
||||
}
|
||||
if (params && typeof params === "object" && Object.keys(params).length > 0) {
|
||||
props.reqParams = params;
|
||||
}
|
||||
if (query && typeof query === "object" && Object.keys(query).length > 0) {
|
||||
props.reqQuery = query;
|
||||
}
|
||||
if ((req as any).route?.path) {
|
||||
props.routePath = (req as any).route.path;
|
||||
}
|
||||
return props;
|
||||
}
|
||||
return {};
|
||||
},
|
||||
});
|
||||
|
||||
Reference in New Issue
Block a user