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
5,639 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 6 days ago in Node-js by Tanvi
38 views
0 votes
1 answer

How to use middleware for logging actions and state changes in Redux?

To use middleware for logging actions and ...READ MORE

answered 4 days ago in Node-js by Anvi
47 views
0 votes
0 answers

How to use generator functions in redux-saga for retry logic?

With the help of code can you ...READ MORE

6 days ago in Node-js by Ashutosh
• 22,830 points
23 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
2,062 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,596 views
+1 vote
1 answer

Protocols used in a distributed/dlt system for the nodes to establish communication

yes all are over TCP/IP connections secured ...READ MORE

answered Aug 6, 2018 in Blockchain by aryya
• 7,460 points
1,680 views
0 votes
1 answer

How to schedule a google meet and get the meet link in NodeJs?

To create a Google Meet, you'll need ...READ MORE

answered May 27, 2022 in Node-js by Neha
• 9,020 points
4,117 views
0 votes
1 answer
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP