Q:

Console.dir - Nodejs redirect to a file

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.

A:

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));
}

