-1

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

2 Answers2

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
  • I will try and let you know – Nikita Seliverstov Nov 30 '18 at 12:08
  • 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
  • 1
    Ok 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...
godot
  • 1,550
  • 16
  • 33
GeorgeC
  • 36
  • 4
  • 1
    Sure, 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