What is pino-http?
pino-http is an HTTP logger middleware for Node.js that integrates with the Pino logging library. It provides high-performance logging for HTTP requests and responses, making it suitable for production environments where speed and efficiency are critical.
What are pino-http's main functionalities?
Basic HTTP Logging
This code sets up a basic HTTP server with pino-http middleware to log incoming requests and outgoing responses.
const http = require('http');
const pino = require('pino');
const pinoHttp = require('pino-http');
const logger = pino();
const httpLogger = pinoHttp({ logger });
const server = http.createServer((req, res) => {
httpLogger(req, res);
res.end('hello world');
});
server.listen(3000);
Custom Log Levels
This code demonstrates how to set custom log levels for the pino-http logger.
const http = require('http');
const pino = require('pino');
const pinoHttp = require('pino-http');
const logger = pino({ level: 'debug' });
const httpLogger = pinoHttp({ logger });
const server = http.createServer((req, res) => {
httpLogger(req, res);
res.end('hello world');
});
server.listen(3000);
Custom Log Formatting
This code shows how to customize the log output format using Pino's prettyPrint options.
const http = require('http');
const pino = require('pino');
const pinoHttp = require('pino-http');
const logger = pino({
prettyPrint: {
colorize: true,
translateTime: 'SYS:standard',
ignore: 'pid,hostname'
}
});
const httpLogger = pinoHttp({ logger });
const server = http.createServer((req, res) => {
httpLogger(req, res);
res.end('hello world');
});
server.listen(3000);
Other packages similar to pino-http
morgan
Morgan is another HTTP request logger middleware for Node.js. It is simpler and more lightweight compared to pino-http, but it does not offer the same level of performance and flexibility in terms of log formatting and custom log levels.
winston
Winston is a versatile logging library for Node.js that can also be used for HTTP request logging. It provides more features and flexibility than pino-http, such as multiple transport layers and log levels, but it is generally slower in performance.
bunyan
Bunyan is another JSON logging library for Node.js that can be used for HTTP request logging. It offers similar features to pino-http, such as high performance and structured logging, but it is not as widely adopted or actively maintained as Pino.
pino-http
High-speed HTTP logger for Node.js
To our knowledge, pino-http
is the fastest HTTP logger in town.
Benchmarks
Benchmarks log each request/response pair while returning
'hello world'
, using
autocannon with 100
connections and 10 pipelined requests.
http-ndjson
(equivalent info): 7730.73 req/sechttp-ndjson
(standard minimum info): 9522.37 req/secpino-http
: 21496 req/secpino-http
(extreme): 25770.91 req/sec- no logger: 46139.64 req/sec
All benchmarks where taken on a Macbook Pro 2013 (2.6GHZ i7, 16GB of RAM).
Install
npm i pino-http --save
Example
'use strict'
var http = require('http')
var server = http.createServer(handle)
var logger = require('../')()
function handle (req, res) {
logger(req, res)
res.end('hello world')
}
server.listen(3000)
$ node example.js | pino
[2016-03-31T16:53:21.079Z] INFO (46316 on MBP-di-Matteo): something else
req: {
"id": 1,
"method": "GET",
"url": "/",
"headers": {
"host": "localhost:3000",
"user-agent": "curl/7.43.0",
"accept": "*/*"
},
"remoteAddress": "::1",
"remotePort": 64386
}
[2016-03-31T16:53:21.087Z] INFO (46316 on MBP-di-Matteo): request completed
res: {
"statusCode": 200,
"header": "HTTP/1.1 200 OK\r\nX-Powered-By: restify\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 11\r\nETag: W/\"b-XrY7u+Ae7tCTyyK7j1rNww\"\r\nDate: Thu, 31 Mar 2016 16:53:21 GMT\r\nConnection: keep-alive\r\n\r\n"
}
responseTime: 10
req: {
"id": 1,
"method": "GET",
"url": "/",
"headers": {
"host": "localhost:3000",
"user-agent": "curl/7.43.0",
"accept": "*/*"
},
"remoteAddress": "::1",
"remotePort": 64386
}
API
pino-http
has the same options as pino.
pino-http
attaches listeners to the request, in order to log when the request completes
Team
Matteo Collina
https://github.com/mcollina
https://www.npmjs.com/~matteo.collina
https://twitter.com/matteocollina
David Mark Clements
https://github.com/davidmarkclements
https://www.npmjs.com/~davidmarkclements
https://twitter.com/davidmarkclem
License
MIT