Logging and error hooks
Dotpress logs through one logger, given to the app, and gives each request a logger of its own, bound to its request ID.
The logger
By default, the app logs to the console, from the info level. Pass your logger to createApp():
The Logger interface is the one of Pino: a Pino instance works as is. Any logger with debug, info, warn, error and child() methods, accepting (message) or (data, message), works too.
The default logger can be configured:
The request logger
ctx.logger is a child logger: every line it writes carries the requestId of the request.
Pass ctx.logger to the services a request calls: their log lines are tied to the request. ctx.events and ctx.mailer use the request logger for you.
Request IDs
Each request gets an ID, crypto.randomUUID() by default. It is:
ctx.requestIdin handlers and middlewares;- bound to every line of
ctx.logger; - sent back in the
X-Request-IDheader of every response, errors included.
To reuse the ID set by a gateway or a load balancer, use generateRequestId. Accept it only from a proxy you trust, and check its format:
Logging every request
A small plugin logs every response with the onResponse hook, including the 404 and 405 of the router:
The hook receives the request, the final response, the request ID, the duration in milliseconds, and the route when one matched (route.path is the declared path, such as /users/:id: a good label for metrics).
Reporting errors
Unexpected errors are logged with the request logger, then passed to the onError hooks: the one of createApp(), and those of the plugins (api.onError()). See Errors.
Tracing and request-scoped storage
The wrapRequest hook of a plugin runs the whole pipeline of a request inside a function: a tracing span, or an AsyncLocalStorage context that services read without receiving ctx.
The hook must call next() once, and return its result.
Pino is no longer loaded automatically: pass it to createApp({ logger }). enableHttpLogging and onRequestComplete are replaced by the onResponse hook of a plugin, and onException by onError.