Logging with Morgan and Winston
Logging with Morgan and Winston
In a Node.js application, logging is crucial for monitoring, debugging, and maintaining the application. Two popular libraries for logging are:
Morgan: A middleware for logging HTTP requests.
Winston: A flexible and powerful logging library for general-purpose logging.
They are often used together: Morgan for HTTP request logs, and Winston for application-level logs.
๐ What is Morgan?
Morgan is an HTTP request logger middleware for Node.js. It's used with frameworks like Express to log details about incoming requests.
Installation:
bash
Copy
Edit
npm install morgan
Basic Usage:
javascript
Copy
Edit
const express = require('express');
const morgan = require('morgan');
const app = express();
// Use 'combined' format for detailed logs
app.use(morgan('combined'));
app.get('/', (req, res) => {
res.send('Hello World!');
});
app.listen(3000);
Available Formats:
combined – Standard Apache combined log output
common – Standard Apache common log output
dev – Concise colored output for development
short – Shorter than common
tiny – Minimal output
You can also define a custom format if needed.
๐ง What is Winston?
Winston is a versatile logging library for Node.js that supports:
Multiple transports (console, files, databases)
Logging levels
Log formatting
Customization
Installation:
bash
Copy
Edit
npm install winston
Basic Usage:
javascript
Copy
Edit
const winston = require('winston');
const logger = winston.createLogger({
level: 'info',
format: winston.format.simple(),
transports: [
new winston.transports.Console(), // Logs to console
new winston.transports.File({ filename: 'app.log' }) // Logs to a file
],
});
// Logging messages
logger.info('Application started');
logger.warn('This is a warning');
logger.error('An error occurred');
Logging Levels (Default):
error
warn
info
http
verbose
debug
silly
๐งฉ Combining Morgan with Winston
You can route Morgan’s HTTP logs to Winston for centralized logging.
javascript
Copy
Edit
const express = require('express');
const morgan = require('morgan');
const winston = require('winston');
const stream = require('stream');
const app = express();
// Winston logger setup
const logger = winston.createLogger({
transports: [
new winston.transports.File({ filename: 'combined.log' }),
new winston.transports.Console()
],
format: winston.format.combine(
winston.format.timestamp(),
winston.format.simple()
)
});
// Stream for Morgan to use Winston
const morganStream = {
write: (message) => logger.info(message.trim())
};
// Use Morgan with Winston stream
app.use(morgan('combined', { stream: morganStream }));
app.get('/', (req, res) => {
logger.info('Root endpoint hit');
res.send('Hello with Winston and Morgan!');
});
app.listen(3000, () => {
logger.info('Server is running on port 3000');
});
✅ When to Use What
Task Use
Log HTTP requests Morgan
Log application events Winston
Centralize all logs Combine both
Log to files or services Winston
๐งฉ Conclusion
Morgan is great for quick HTTP request logging in Express.
Winston is a powerful logging tool for application logs.
Combine them to create a complete logging solution for your Node.js application.
Learn MERN Stack Course in Hyderabad
Read More
Building a Pagination API with MongoDB
Visit Our Quality Thought Training in Hyderabad
Comments
Post a Comment