Q35: What is Morgan middleware and how do you configure standard log formats?
morgan is an HTTP request logging middleware that records details about incoming requests and outgoing status codes.
Configuring Morgan Log Profiles
const morgan = require('morgan');
// Predefined profiles: 'combined', 'common', 'dev', 'short', 'tiny'
if (process.env.NODE_ENV === 'production') {
app.use(morgan('combined')); // Standard Apache combined format
} else {
app.use(morgan('dev')); // Color-coded concise logging
}💡 Morgan simplifies debugging by logging status codes, methods, and response times.
Key Takeaways
- Use
devlogging in local development; use structuredcombinedlogging in production. - Can pipe directly into filesystem logs or external log aggregators.