Back to Blog
Lesson 42 of the REST API Design: Design Your First Clean REST API course
API ArchitectureAugust 29, 20264 min read

Logging and Monitoring: Essential API Production Practices

Learn to implement request logging and monitoring in your REST API to track usage and errors, ensuring your production service remains healthy and debuggable.

APILoggingMonitoringProductionMiddleware
A beekeeper in a bright suit checking hives outdoors, ensuring bee health and productivity.

Previously in this course, we explored implementing links in responses to improve API discoverability. Today, we shift our focus from the client's experience to the developer's visibility: how to keep an eye on your API when it’s running in the wild.

In production, your API is a black box. Without proper observability, you are effectively flying blind; you won't know if a user is struggling with a 500-level error until they email you to complain. To move toward professional-grade backend development, we must implement two pillars of reliability: Logging and Monitoring.

The Fundamentals of Production Observability

Logging is the practice of recording events—such as incoming requests, authentication attempts, or database errors—to a persistent store (like a file or a cloud service). Monitoring, by contrast, is the continuous process of observing these logs and system metrics to ensure the service remains operational and performant.

When building a REST API, these two concepts serve different but complementary roles:

  • Logging: Provides the "why" and "what" when something goes wrong. It captures the context of specific requests.
  • Monitoring: Provides the "when" and "how often." It alerts you if your error rate spikes or if your response times exceed acceptable thresholds.

For a deeper dive into the theory of observability, I recommend reading about error handling and logging patterns for production systems to understand how to turn failures into actionable data.

Implementing Request Logging

Stack of cut logs with blue markings in autumn forest, showcasing deforestation and natural resources.

In a Node.js/Express environment, the most efficient way to implement request logging is via middleware. Middleware intercepts every incoming request, allowing you to extract details like the HTTP method, the URL, the response status, and the time taken to process the request.

While you can write a custom logger, using a standard middleware like morgan is the industry-standard approach for simple, effective logging.

Worked Example: Basic Request Logging

First, install the library: npm install morgan. Then, integrate it into your main server file:

JAVASCRIPT
const express = require(CE9178">'express');
const morgan = require(CE9178">'morgan');
const app = express();

// Use the CE9178">'combined' format for Apache-style logs
// This includes IP, timestamp, method, URL, status, and user-agent
app.use(morgan(CE9178">'combined'));

app.get(CE9178">'/v1/tasks', (req, res) => {
  res.status(200).json({ data: [] });
});

app.listen(3000, () => console.log(CE9178">'API running on port 3000'));

With morgan('combined'), every request will output a line to your terminal looking something like this: ::1 - - [10/Oct/2023:10:00:00 +0000] "GET /v1/tasks HTTP/1.1" 200 15 "-" "Mozilla/5.0..."

For more advanced needs, check out how to add logging and monitor API activity with Morgan middleware to customize your output format for specific production requirements.

Monitoring API Status: The Health Check

A "Health Check" is an endpoint that lets your infrastructure (like a load balancer or a monitoring tool) know that your API is alive. It should be lightweight and return a simple status.

Add this to your project to ensure you can verify service status at any time:

JAVASCRIPT
app.get(CE9178">'/health', (req, res) => {
  res.status(200).json({
    status: CE9178">'UP',
    timestamp: new Date().toISOString(),
    uptime: process.uptime()
  });
});

This endpoint is your first line of defense. If your monitoring system sees a 503 or a timeout from /health, it knows to automatically restart your container or alert you immediately.

Practice Exercise

  1. Add the morgan middleware to your current project.
  2. Create the /health endpoint shown above.
  3. Start your server and perform a GET request to both your tasks endpoint and the new /health endpoint.
  4. Verify that the logs appear in your console for both requests.

Common Pitfalls

  • Logging Sensitive Data: Never log passwords, PII (Personally Identifiable Information), or authorization headers. Scrub these values before they hit your logs.
  • Over-logging: Don't log the entire request body for every single request in production. It creates massive, expensive log files and can slow down your API.
  • Ignoring Monitoring: A log file that no one reads is useless. Ensure that your logs are being shipped to a location (like CloudWatch, Datadog, or an ELK stack) where you can actually search them.

FAQ

Q: Should I log to a file or standard output? In modern cloud environments, you should log to stdout (console). The container orchestrator (like Kubernetes or Docker) will capture that stream and forward it to your logging platform.

Q: How often should I monitor the health check? A typical production setup checks the health status every 10–30 seconds.

Recap

We have moved beyond simple route handling to address operational visibility. By implementing request logging with middleware and creating a dedicated /health endpoint, you have established the basic infrastructure needed to debug production issues effectively.

Up next: We will dive into Caching Strategies to optimize your API's performance and reduce database load.

Similar Posts