Every request to the walks API now leaves one JSON line on standard output, and jq can pick out the lines you ask for. One piece of output is still left over from the starting project. When a booking fails with a 500, the error handler prints the error as free text, on a stream jq never sees.
In this lesson, you’ll move that error onto a JSON line of its own, under the same request ID. You’ll also decide what must never reach the log, such as an owner’s phone number, and use a request ID from a user’s report to find the lines that explain the failure. Start with the error that’s still free text.
Put the 500 on a JSON Line
A server error is the event you most need to find later, yet its log line is the one your tools can’t read. Today, the client gets the generic error body, and the error handler’s console.error() prints the rest. Here’s a failed booking in the API’s terminal, with the stack truncated and the JSON line wrapped for display:
Request 5a765868-d737-4646-a2a7-7cf2d46ce9d9 failed: Error: Simulated save failure
at file:///.../server.ts:61:9
...
{"time":"2026-11-27T19:11:04.410Z","level":"error","method":"POST",
"path":"/walks","status":500,"duration_ms":14,
"request_id":"5a765868-d737-4646-a2a7-7cf2d46ce9d9"}
console.error() writes to standard error, a second output stream next to standard output. Your terminal shows both, but a pipe such as npm start --silent | jq carries only standard output, so jq never sees the error. To fix that, send the error through the same log() function as every other line.
Why not pass err to log() as a field and let JSON.stringify() handle it? Try it in Node’s REPL:
> const failure = new Error("Simulated save failure");
undefined
> JSON.stringify({ error: failure })
'{"error":{}}'
> failure.stack.split("\n")[0]
'Error: Simulated save failure'
An Error keeps its message and stack in properties that JSON.stringify() skips, so the error turns into an empty object. Its .stack, however, is a plain string that starts with the message, so log that. In the error handler, replace the console.error() line with a call to log():
server.ts
log("error", {
message: "Request failed",
request_id: res.locals.requestId,
stack: err instanceof Error ? err.stack : "Not an Error",
});
Only a 500 reaches this line, because the 4xx branches above it still answer and return first. The error handler’s err parameter is unknown, because JavaScript can throw any value. The instanceof check takes the stack from a real Error. Anything else gets a fixed note instead, since a thrown string could hold a token.