Logging in Node.js: Winston vs Morgan vs Pino

|
| By Webner

Logging is an essential part of any application. It helps developers understand what is happening in the app, debug issues, and monitor performance. In Node.js, there are several popular logging libraries available. In this post, we will compare three of them: Winston, Morgan, and Pino. We will also see examples of how to use them.

1. Why Logging is Important

Before diving into libraries, let’s understand why logging matters:

  • Debugging: Find errors and fix bugs quickly.
  • Monitoring: Track application behavior in real time.
  • Auditing: Maintain records of user actions and system events.

2. Winston

Winston is a versatile logging library for Node.js. It allows you to log messages in different formats and store them in files, databases, or the console.

Key Features:

  • Multiple transports (console, file, database, etc.)
  • Custom log levels
  • JSON formatting support

Example:

 const winston = require(‘winston’);  const logger = winston.createLogger({

     level: ‘info’,

     format: winston.format.json(),

     transports: [

         new winston.transports.Console(),

         new winston.transports.File({ filename: ‘app.log’ })

     ],

 });

 

 logger.info(‘This is an info message’);

 logger.error(‘This is an error message’);

In the above example, messages are logged to both the console and a file named app.log.

3. Morgan

Morgan is a middleware specifically used for logging HTTP requests in Express applications. It is simple and lightweight.

Key Features:

  • HTTP request logging
  • Predefined formats like tiny, combined, dev
  • Works as Express middleware

Example:

 const express = require(‘express’); const morgan = require(‘morgan’);

 const app = express();

 // Log HTTP requests in ‘dev’ format

 app.use(morgan(‘dev’));

 app.get(‘/’, (req, res) => {

     res.send(‘Hello World!’);

 });

 app.listen(3000, () => console.log(‘Server running on port 3000’));

Morgan logs each HTTP request with details like method, URL, status code, and response time. It’s great for monitoring API requests.

4. Pino

Pino is a fast and low-overhead logging library. It is designed for performance-critical applications.

Key Features:

  • Extremely fast logging
  • JSON output
  • Works well with log management tools

Example:

 const pino = require(‘pino’); const logger = pino({ level: ‘info’ });

 

 logger.info(‘Server started’);

 logger.error(‘An error occurred’);

Pino logs messages in JSON format, which is easy to parse and analyze with log management systems.

5. Comparing Winston, Morgan, and Pino

Feature Winston Morgan Pino
Use Case General logging HTTP request logging Fast, low-overhead logging
Performance Medium Fast Very fast
Output Format JSON, Console, File, etc. Text-based logs JSON
Integration Express + Any Node.js app Express middleware Express + Any Node.js app

 

6. When to Use Each

  • Use Winston if you need a flexible logger for different types of logs.
  • Use Morgan if you only need HTTP request logs in an Express app.
  • Use Pino if you want high-performance logging, especially for production apps.

7. Summary

Choosing the right logging library depends on your application needs. Winston is flexible, Morgan is perfect for HTTP requests, and Pino is lightning-fast. Often, developers use a combination of these libraries—for example, Morgan for HTTP requests and Winston for application logs.

Logging is not just about printing messages; it’s about understanding your app and making it maintainable. So pick the right tool and start logging sma

Leave a Reply

Your email address will not be published. Required fields are marked *