Another alternative logger is the Morgan logger, which can output logs in different formats. These can be defined using preset labels, a tokenized string or a function.
We'll use Morgan to make our Express server log messages in the common Apache log format.
Let's copy the express-views folder from our previous recipe into a new folder, which we'll call express-morgan-logging, and install morgan:
$ cp -fr adding-a-view-layer/express-views express-morgan-logging $ cd express-morgan-logging $ npm install --save morgan
Near the top of the index.js file, we can load Morgan like so:
const morgan = require('morgan')
Then around the middleware, we can register it as the first middleware like so:
app.use(morgan('common'))
We pass common to configure Morgan to output messages in common Apache log format.
Let's start our server with node index.js and make a request to http://localhost:3000 , we should see something similar to the following:
Morgan is a nice lightweight logger that can provide Apache style (and other common) log formats that may integrate well into pre-existing deployments. However, it will only work for request/response logging, custom log messages are not supported.