Your library newspapers
Posted: Sun Dec 22, 2024 9:40 am
Before we do that,
Let's use the tool pino-pretty to view a more readable version of the logs. In your terminal, run:
Bash
Copy the code
npm install --save-dev pino-pretty
LOG_LEVEL=debug node index.js | ./node_modules/.bin/pino-pretty
All your logs will now be passed using the | to operator command philippines mobile number example pino-pretty, and your output should be cleaned up to contain only the crucial information in color. If you send a request to http://localhost:3000 again , you should see your message as well debug.
Screenshot of beautifully printed pino newspapers
There are a variety of existing transports to beautify or transform your logs. You can even display them with emojis using pino-colada. These will come in handy when developing locally. Once you're running your server in production, you may want to move your logs to another transport , write them to disk using > for further processing, or do both using a command like tee.
The documentation also includes information on things like rotating log files, filtering, and writing logs to different files.
Your library newspapers
Now that we've seen how to write logs efficiently for our server applications, why not use the same technology for the libraries we create?
The problem is that your library may want to log some things for debugging purposes, but it really shouldn't clutter the user's application. Instead, the user should be able to enable logging if they need to debug something. Your library should be silent by default and leave the choice of writing output to the user.
- let's address the fact that the output is not really human-readable at the moment. This is intentional. pino follows a philosophy
Let's use the tool pino-pretty to view a more readable version of the logs. In your terminal, run:
Bash
Copy the code
npm install --save-dev pino-pretty
LOG_LEVEL=debug node index.js | ./node_modules/.bin/pino-pretty
All your logs will now be passed using the | to operator command philippines mobile number example pino-pretty, and your output should be cleaned up to contain only the crucial information in color. If you send a request to http://localhost:3000 again , you should see your message as well debug.
Screenshot of beautifully printed pino newspapers
There are a variety of existing transports to beautify or transform your logs. You can even display them with emojis using pino-colada. These will come in handy when developing locally. Once you're running your server in production, you may want to move your logs to another transport , write them to disk using > for further processing, or do both using a command like tee.
The documentation also includes information on things like rotating log files, filtering, and writing logs to different files.
Your library newspapers
Now that we've seen how to write logs efficiently for our server applications, why not use the same technology for the libraries we create?
The problem is that your library may want to log some things for debugging purposes, but it really shouldn't clutter the user's application. Instead, the user should be able to enable logging if they need to debug something. Your library should be silent by default and leave the choice of writing output to the user.