Last active
March 26, 2021 05:26
-
-
Save crazy4groovy/0def7ac2f6c8ee8d9756c15e67d25053 to your computer and use it in GitHub Desktop.
simple file logger (NodeJS)
This file contains 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
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