0
0
Expressframework~15 mins

Morgan for HTTP request logging in Express - Deep Dive

Choose your learning style9 modes available
Overview - Morgan for HTTP request logging
What is it?
Morgan is a tool used in Express applications to automatically record details about every HTTP request your server receives. It helps you see who is visiting your site, what they are asking for, and how your server responds. Morgan writes this information in a clear format to your console or a file, making it easier to track and debug your app.
Why it matters
Without Morgan or similar logging tools, developers would have to manually write code to track every request, which is tedious and error-prone. Without request logs, it is hard to find bugs, understand user behavior, or monitor server health. Morgan solves this by providing a simple, consistent way to see all requests in real time, helping keep your app reliable and secure.
Where it fits
Before learning Morgan, you should understand basic Express server setup and middleware concepts. After Morgan, you can explore more advanced logging tools, error tracking, and monitoring services that build on request logs to improve app maintenance and performance.
Mental Model
Core Idea
Morgan is like a smart receptionist who notes down every visitor’s details and what they asked for, so you always know what’s happening at your server’s front door.
Think of it like...
Imagine a hotel lobby where a receptionist writes down every guest’s name, arrival time, and purpose of visit. This log helps the hotel manager track guests and spot any issues quickly. Morgan does the same for your web server’s visitors.
┌───────────────┐
│ HTTP Request  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│   Morgan      │  <-- Logs request details (method, URL, status, time)
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Express App   │  <-- Handles request
└───────────────┘
Build-Up - 6 Steps
1
FoundationWhat is Morgan and why use it
🤔
Concept: Introducing Morgan as a middleware that logs HTTP requests automatically.
Morgan is a middleware for Express that logs details about each HTTP request your server receives. Middleware means it runs between receiving a request and sending a response. Morgan saves you from writing manual logging code by printing request info like method (GET, POST), URL, response status, and response time.
Result
When you add Morgan to your Express app, every request you make to the server is logged in the console automatically.
Understanding Morgan as middleware helps you see how it fits naturally into Express’s request-response flow without extra effort.
2
FoundationInstalling and basic setup
🤔
Concept: How to add Morgan to an Express project and enable simple logging.
First, install Morgan with npm: npm install morgan. Then, in your Express app file, import Morgan and add it as middleware: const morgan = require('morgan'); app.use(morgan('dev')); The 'dev' format shows concise colored logs with method, URL, status, and response time.
Result
Your server console now shows logs like: GET / 200 12ms every time you visit a route.
Knowing how to quickly set up Morgan lets you start seeing request logs immediately, improving your debugging and monitoring.
3
IntermediateUnderstanding Morgan formats
🤔Before reading on: do you think Morgan logs can be customized or are they fixed? Commit to your answer.
Concept: Morgan supports different log formats to show more or less detail, or custom info.
Morgan has built-in formats like 'combined' (detailed logs with user agent and referrer), 'common' (standard Apache style), and 'tiny' (minimal info). You can also create your own format string or function to log exactly what you want. For example, app.use(morgan('combined')) logs more info than 'dev'.
Result
You can tailor logs to your needs, from simple to detailed, helping with different debugging or monitoring tasks.
Understanding formats lets you balance between too much and too little info, making logs more useful and less noisy.
4
IntermediateLogging to files instead of console
🤔Before reading on: do you think Morgan can save logs to a file directly or only print to console? Commit to your answer.
Concept: Morgan can write logs to files using Node.js streams, not just the console.
To save logs, create a writable stream with Node's fs module: const fs = require('fs'); const path = require('path'); const accessLogStream = fs.createWriteStream(path.join(__dirname, 'access.log'), { flags: 'a' }); Then pass it to Morgan: app.use(morgan('combined', { stream: accessLogStream })); This saves logs to 'access.log' for later review or analysis.
Result
Your server logs are stored persistently, helping with audits, debugging after crashes, or analyzing traffic over time.
Knowing how to log to files is key for production apps where console logs are not enough or get lost.
5
AdvancedCustom tokens for tailored logging
🤔Before reading on: do you think Morgan lets you add your own data fields to logs? Commit to your answer.
Concept: Morgan allows defining custom tokens to include extra info in logs, like user IDs or request headers.
You can define a token with morgan.token('name', function(req) { return req.headers['x-user-id'] || '-'; }); Then use it in a format string: app.use(morgan(':method :url :status :res[content-length] - :response-time ms :name')); This logs the user ID from headers if present.
Result
Logs become richer and more relevant to your app’s needs, helping track users or special request data.
Custom tokens unlock powerful, app-specific logging without changing core Morgan code.
6
ExpertPerformance impact and best practices
🤔Before reading on: do you think logging every request always has no cost? Commit to your answer.
Concept: Logging adds overhead; understanding how to minimize impact is crucial for high-performance apps.
Morgan processes every request, which can slow down your server if logs are too detailed or synchronous. Use asynchronous file streams, limit log detail in production, or disable logging for static assets. Also, combine Morgan with log rotation tools to avoid huge log files. Profiling your app with and without Morgan shows the real cost.
Result
You maintain fast, responsive servers while still getting valuable logs.
Knowing the tradeoffs of logging helps you design systems that balance observability and performance.
Under the Hood
Morgan works by hooking into Express’s middleware chain. When a request arrives, Morgan captures request details like method and URL immediately. It waits until the response finishes to get status and response time. It then formats this data into a string using predefined or custom formats and outputs it to the console or a stream. Internally, it uses Node.js streams for efficient writing and event listeners to detect response completion.
Why designed this way?
Morgan was designed to be simple, fast, and flexible. Using middleware fits naturally into Express’s architecture. Waiting for response end ensures accurate status and timing. Streams allow efficient logging without blocking the server. Alternatives like manual logging were error-prone and repetitive, so Morgan automates this with minimal developer effort.
┌───────────────┐
│ Incoming Req  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Morgan Middleware │
│  - Capture req info│
│  - Wait response end│
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Express Handler│
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Response Ends  │
└──────┬────────┘
       │
       ▼
┌───────────────┐
│ Morgan Logs   │
│  - Format log │
│  - Write to   │
│    console or │
│    file stream│
└───────────────┘
Myth Busters - 4 Common Misconceptions
Quick: Does Morgan log request bodies by default? Commit to yes or no.
Common Belief:Morgan logs everything about the request, including the body content.
Tap to reveal reality
Reality:Morgan does not log request bodies by default; it mainly logs method, URL, status, and timing. Logging bodies requires custom tokens or separate middleware.
Why it matters:Assuming bodies are logged can lead to missing critical data during debugging or security audits.
Quick: Is Morgan only for development environments? Commit to yes or no.
Common Belief:Morgan is just a development tool and should not be used in production.
Tap to reveal reality
Reality:Morgan is often used in production with proper configuration, like logging to files and limiting verbosity.
Why it matters:Avoiding Morgan in production can leave you blind to real user issues and server problems.
Quick: Does Morgan slow down your server significantly? Commit to yes or no.
Common Belief:Morgan logging always causes noticeable performance problems.
Tap to reveal reality
Reality:Morgan adds some overhead but is designed to be lightweight; performance impact is minimal if used properly.
Why it matters:Overestimating impact may cause developers to skip logging, losing valuable insights.
Quick: Can Morgan log custom data like user IDs without extra setup? Commit to yes or no.
Common Belief:Morgan automatically logs all request details including custom app data.
Tap to reveal reality
Reality:Morgan only logs standard HTTP info unless you define custom tokens to include extra data.
Why it matters:Not knowing this can cause confusion when expected data is missing from logs.
Expert Zone
1
Morgan’s logging happens asynchronously but still can block event loop if writing to slow streams; understanding Node.js streams is key to optimizing performance.
2
Custom tokens can access any part of the request or response, enabling integration with authentication or session data for richer logs.
3
Morgan’s middleware order matters; placing it before or after other middleware affects what it can log, especially for error handling or body parsing.
When NOT to use
Morgan is not ideal when you need structured logs in JSON for centralized logging systems; in such cases, use dedicated logging libraries like Winston or Pino that support structured output and advanced features.
Production Patterns
In production, Morgan is often combined with log rotation tools to manage file size, integrated with monitoring systems by piping logs, and configured to exclude static asset requests to reduce noise.
Connections
Middleware pattern
Morgan is an example of middleware in Express.
Understanding Morgan deepens your grasp of middleware as a way to insert reusable logic into request handling.
Logging in distributed systems
Morgan provides local request logs, which are a foundation for distributed tracing and centralized logging.
Knowing Morgan helps appreciate the challenges of collecting and correlating logs across multiple services.
Hotel front desk logging
Both Morgan and hotel receptionists record visitor details to track activity and spot issues.
Recognizing this pattern across domains shows how logging is a universal tool for monitoring and control.
Common Pitfalls
#1Logging request bodies directly with Morgan without custom tokens.
Wrong approach:app.use(morgan('dev')); // expects body logged automatically
Correct approach:morgan.token('body', (req) => JSON.stringify(req.body)); app.use(morgan(':method :url :status :body'));
Root cause:Misunderstanding that Morgan logs only standard HTTP info unless extended.
#2Logging to console in production causing performance issues and lost logs.
Wrong approach:app.use(morgan('combined')); // logs only to console in production
Correct approach:const fs = require('fs'); const accessLogStream = fs.createWriteStream('access.log', { flags: 'a' }); app.use(morgan('combined', { stream: accessLogStream }));
Root cause:Not redirecting logs to persistent storage in production.
#3Placing Morgan middleware after routes, so requests are not logged.
Wrong approach:app.get('/', (req, res) => res.send('Hello')); app.use(morgan('dev'));
Correct approach:app.use(morgan('dev')); app.get('/', (req, res) => res.send('Hello'));
Root cause:Middleware order misunderstanding in Express.
Key Takeaways
Morgan is a middleware tool that automatically logs HTTP requests in Express apps, saving manual effort.
It supports multiple log formats and can be customized with tokens to include app-specific data.
Morgan can write logs to console or files, which is essential for production monitoring and debugging.
Proper setup and middleware ordering are crucial to ensure accurate and efficient logging.
Understanding Morgan’s design and limitations helps balance logging detail with app performance.