Ensure that you have a recent version of Node.js and npm installed locally on your machine.
Start by creating a new directory, change into it and then initialize a Node.js project:
mkdir pino-logging && cd pino-logging
npm init -y
Afterwards, install the pino package from NPM using the command below:
npm install pino
Create a new logger.js file in the root of your project directory, and populate it with the following contents:
logger.js
const pino = require('pino');
module.exports = pino({});
This code requires the pino package and exports a pino() function that takes two optional arguments, options and destination, and returns a logger instance. We'll explore all the different ways you can customize the Pino logger, but for now let's go ahead and use the exported logger in a new main.js file as shown below:
main.js
const logger = require('./logger');
logger.info('Hello, world!');
Once you save the file, execute the program using the following command:
node main.js
You should see the following output:
Output
{"level":30,"time":1643365551573,"pid":5673,"hostname":"Kreig","msg":"Hello, world!"}
The first thing you'll notice about the output above is that it's in newline delimited JSON format (NDJSON) because Pino defaults to structured logging which is the recommended way to output logs in production contexts. You'll also notice that the log level, timestamp, hostname, and process id of the Node.js application is also included in the log entry in addition to the log message.
For further reference, follow this link.