>Huzaima

How to log all API requests in Express

Huzaima KhanMar 18, 20235 min read

Are you looking for ways to log all the API requests to your Express application? Logging API requests can be an essential tool for debugging and analyzing traffic to your server. Express is a popular web application framework for Node.js, used to build server-side web applications. When building an Express app, it's important to log all the API requests coming through it, as this helps debug, identify errors, and monitor application performance.

1. Install the morgan middleware

The morgan middleware is a popular logging middleware for Node.js, which can be used to log all the requests coming through an Express app. To install morgan, open a terminal and run the following command:

2. Set up morgan middleware

Once morgan is installed, you need to set it up as middleware in your Express app. This can be done by requiring the morgan module and calling the morgan function with a specified format. Here's an example code snippet:

In the above code snippet, we require the morgan module and set it up as middleware using the app.use() method. We pass in the 'tiny' format to the morgan function, which will log the request method, URL, status code, response time, and size of the response body.

Now that you have added the Morgan middleware to your app, you can test your logging by making a request to your app. Open your browser and navigate to http://localhost:3000 (assuming your app is running on port 3000). You should see the request logged in your console.

3. Customize the morgan format (Optional)

The morgan function accepts various formats, and you can customize the format of the logs to fit your needs. Here are some commonly used formats:

  • "combined": This format is the most commonly used and provides detailed information about the incoming request, including the HTTP method, status code, response time, IP address, user agent, and referer.
  • "common": This format is similar to the "combined" but excludes the user agent and referer information.
  • "dev": This format is ideal for development environments as it provides a concise and colorful output that includes the HTTP method, status code, response time, and URL.
  • "short": This format provides a shorter output that includes only the HTTP method, URL, and status code.
  • "tiny": This format provides the most minimalistic output and includes only the HTTP method, URL, status code, and response time in milliseconds. Example:

To use a custom format, you can pass a string or a function to the morgan function. Here's an example:

In this example, we're logging the request method, URL, status code, response time, and size of the response body, separated by spaces.

4. Write logs to a file (Optional)

By default, morgan logs to the console. However, you can also write logs to a file using the stream option. Here's an example:

In this example, we create a write stream to a file named 'access.log' in the same directory as the script. We pass the write stream to the morgan function using the stream option, which tells morgan to write logs to the file instead of the console.

5. Use a logger library (Optional)

Using a logging library instead of middleware is recommended for more advanced logging capabilities. Popular logging libraries for Node.js include winston and bunyan.

To use a logger library, you'll need to set up a logger instance and use it to log requests in your middleware or route handlers. Here's an example using Winston:

In this code, we set up a logger instance using Winston and define a custom middleware that logs requests using the logger. We use the info method to log requests at the "info" level, which is the default logging level for Winston. We also include additional metadata in the log entry, such as the response time.

Logging all the requests coming through an Express app is an important part of building and maintaining a web application. By using the morgan middleware, you can easily log all the requests and customize the format and destination of the logs to fit your needs.

Find me here:
GitHub  Twitter  LinkedIn  Instagram  Facebook

All Rights Reserved © Huzaima Khan 2023