Saturday, June 29, 2024

JavaScript logs, in color: chalk.js

(AI answer)

node.js: process.stdout.write() vs. console.log() 

In node.js, process.stdout.write() and console.log() are both used to write to the standard output (stdout), but there are some differences between them:

Formatting: console.log() supports formatted output, similar to printf() in C. For example, you can use placeholders like %s for strings, %d for numbers, and %j for JSON. process.stdout.write() does not support this kind of formatting.

Newline character: console.log() automatically appends a newline character (\n) at the end of the output, while process.stdout.write() does not. If you want a newline at the end of the output with process.stdout.write(), you have to add it manually.

Synchronous vs Asynchronous: console.log() is synchronous on Windows and asynchronous on POSIX (Unix, Linux, macOS). process.stdout.write() is synchronous if the destination is a terminal or a file (to prevent lost data), and asynchronous if it's a pipe or a socket.

Performance: process.stdout.write() can be faster than console.log() because it doesn't involve as much overhead (like string formatting and adding a newline character).

In general, you can use console.log() when you need formatted output or when you're debugging, and process.stdout.write() when you need to write raw output or when performance is a concern.

console.log('Hello, %s!', 'world'); // Hello, world!
process.stdout.write('Hello, world!\n'); // Hello, world!

color logs in node.js console / terminal

To change colors with console.log(), you can use ANSI escape codes. These are sequences of characters that can be used to change the color of the text in the terminal. Here's an example:

console.log('\x1b[36m%s\x1b[0m', 'I am cyan');
 // Outputs "I am cyan" in cyan.

In this code, \x1b[36m is the ANSI escape code for cyan, and \x1b[0m resets the color back to the default color. The %s is a placeholder that gets replaced with the string 'I am cyan'.

Here are the ANSI color codes for 8 different colors:

Black:   \x1b[30m
Red:     \x1b[31m
Green:   \x1b[32m
Yellow:  \x1b[33m
Blue:    \x1b[34m
Magenta: \x1b[35m
Cyan:    \x1b[36m
White:   \x1b[37m

Remember to reset the color back to the default after you're done, or else the color change will affect all subsequent text that gets printed to the console. You can do this with \x1b[0m.

Note: These color codes might not work in all terminals or operating systems. If you're writing a Node.js application that needs to be portable across different systems, you might want to use a library like chalk that handles these differences for you.

const chalk = require('chalk');
console.log('Hello, world!'));

chalk - npm 311 million weekly downloads!

color logs in web browser console

Since Chrome 69, ANSI escape codes are natively supported in the developer console.

you can also use colors in console.log() in web browsers, but the method is different from Node.js. In web browsers, you can use CSS to style your console output. Here's an example:

console.log('%cHello, world!', 'color: blue; font-size: 20px');

In this code, %c is a placeholder for CSS styles. The second argument to console.log() is a string of CSS properties that will be applied to the text. In this case, the text will be blue and 20 pixels in size.

Note that not all CSS properties are supported, and the exact support may vary between different browsers. Commonly used properties like color and font-size are generally well-supported.

No comments: