Introduction to Morgan: Specialized Logging for Node.js Web Applications
In the world of Node.js web development, monitoring HTTP requests is critical for understanding application performance and user behavior. Morgan has established itself as the definitive HTTP request logger for Express applications, offering focused functionality for capturing valuable request metadata.
Why Morgan is the Go-To HTTP Request Logger for Express
Morgan has become an essential tool in the Express ecosystem for several key reasons:
- Express Integration: Designed specifically as middleware for Express applications
- Predefined Log Formats: Common, combined, dev, short, and tiny formats available out-of-the-box
- Custom Token Support: Create custom tokens to log exactly what you need
- Lightweight Design: Focused functionality with minimal overhead
- Multiple Output Options: Log to console, files, or other streams
- Production-Ready Defaults: Sensible configurations for different environments
Getting Started with Morgan
Installation is simple with npm:
1npm install morgan
Basic usage within an Express application:
1246791012131416const express = require('express');const morgan = require('morgan');const app = express();// Use the predefined 'dev' formatapp.use(morgan('dev'));// Or create a custom formatapp.use(morgan(':method :url :status :res[content-length] - :response-time ms'));app.get('/', (req, res) => {res.send('Hello World');});app.listen(3000);
Enhancing Morgan with OpenTelemetry for Complete Web Application Observability
While Morgan excels at HTTP request logging, modern web applications require a more comprehensive approach to monitoring. By integrating Morgan with an OpenTelemetry-native observability solution, you can transform isolated request logs into components of a unified monitoring strategy.
This powerful combination enables:
- Request-Trace Correlation: Link HTTP requests directly to distributed traces
- End-to-End Request Visibility: Follow requests as they traverse your microservices
- Performance Insights: Connect request timing with backend process performance
- Anomaly Detection: Quickly identify unusual request patterns or performance issues
- User Journey Mapping: Track user flows across your application with correlated telemetry
Implementing Morgan with OpenTelemetry
Setting up the integration is straightforward:
123457891112141617182021252629303133const express = require('express');const morgan = require('morgan');const opentelemetry = require('@opentelemetry/api');const { NodeTracerProvider } = require('@opentelemetry/sdk-trace-node');const { ExpressInstrumentation } = require('@opentelemetry/instrumentation-express');// Initialize OpenTelemetryconst provider = new NodeTracerProvider();provider.register();// Set up Express instrumentationconst expressInstrumentation = new ExpressInstrumentation();const app = express();// Create a custom Morgan format that includes trace IDmorgan.token('trace-id', (req) => {const span = opentelemetry.trace.getSpan(opentelemetry.context.active());return span ? span.spanContext().traceId : 'unknown';});// Use Morgan with trace contextapp.use(morgan(':method :url :status :response-time ms - trace: :trace-id'));app.get('/', (req, res) => {res.send('Hello World');});app.listen(3000);
Analyzing OpenTelemetry Logs in Dash0
Logs can be directly routed into Dash0. Dash0 with OpenTelemetry provides the ability to filter, search, group, and triage within a simple user interface, with full keyboard support. Dash0 also gives full log context by showing trace context, the call and resource that created the log - including details like the Kubernetes Pod, server, and cloud environment.
Log AI also enhanced the logs with more semantical metadata and structure without any manual pattern declaration.
Conclusion
In 2025, Morgan remains the definitive HTTP request logger for Express applications, offering specialized functionality that perfectly complements the Express ecosystem. Its focused approach to request logging makes it an essential tool for web developers seeking to understand how users interact with their applications.
By enhancing Morgan with OpenTelemetry integration, developers can maintain this focused approach while gaining the benefits of unified observability. This combination delivers powerful insights by connecting HTTP request logs with traces and metrics, enabling faster problem resolution and better understanding of application behavior.
For Express applications that demand both detailed request logging and comprehensive observability, Morgan with OpenTelemetry integration provides an ideal solution that delivers actionable insights into your web application's performance and user experience.