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,070 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,060 points

Related Questions In Node-js

0 votes
1 answer

How to use executables from a package installed locally in node_modules?

Hello @kartik, Use the npm bin command to get the ...READ MORE

answered Jul 13, 2020 in Node-js by Niroj
• 82,880 points
1,492 views
0 votes
1 answer

How to get the _id of inserted document in Mongo database in NodeJS?

Hello @kartik, A shorter way than using second ...READ MORE

answered Sep 7, 2020 in Node-js by Niroj
• 82,880 points
13,662 views
0 votes
1 answer

How to use MongoDB with promises in Node.js?

Hello @kartik, Try this: var MongoClient = require('mongodb').MongoClient var url ...READ MORE

answered Oct 12, 2020 in Node-js by Niroj
• 82,880 points
1,200 views
0 votes
1 answer

How to run app.js in nodejs?

Hello @kartik, Assuming I have node and npm properly installed on the ...READ MORE

answered Oct 13, 2020 in Node-js by Niroj
• 82,880 points
2,874 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
1,878 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,395 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,319 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,060 points
3,800 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