Sitemap

Better NodeJS logging with pino logger

2 min readDec 30, 2021

--

Press enter or click to view image in full size
Photo by Joshua Sortino on Unsplash

In this article we will go through how to improve your logging in a nodejs application with the pino node library.

We’ll assume you have a node project created with the following npm package.json scripts:

"scripts": {
"start": "node index.js"
}

We’ll install 2 packages, pino and pino-pretty. pino-pretty is a utility package which translates the raw output of pino into a readable format.

npm install pino pino-pretty --save

Next we’ll create a new file called logger.js which will export a configured pino logger object to be used throughout the app.

// logger.js

const pino = require('pino');
const log = pino({
level: process.env.LOG_LEVEL || 'debug'
});
module.exports = log;

What we’ve done above is create a pino logger object and configured it with a logger level passed in through an environment variable or use the “debug” level by default.

Now let’s log some stuff.

// index.js

const log = require('./logger');

log.debug('First message logged with pino logger');

log.info('Informational message');

log.warn('A warning message');

log.error('Something went wrong');

const customObject = {
username: 'user1'
};
log.debug({customObject: customObject}, 'Debug message with custom objects');

Run the node application with npm run start and you should see some output

{"level":20,"time":1640903971918,"pid":29491,"hostname":"M.local","msg":"First message logged with pino logger"}
{"level":30,"time":1640903971918,"pid":29491,"hostname":"M.local","msg":"Informational message"}
{"level":40,"time":1640903971918,"pid":29491,"hostname":"M.local","msg":"A warning message"}
{"level":50,"time":1640903971918,"pid":29491,"hostname":"M.local","msg":"Something went wrong"}
{"level":20,"time":1640903971918,"pid":29491,"hostname":"M.local","customObject":{"username":"user1"},"msg":"Debug message with custom objects"}

The above output might be fine to store in a file but this is not very readable, let’s fix that.

Get Max Antonov’s stories in your inbox

Join Medium for free to get updates from this writer.

We’ll do this by piping the raw output through the pino-pretty package. Let’s update our package.json script to use the pino logger.

"scripts": {
"start": "node index.js | ./node_modules/.bin/pino-pretty --translateTime"
}

The --translateTime flag gives us a formatted timestamp in our log output.

Run the application again and you should see a more readable output

[2021-12-30 22:43:10.873 +0000] DEBUG (29502 on M.local): First message logged with pino logger
[2021-12-30 22:43:10.874 +0000] INFO (29502 on M.local): Informational message
[2021-12-30 22:43:10.874 +0000] WARN (29502 on M.local): A warning message
[2021-12-30 22:43:10.874 +0000] ERROR (29502 on M.local): Something went wrong
[2021-12-30 22:43:10.874 +0000] DEBUG (29502 on M.local): Debug message with custom objects
customObject: {
"username": "user1"
}

The last thing you might want is to have different log levels depending if you run the application in development or production mode. We can do with 2 scripts in our package.json

"scripts": {
"start": "node index.js | ./node_modules/.bin/pino-pretty --translateTime",
"prod": "LOG_LEVEL=info node index.js | ./node_modules/.bin/pino-pretty --translateTime"
}

With the above in place you can run the application with:

  • npm run start in development (with a default log level of “debug”, everything will be logged)
  • npm run prod in production

Happy logging!

For more details on the pino library documentation: https://getpino.io/

--

--