Adding logging
A web server should provide some form of data, particularly of incoming requests and their responses before it can be considered production worthy.
In this recipe, we will look at using pino
, which is a high performance JSON logger with Express. In the There's more... section we'll look at alternative loggers and integrating logging into Koa and Hapi.
Getting ready
Let's copy the express-views
folder from our previous recipe into a new folder that we'll call express-logging
, and install pino
and express-pino-logger
:
$ cp -fr adding-a-view-layer/express-views express-logging$ cd express-logging$ npm install --save pino express-pino-logger
How to do it...
We'll require pino
and express-pino-logger
at the top of the index.js
file:
const {join} = require('path') const express = require('express') const pino = require('pino')() const logger = require('express-pino-logger')({ instance: pino }) const index = require('./routes/index')
Notice how we instantiate a Pino instance by...