3

How can i redirect the console.dir(obj) output to a file instead of the console? I considered overwriting console.dir (with my custom fn.)to use file stream instead of stdout stream but the logic to walk down the object tree scares me.

Note: My node process is launched indirectly by emacs editor, so i cant use pipe to redirect any output.

  • i have said, i cant access stdout as my node process is a child process of emacs process –  Jan 07 '15 at 01:03
  • have you seen this http://stackoverflow.com/questions/8393636/node-log-in-a-file-instead-of-the-console ? – d.k Jan 07 '15 at 01:08
  • overwriting console.log is doable as what goes in as an arg, comes out... console.dir is not the same... –  Jan 07 '15 at 01:11
  • ok, then sorry. Cannot help here (( – d.k Jan 07 '15 at 01:21

1 Answers1

3

console.dir is just a wrapper around util.inspect that uses its default arguments.

console.log(util.inspect(myObj));

So you could simply do

var fs = require('fs');
function logToFile(input){
    fs.writeFile('.log', util.inspect(input));
}
laggingreflex
  • 32,948
  • 35
  • 141
  • 196