Skip to content

Instantly share code, notes, and snippets.

@iOnline247
Created August 12, 2020 06:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save iOnline247/3df00427b313967df1b6b8f45b1db4d4 to your computer and use it in GitHub Desktop.
Save iOnline247/3df00427b313967df1b6b8f45b1db4d4 to your computer and use it in GitHub Desktop.
util.format example
// 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