In this tutorial, we aim to understand how to log errors with Winston and Morgan, two highly used logging libraries in Express.js. Logging errors is a significant part of any application as it helps in debugging and maintaining the application.
Winston is a versatile logging library for Node.js. It is designed to be a simple and universal logging library with support for multiple transports. A transport is essentially a storage device for your logs.
Morgan is another HTTP request logger middleware for Node.js. It simplifies the process of logging requests to your application.
To install Winston and Morgan, run the following command in your terminal:
npm install winston morgan
We will create an Express application and integrate Winston and Morgan into it for logging.
// Importing express module
const express = require('express');
// Creating express application
const app = express();
// A simple GET route
app.get('/', (req, res) => {
res.send('Hello World!');
});
// Starting the server
app.listen(3000, () => {
console.log('Server is running at http://localhost:3000');
});
// Importing required modules
const express = require('express');
const winston = require('winston');
const morgan = require('morgan');
// Creating express application
const app = express();
// Creating new Winston logger
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.Console()
]
});
// Using morgan middleware for logging
app.use(morgan('combined', { stream: { write: message => logger.info(message.trim()) } }));
// A simple GET route
app.get('/', (req, res) => {
res.send('Hello World!');
throw new Error('This is an error');
});
// Error handling middleware
app.use((err, req, res, next) => {
logger.error(err.message);
res.status(500).send('Something went wrong!');
});
// Starting the server
app.listen(3000, () => {
console.log('Server is running at http://localhost:3000');
});
We have learned about the Winston and Morgan logging libraries and how to integrate them into an Express.js application. Furthermore, we have learned how to log errors using these libraries.
To delve deeper into these libraries, you can visit their official documentation: Winston and Morgan.
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
transports: [
new winston.transports.File({ filename: 'error.log', level: 'error' }),
new winston.transports.File({ filename: 'info.log', level: 'info' }),
new winston.transports.Console()
]
});
To log different information about the requests and responses, you can modify the string passed to the morgan function. See the Morgan documentation for more details.
To handle unhandled promise rejections, you can listen for the unhandledRejection
event on the process object.
process.on('unhandledRejection', (reason, promise) => {
logger.error('Unhandled Promise Rejection: ', reason);
});
Remember that practice is key when learning new concepts. Keep experimenting with different scenarios and configurations to get a better understanding of Winston and Morgan.