Created
August 12, 2020 06:10
-
-
Save iOnline247/3df00427b313967df1b6b8f45b1db4d4 to your computer and use it in GitHub Desktop.
util.format example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://nodejs.org/api/util.html#util_util_format_format_args | |
// util.format() is a synchronous method that is intended as a debugging tool. | |
// Some input values can have a significant performance overhead that can block | |
// the event loop. Use this function with care and never in a hot code path. | |
const { format } = require("util"); | |
function log(...args) { | |
// Note the subtle difference in the output. The %j is converted to JSON. | |
const msg = format("%j %s %s %s", ...args); | |
// The output of the first argument is not JSON. | |
const msg2 = format(...args); | |
process.stdout.write(`${msg}\n`); | |
process.stdout.write(`${msg2}\n`); | |
} | |
log( | |
{ test: true, whoops: false }, | |
"test", | |
Symbol("key"), | |
Buffer.from("oh no").toString("hex"), | |
new Error("not good") | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment