Lesson 23 / 25
Request Logging with morgan & pino
Log HTTP requests with morgan for development and structured pino-http logs for production.
morgan and pino-http
morgan prints readable request lines; pino-http emits structured JSON that log tools can search.
import morgan from 'morgan';
import pinoHttp from 'pino-http';
if (process.env.NODE_ENV === 'production') {
app.use(pinoHttp());
} else {
app.use(morgan('dev'));
}
app.get('/', (req, res) => {
req.log?.info('home visited');
res.send('ok');
});Never log passwords, tokens or full request bodies. Include a request ID so you can follow one request across log lines.