Last active
April 21, 2020 21:26
-
-
Save simsketch/5da4df79693c3f7148c52ebbf3c9f06b to your computer and use it in GitHub Desktop.
console.log to file and console
This file contains hidden or 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
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