Logging is an essential part of any application, offering critical insights into the system's behavior, aiding in debugging, and monitoring performance. In the Node.js ecosystem, Pino stands out as a fast, low-overhead logging library. In this guide, we will explore how to implement Pino logger in a Node.js and ExpressJS application.
Why Pino?
Pino is favored for several reasons:
Performance: It is designed to be extremely fast, with minimal overhead.
Structured Logs: Logs are output in JSON format, making them easy to parse and analyze.
Ecosystem: Pino offers a rich ecosystem of plugins and integrations.
Setting Up Your Node.js and Express Project
Before we start, ensure you have Node.js and npm installed. Create a new Node.js project and set up ExpressJS:
mkdir express-pino-logger
cd express-pino-logger
npm init -y
npm install express
Installing Pino and Related Packages
Next, install Pino and its HTTP logging middleware for Express:
npm install pino pino-http
Optionally, you can install pino-pretty
for better log readability during development:
npm install pino-pretty --save-dev
Configuring Pino in Your Express Application
Create a Logger Configuration File
Start by creating a
logger.js
file to configure Pino:const pino = require('pino'); const logger = pino({ level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info', transport: process.env.NODE_ENV !== 'production' ? { target: 'pino-pretty', options: { colorize: true } } : undefined }); module.exports = logger;
Integrate Pino HTTP Middleware
Next, set up the Pino HTTP middleware in your Express application. Modify your
app.js
(orindex.js
if you are using that as your main file):const express = require('express'); const pino = require('pino-http'); const logger = require('./logger'); const app = express(); // Use Pino HTTP middleware app.use(pino({ logger })); app.get('/', (req, res) => { req.log.info('Root route accessed'); res.send('Hello World!'); }); const port = 3000; app.listen(port, () => { logger.info(`Server is running on port ${port}`); });
Using Pino Logger in Your Application
With Pino integrated, you can now use the logger within your application routes and middleware.
Logging Requests and Responses
Pino HTTP middleware logs incoming requests and outgoing responses automatically. The logs include details such as HTTP method, URL, status code, and response time.
Custom Logging
You can log custom messages within your routes and middleware using req.log
. For example:
app.get('/custom', (req, res) => {
req.log.info('Custom route accessed');
res.send('This is a custom route');
});
Pretty-Printing Logs in Development
To enhance log readability during development, ensure the transport
option is set up correctly in the logger.js
file. The pino-pretty
transport formats the logs for better readability:
const logger = pino({
level: process.env.NODE_ENV !== 'production' ? 'debug' : 'info',
transport: process.env.NODE_ENV !== 'production' ? {
target: 'pino-pretty',
options: {
colorize: true
}
} : undefined
});
Conclusion
Integrating Pino logger into a Node.js and ExpressJS application significantly enhances logging capabilities while maintaining performance. Pino's structured logging and extensive feature set make it an excellent choice for modern web applications. By following this guide, you can set up and start using Pino in your ExpressJS projects, ensuring efficient and structured logging.
Happy coding!