I have a problem I need to get all console.log outputs is it possible to dump all console.log outputs to a one variable? I use node.js. A little bit of context: I have an output of mocha reporter and I want to have all output to format it and send it via email
Asked
Active
Viewed 338 times
-1
-
Probably possible, but pretty weird, can you give a bit more context? – CertainPerformance Nov 30 '18 at 11:50
-
Better write them in a log file? – Aefits Nov 30 '18 at 11:51
-
I add a context – Nikita Seliverstov Nov 30 '18 at 11:53
-
Possibly dupe of https://stackoverflow.com/questions/8393636/node-log-in-a-file-instead-of-the-console – mchl18 Nov 30 '18 at 11:57
-
I don't want to create log file – Nikita Seliverstov Nov 30 '18 at 12:05
-
You shouldn't even use `console.log` and now you actually want to use it to collect log information in order to send it via email? What on earth are you working on? – Stefan Falk Nov 30 '18 at 14:35
-
I need it because i need to send the html optimized output of mocha reporter and we don't use any CI. I don't really see better solution only for example made my custom reporter but it's much more work – Nikita Seliverstov Dec 11 '18 at 10:11
2 Answers
1
You can use a kind of hack where you substitute console.log with another function following a kind of decorator pattern:
var logs = ''; // logs needs to be defined for the += operator
const tmp = console.log;
console.log = function(...args){
logs += arg + ' '
tmp(...args)
}
Obviously, using a global variable is not great but you can improve from there...

godot
- 1,550
- 16
- 33
-
-
It's strange but now i have output like this: `%d passing (%ss) %d failing undefined %s) %s: %s%s %s` – Nikita Seliverstov Nov 30 '18 at 12:21
-
Your message is a bit cryptic... What output are you expecting? Regarding the `undefined`, it's normal. I don't know if you noticed but I had changed my code to remove the problem. – godot Nov 30 '18 at 12:25
-
normally it look like this `2 passing (24.20s) 1 failing` the %d and %s it think needed to replace it with data but it break only if redefine `console.log` – Nikita Seliverstov Nov 30 '18 at 12:30
-
1Ok it's because your logger is using string substitution... See if my correction is getting you closer to where you want (you'll have to implement a string sub yourself in order to finish the job). – godot Nov 30 '18 at 12:36
-
I think I'm very near now the normal console output doesn't breaks but if I try to output logs, even in normal output string substitution breaks. Even if try to output like this `let final = logs; console.log(final);'. (didn't saw your edit will try to implement by my self) – Nikita Seliverstov Nov 30 '18 at 12:52
1
Based on the answer of @godot:
var logs = '' // logs needs to be defined for the += operator
const tmp = console.log
console.log = function(...args) {
// string substitution, console.log style
const formatArg = util.format(...args)
logs += formatArg + '\n'
tmp(...args)
}
// To test it :
console.log('test1')
// use console.log with the "printf" style
console.log('test%d', 2)
// same
console.log('%s%d', 'test', 3)
// now show all the logs that were collected in the global var logs variable
console.log('logs: ', logs) // if you don't like globals, u can also use put it in an attribute of the console object...
-
1Sure, I only added the `util.format` function which implement the "string substitution" that console.log is using. Look [here](https://millermedeiros.github.io/mdoc/examples/node_api/doc/util.html#util.format) for the explanation but if you know C, it is very similar to the famous `printf` function. – GeorgeC Nov 30 '18 at 17:46
-
The [rest](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/rest_parameters) operator (`...`) in the parameters of the function *definition*) and the [spread](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Spread_syntax) operator (`...`) in the argument of a function *call*) is allowing you to respectively gather together and spread all the arguments of a function into/from one variable (an array to be precise). Here as we don't know the number of arguments of the console.log function we are trying to reproduce, it is super convenient! – GeorgeC Nov 30 '18 at 17:51
-
I meant: edit the answer and explain your code, code only answers are flagged as low quality answers. – DarkMukke Dec 01 '18 at 21:04