NodeJS Logging - may be the best practice

2016-11-17 23:11:51 -0500

There are several approaches to pass logging strings and variables into console.log/console.error

Background:

Conclusion:

Best one:

//All useful info will be printed and being consistent
console.log('name:', $username, 'details:', $detailJSON, 'exception stacks:', $ex);

Good ones:

//This will print all useful info but we are mixing different approaches: first 2 are using placeholders %s and %j, while the last one is not
console.log('name: %s, details: %j, exception stacks:', $username, $detailJSON, $ex);

Bad ones:

//$detailJSON will be printed as [object Object]
console.log(`name: $username, details: {$detailJSON}, exception stacks: ${ex}`);
//$ex will be printed as {}
console.log('name: %s, details: %j, exception stacks: %j', $username, $detailJSON, $ex);
«Newer      Older»
Comment:
Name:

Back to home

Subscribe | Register | Login | N