Intermediate LevelExpress JS
How do you integrate Morgan HTTP request logging with Winston structured JSON logging?
❓ Interview Prompt: Explain morgan HTTP request logging formats, winston structured JSON logging, and streaming HTTP audit logs to centralized logging platforms.
Standard 'console.log' statements produce unformatted plain-text logs that cannot be searched or indexed efficiently in production log aggregators (Datadog, ElasticSearch/ELK).
Morgan + Winston Integration:
1
Morgan: Automatically logs incoming HTTP requests (method, URL, status code, response time ms).
2
Winston: Formats log messages into structured JSON objects.
3
Stream Integration: Configure Morgan to stream its HTTP log lines directly into Winston's JSON logger stream!
Code Implementation Example
javascript
import express from 'express';
import morgan from 'morgan';
import winston from 'winston';
const logger = winston.createLogger({
format: winston.format.json(),
transports: [new winston.transports.Console()]
});
const app = express();
// Stream Morgan HTTP request logs directly into Winston JSON logger!
app.use(morgan(':method :url :status :res[content-length] - :response-time ms', {
stream: { write: (message) => logger.info(message.trim()) }
}));💡Streaming Morgan HTTP request logs into Winston structured JSON logger.
🎯 Interview Key Takeaway
Stream Morgan HTTP request logs into Winston to produce structured JSON logs suitable for Datadog and ELK indexing.
#morgan#winston#Logging#Intermediate
Back to all Express JS Questions→