Created
November 10, 2017 22:27
-
-
Save xeoncross/7a76ec15b7da593819c1f38896c584cc to your computer and use it in GitHub Desktop.
Testing using a file stream for logging things
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 fs = require('fs'); | |
function createLogger() { | |
// const log_file = fs.createWriteStream(`${__dirname}/debug.log`, { flags: 'w' }); | |
const logStream = fs.createWriteStream('log.txt', { flags: 'a' }); | |
// logStream.log('Initial line...'); | |
// logStream.end('this is the end line'); | |
function formatDate(d) { | |
return d | |
.toISOString() | |
.substring(0, 19) | |
.replace('T', ' '); | |
} | |
const log = (message) => { | |
const d = new Date(); | |
logStream.write(`${formatDate(d)} ${message}\n`); | |
} | |
return { | |
log, | |
end: logStream.end | |
} | |
} | |
const a = createLogger(); | |
a.log('a.1'); | |
const b = createLogger(); | |
b.log('b.1'); | |
for (let i = 0; i < 10; i++) { | |
const x = new Promise((resolve) => { | |
a.log(`a.${i}`); | |
b.log(`b.${i}`); | |
resolve(); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment