Skip to content

Instantly share code, notes, and snippets.

@xeoncross
Created November 10, 2017 22:27
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 xeoncross/7a76ec15b7da593819c1f38896c584cc to your computer and use it in GitHub Desktop.
Save xeoncross/7a76ec15b7da593819c1f38896c584cc to your computer and use it in GitHub Desktop.
Testing using a file stream for logging things
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