pino-http is a high-speed HTTP logger middleware designed for Node.js applications, leveraging the highly performant Pino logging library. It focuses on providing structured JSON logs for HTTP requests and responses with minimal overhead, making it ideal for high-throughput microservices and APIs. The package is currently at version 11.0.0 and follows a release cadence that often aligns with major updates to its underlying `pino` dependency, typically seeing several minor/patch releases throughout the year. Its primary differentiator is its exceptional performance compared to other HTTP loggers, achieved by deferring heavy log processing. It provides extensive customization options for log levels, request ID generation, and structured log data.
npm install pino-httpVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up `pino-http` with a custom base logger, request ID generation, and dynamic log level based on response status. It includes a basic HTTP server, showcasing how `req.log` is injected for contextual logging and how to use `pino-pretty` for development output.
Upgrade Node.js to version 20 or higher. If upgrading is not immediately possible, pin pino-http to a `^10.0.0` version.
Choose either `customLogLevel` (a function for dynamic level selection) or `useLevel` (a static level string) but not both.
Set `autoLogging: false` to disable automatic logging entirely, or `autoLogging: { ignore: (req) => req.url === '/healthz' }` for conditional exclusion.Carefully consider what data is serialized. Use Pino's redaction capabilities or custom serializers to remove sensitive information. Avoid logging large request bodies unless absolutely necessary.
Provide a custom `genReqId` function that generates truly unique IDs, e.g., using `uuid` or a tracing ID from upstream services (`req.headers['x-request-id']`).
Install `pino-pretty` (`npm i -D pino-pretty`) and configure Pino's `transport` option, typically only for development environments, to use it.
For CommonJS, ensure `const pinoHttp = require('pino-http')();` (with `()`). For ESM, use `import pinoHttp from 'pino-http';` then call it as `pinoHttp();`.Ensure `loggerMiddleware(req, res);` (where `loggerMiddleware` is your `pinoHttp()` instance) is called early in your HTTP request handling chain, typically as the first middleware in frameworks like Express.
Ensure that custom log levels are correctly defined in your Pino configuration via the `customLevels` option and that the `useLevel` or `customLogLevel` functions return one of these defined levels or standard Pino levels.