Using an HTTP request logger
As previously seen, writing a request logger is simple. However, writing our own could take precious time. Luckily, there are several other alternatives out there. For example, a very popular HTTP request logger widely used is morgan https://expressjs.com/en/resources/middleware/morgan.html.
morgan is a configurable middleware function that takes two arguments format
and options
which are used to specify the format in which the logs are displayed and what kind of information needs to be displayed.
There are several predefined formats:
tiny
: Minimal outputshort
: Same as tiny, including remote IP addresscommon
: Standard Apache log outputcombined
: Standard Apache combined log outputdev
: Displays the same information as the tiny format does. However, the response statuses are colored.
Getting ready
Create a new package.json
file with the following content:
{ "dependencies": { "express": "4.16.3", "morgan": "1.9.0" } }
Then, install the dependencies...