Skip to content

Instantly share code, notes, and snippets.

@simsketch
Last active April 21, 2020 21: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 simsketch/5da4df79693c3f7148c52ebbf3c9f06b to your computer and use it in GitHub Desktop.
Save simsketch/5da4df79693c3f7148c52ebbf3c9f06b to your computer and use it in GitHub Desktop.
console.log to file and console
var fs = require('fs');
var util = require('util');
var logFile = fs.createWriteStream('log.txt', { flags: 'a' });
// Or 'w' to truncate the file every time the process starts.
var logStdout = process.stdout;
console.log = function () {
logFile.write(util.format.apply(null, arguments) + '\n');
logStdout.write(util.format.apply(null, arguments) + '\n');
}
//from https://stackoverflow.com/a/21061306/1579789
//const util = require('util') <-- this was already declared
//use this syntax if you have an array with more than 100 items that you are logging.
console.log(util.inspect(array, { maxArrayLength: null }));
//from https://zaiste.net/nodejs_log_over_100_array_items/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment