Pino HTTP Debug Printer
pino-http-print is a specialized utility for formatting and printing HTTP request and response logs generated by Pino-compatible HTTP loggers (such as pino-http, express-pino-logger, restify-pino-logger, or koa-pino-logger) into a concise, human-readable, `curl`-like output. It functions primarily as a Pino transport, transforming raw JSON log lines into an easily digestible format suitable for console debugging or integration with other output streams. The current stable version is v4.0.0. As part of the pinojs ecosystem, releases typically align with broader Pino updates, ensuring compatibility with current Node.js versions and core Pino features. Its key differentiation lies in its dedicated focus on structuring HTTP log data for rapid analysis, while also capable of leveraging `pino-pretty` to format general application logs when the `all` option is enabled, providing a cohesive debugging experience.
Common errors
-
Error: Cannot find module 'pino-http-print'
cause The `pino-http-print` package is not installed as a dependency in your project or globally for CLI usage.fixInstall the package using npm: `npm install pino-http-print` for local use, or `npm install -g pino-http-print` if planning to use it via the CLI as a legacy Pino transport. -
TypeError: pino is not a function
cause This error often occurs when `pino.transport` is called incorrectly, or when the returned transport configuration is not properly passed to the `pino()` constructor to create a logger instance.fixEnsure you are creating your logger by passing the transport *configuration object* to `pino()`: `const transport = pino.transport({ target: 'pino-http-print', ... }); const logger = pino(transport);`
Warnings
- breaking Version 4.0.0 of `pino-http-print` officially dropped support for Node.js 12. Running on unsupported Node.js versions may lead to unexpected behavior or failures.
- gotcha Options specific to `pino-pretty` (e.g., `ignore`, `sync`) must be encapsulated within the `prettyOptions` object when passed to `pino-http-print`, especially when `all: true` is enabled.
- gotcha The `destination` option defaults to `1`, representing standard output (stdout). If you intend to redirect logs to a file or another custom stream, this option must be explicitly set.
Install
-
npm install pino-http-print -
yarn add pino-http-print -
pnpm add pino-http-print
Imports
- httpPrintFactory
import pinoHttpPrint from 'pino-http-print'
import { httpPrintFactory } from 'pino-http-print' - httpPrintFactory
const pinoHttpPrint = require('pino-http-print')const { httpPrintFactory } = require('pino-http-print') - 'pino-http-print'
const transport = pino.transport('pino-http-print', { ... })const transport = pino.transport({ target: 'pino-http-print', options: { ... } })
Quickstart
const pino = require('pino');
const http = require('http');
const pinoHttp = require('pino-http');
// Configure pino-http-print as a Pino transport
const transport = pino.transport({
target: 'pino-http-print',
options: {
destination: 1, // Output to stdout
all: true, // Also print non-HTTP logs using pino-pretty
colorize: true,
translateTime: 'SYS:HH:MM:ss', // Custom time format
prettyOptions: {
ignore: 'pid,hostname', // Example pino-pretty option to ignore fields
}
}
});
// Create a Pino logger instance using the configured transport
const logger = pino(transport);
// Integrate pino-http with the logger
const httpLogger = pinoHttp({ logger });
const server = http.createServer((req, res) => {
httpLogger(req, res); // Attach pino-http to the request
if (req.url === '/') {
logger.info('Handling root request.');
res.end('Hello, World!');
} else if (req.url === '/error') {
logger.error('An example error log for internal server error.');
res.statusCode = 500;
res.end('Internal Server Error');
} else {
logger.warn('Unknown path accessed.');
res.statusCode = 404;
res.end('Not Found');
}
});
server.listen(3000, () => {
logger.info('Server listening on http://localhost:3000');
console.log('To test, run this script and then use curl in another terminal:');
console.log(' curl http://localhost:3000');
console.log(' curl http://localhost:3000/error');
console.log(' curl http://localhost:3000/unknown');
});