-1

I have a variable named, 'res'. This variable if used with console.log() print all request to a function. My question is... How to log this variable in a log.txt file?

Example using console.log(res)

Print in console:

[ 'AndroidShareANE',
  'pixi.js',
  'ShortSword',
  'faceshiftparser',
  'clmutils',
  'chaikin-smooth',
  'getuservideo',
  'getboundingbox',
  'clmdraw',
  'SpriteSheetScrubberTutorial',
  'interpolation-arrays',

This is a one part of response.

My purpose is log in log.txt file the var content. Identical to console.log() result.

Thanks for your collaboration, and sorry my bad english.

Santiago D'Antuoni
  • 164
  • 1
  • 2
  • 13

1 Answers1

2

The easiest way is to create a new Console object that writes to your file. The docs have an example of this exact thing, which I'll reproduce here for posterity:

const output = fs.createWriteStream('./stdout.log');
const errorOutput = fs.createWriteStream('./stderr.log');
// custom simple logger
const logger = new Console(output, errorOutput);
// use it like console
var count = 5;
logger.log('count: %d', count);
// in stdout.log: count 5

If you don't pass the second argument (errorOutput) to new Console then the error output will also be written to the output file.

Jordan Running
  • 102,619
  • 17
  • 182
  • 182