Some libraries like chalk

Sharing knowledge to enhance japan database performance and growth.
Post Reply
poxoja9630
Posts: 8
Joined: Sun Dec 22, 2024 5:30 am

Some libraries like chalk

Post by poxoja9630 »

Copy the code
DEBUG=mylib:randomid node -r pino-debug index.js | ./node_modules/.bin/pino-colada
Your library debug logs now display in the same format as your application logs.

Screenshot Debug logs working with pino and pino-colada
Exiting your command line interface
The last case we'll cover in this post is the somewhat special case of logging CLIs rather than libraries. My philosophy here is to keep logical logs separate from CLI output "logs". For all logical logs, you should use a library such as debug. That way, you or others can reuse the logic without being tied to the specific use case of your CLI.

When you build a command line interface with Node.js , you'll philippines mobile number example probably want to spruce things up by adding colors, arrow buttons, or formatting them in a way that makes them visually appealing. However, when building your command line interface, there are a few scenarios you should keep in mind.

First, your CLI may be used in the context of a continuous integration (CI) system, in which case you'll want to ditch the colors or other fancy stuff in the way the output looks. Some CI systems set an environment flag called ci. To check if you're in the context of a CI, the best thing to do is to use a package like is-ci, which already supports a bunch of CI systems.

Image

Some libraries like chalk already detect ICs for you and get rid of colors for you. Let's see what that looks like.

Install chalk using npm install chalk and create a file named cli.js. Put the following in it:

JavaScript

Copy the code
const chalk = require('chalk');

console.log('%s Hi there', chalk.cyan('INFO'));
If you run this script using node cli.js, you will see colored output.

Screenshot CLI output in color
But if you run it with CI=true node cli.js, you will see that the color is removed:

Screenshot CLI output without colors and IC mode enabled
The other scenario to keep in mind is if your stdout is running in terminal mode, i.e. the content is written to a terminal. If so, we can show the beautified output using tools like boxen. If not, it is likely that the output is being redirected to a file or passed somewhere.

You can check whether stdin, stdout or stderr are running in terminal mode by checking the attribute isTTY on the corresponding stream. For example: process.stdout.isTTY. TTY stands for "teletypewriter" and in this case specifically refers to the terminal.

The values ​​may differ for each of the three streams depending on how the Node.js process was started. You can read more about this in the "Process I/O" section of the Node.js documentation .

Let's see how the value of process.stdout.isTTY varies depending on the situation. Update your file cli.js to check it:

JavaScript

Copy the code
const chalk = require('chalk');

console.log(process.stdout.isTTY);
console.log('%s Hi there', chalk.cyan('INFO'));
Now run node cli.js in your terminal and you will see true it displayed, followed by our colored message.

Screenshot Output showing "true" and output in color
Then perform the same operation, but redirecting the output to a file and examining the contents afterwards, by running:
Post Reply