Skip to content

Instantly share code, notes, and snippets.

@crazy4groovy
Last active March 26, 2021 05:26
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 crazy4groovy/0def7ac2f6c8ee8d9756c15e67d25053 to your computer and use it in GitHub Desktop.
Save crazy4groovy/0def7ac2f6c8ee8d9756c15e67d25053 to your computer and use it in GitHub Desktop.
simple file logger (NodeJS)
const newFileLogger = (filename, init = false) => {
if (init) fs.writeFileSync(filename, '', 'utf8'); // wipe/reset
return (...lines) => {
fs.appendFileSync(
filename,
lines
.map(l => JSON.stringify(l, null, 2))
.join('\n') + '\n',
'utf8'
)
}
}
cont log = newFileLogger('~/log.txt', true);
...
log('Data:', [1, 2, 3, 99]);
...
log('Person:', {name: 'Bob', age: 7}, false, new Date());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment