Logging traffic and activity with Morgan
A real web server can have a multitude of requests and events happening over a given day. Logging your traffic and activity on your web server can be very important for diagnosing unexpected behaviors after they happen. Express comes ready with a logging module called Morgan that provides all that you will need to record events in your application and even save those events to a log file.
Getting Ready
Let's enhance our logging for our normal HTTP traffic. We'll use a predefined logging format provided by Morgan to record visits to our Angular application and our API usage. We'll also create a custom event log to track admin authorized requests so that we know which admin sessions are responsible for certain requests. We can write these events to a log file as well, so we can check the history of events even between server restarts.
How to do it...
Let's follow the steps below to add custom Morgan logging to our Express web server:
- First, we'll simply...