How to use pino-transport in nodejs for logs

0 votes
const pino = require('pino')
const transport = pino.transport({
  targets: [{
    level: 'info',
    target: 'pino-pretty' // must be installed separately
  }, {
    level: 'trace',
    target: 'pino/file',
    options: { destination: '/path/to/store/logs' }
  }]
})
pino(transport)
image

Why I am getting this error pino.transport is not a function?

Jun 8, 2022 in Node-js by Vaani
• 7,070 points
6,828 views

1 answer to this question.

0 votes

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

copy code to clipboard

npm init -y

copy code to clipboard

Afterwards, install the pino package from NPM using the command below:

npm install pino

copy code to clipboard

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({});

copy code to clipboard

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!');

copy code to clipboard

Once you save the file, execute the program using the following command:

node main.js

copy code to clipboard

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.

answered Jun 9, 2022 by Neha
• 9,020 points

Related Questions In Node-js

0 votes
1 answer
0 votes
1 answer

How to use the call effect in redux-saga for API requests?

To write an action creator that handles ...READ MORE

answered Mar 19 in Node-js by Tanvi