Redirecting stdout and stderr to a file

2,025 views
Skip to first unread message

Gabriel Farrell

unread,
Oct 21, 2011, 11:14:10 AM10/21/11
to nod...@googlegroups.com
In looking for the best way to redirect stdout and stderr I found this
old thread: https://groups.google.com/forum/#!topic/nodejs/RvrOa1Ch964/discussion

I'm wondering what's wrong with the more obvious approach:

var fs = require('fs');

console.log('console.log pre-redirect');
console.error('console.error pre-redirect');
process.stdout.write('process.stdout.write pre-redirect\n');
process.stderr.write('process.stderr.write pre-redirect\n');

var oldout = process.stdout;
var olderr = process.stderr;

process.stdout = process.stderr = fs.createWriteStream('file.log');

console.log('console.log redirected');
console.error('console.error redirected');
process.stdout.write('process.stdout.write redirected\n');
process.stderr.write('process.stderr.write redirected\n');

process.stdout = oldout;
process.stderr = olderr;

console.log('console.log un-redirected');
console.error('console.error un-redirected');
process.stdout.write('process.stdout.write un-redirected\n');
process.stderr.write('process.stderr.write un-redirected\n');

In my tests on 0.4.9 this works as expected aside from "console.error
redirected", which writes to the console instead of the file. In
0.5.10-pre (current master), none of the "redirected" statements are
redirected.

Ben Noordhuis

unread,
Oct 21, 2011, 11:33:23 AM10/21/11
to nod...@googlegroups.com

From https://github.com/joyent/node/wiki/API-changes-between-v0.4-and-v0.6

* process.stdin, process.stdout and process.stderr are getters now.

Gabriel Farrell

unread,
Oct 21, 2011, 11:58:27 AM10/21/11
to nod...@googlegroups.com

Ah. So how would one redirect them?

Koichi Kobayashi

unread,
Oct 21, 2011, 12:12:41 PM10/21/11
to nod...@googlegroups.com
Hi,

From Ben's comment (#1878).

process.__defineGetter__('stdout', function() {
return your_object;
});

I added it to wiki.


On Fri, 21 Oct 2011 11:58:27 -0400, Gabriel Farrell <gsf...@gmail.com> wrote:

> On Fri, Oct 21, 2011 at 11:33 AM, Ben Noordhuis <in...@bnoordhuis.nl> wrote:
> > On Fri, Oct 21, 2011 at 08:14, Gabriel Farrell <gsf...@gmail.com> wrote:
> >> In looking for the best way to redirect stdout and stderr I found this
> >> old thread: https://groups.google.com/forum/#!topic/nodejs/RvrOa1Ch964/discussion
> >>
> >> I'm wondering what's wrong with the more obvious approach:
> >>

> >> ? ?var fs = require('fs');
> >>
> >> ? ?console.log('console.log pre-redirect');
> >> ? ?console.error('console.error pre-redirect');
> >> ? ?process.stdout.write('process.stdout.write pre-redirect\n');
> >> ? ?process.stderr.write('process.stderr.write pre-redirect\n');
> >>
> >> ? ?var oldout = process.stdout;
> >> ? ?var olderr = process.stderr;
> >>
> >> ? ?process.stdout = process.stderr = fs.createWriteStream('file.log');
> >>
> >> ? ?console.log('console.log redirected');
> >> ? ?console.error('console.error redirected');
> >> ? ?process.stdout.write('process.stdout.write redirected\n');
> >> ? ?process.stderr.write('process.stderr.write redirected\n');
> >>
> >> ? ?process.stdout = oldout;
> >> ? ?process.stderr = olderr;
> >>
> >> ? ?console.log('console.log un-redirected');
> >> ? ?console.error('console.error un-redirected');
> >> ? ?process.stdout.write('process.stdout.write un-redirected\n');
> >> ? ?process.stderr.write('process.stderr.write un-redirected\n');


> >>
> >> In my tests on 0.4.9 this works as expected aside from "console.error
> >> redirected", which writes to the console instead of the file. In
> >> 0.5.10-pre (current master), none of the "redirected" statements are
> >> redirected.
> >
> > From https://github.com/joyent/node/wiki/API-changes-between-v0.4-and-v0.6
> >
> > * process.stdin, process.stdout and process.stderr are getters now.
>
> Ah. So how would one redirect them?
>

> --
> Job Board: http://jobs.nodejs.org/
> Posting guidelines: https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
> You received this message because you are subscribed to the Google
> Groups "nodejs" group.
> To post to this group, send email to nod...@googlegroups.com
> To unsubscribe from this group, send email to
> nodejs+un...@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/nodejs?hl=en?hl=en

Reply all
Reply to author
Forward
0 new messages