32

I have a child process that I am using as follows in node.js. Instead of redirecting the output to the console I would like to put the output in a log file located somewhere on the machine this is running on (and should work for both windows and mac).

The code below is what I am using and I would like to output the files into a log file. What changes needed to do that here? Thanks!

My Code:

var spawn = require('child_process').spawn,
    ls    = spawn('ls', ['-lh', '/usr']);

ls.stdout.on('data', function (data) {
  console.log('stdout: ' + data);
});

ls.stderr.on('data', function (data) {
  console.log('stderr: ' + data);
});

ls.on('close', function (code) {
  console.log('child process exited with code ' + code);
});
user220755
  • 4,358
  • 16
  • 51
  • 68

5 Answers5

53

Here's an example of logging to file using streams.

var logStream = fs.createWriteStream('./logFile.log', {flags: 'a'});

var spawn = require('child_process').spawn,
    ls    = spawn('ls', ['-lh', '/usr']);

ls.stdout.pipe(logStream);
ls.stderr.pipe(logStream);

ls.on('close', function (code) {
  console.log('child process exited with code ' + code);
});
generalhenry
  • 17,227
  • 4
  • 48
  • 63
  • 3
    createWriteStream is the best thing for log files. From the docs: "Note that it is unsafe to use fs.write multiple times on the same file without waiting for the callback. For this scenario, fs.createWriteStream is strongly recommended." – BaronVonKaneHoffen Sep 12 '13 at 22:02
  • Using almost identical code, I sometimes get an exception that I am trying to write to the stream after it's closed. The docs say `pipe` will close the destination stream when the source stream emits an 'end' event. Can it be that stderr is closing just a tiny bit before stdout finishes emitting all it's data? – stav Aug 30 '16 at 05:05
6

There are two ways you can achieve this, one is using

let logConsoleStream = fs.createWriteStream('./logConsoleFile.log', {flags: 'a'});
let logErrorStream = fs.createWriteStream('./logErrorFile.log', {flags: 'a'});

and redirect all logs or errors using this

ls.stdout.pipe(logConsoleStream );  // redirect stdout/logs only
ls.stderr.pipe(logErrorStream );    // redirect error logs only

by separating log files you will have separate files for Error logs and console logs this is exactly same as generalhenry shared above

And Second Way for Achieving this with the help of Command Line

when you execute node app from the command line

node app/src/index.js

you can specify here where you want to redirect logs and Errors from this application

there are three stream redirection commands using the command line

`>`     It will redirect your only stdout or logs to the specified path
`2>`    It will redirect your errors logs to the specified path
`2>&1 ` It will redirect all your stdout and stderr to a single file

example: how you will use these commands

node app/src/index.js > ./logsOnly.txt
node app/src/index.js 2> ./ErrorsOnly.txt
node app/src/index.js 2>&1 ./consoleLogsAndErrors.txt

I hope someone coming later finds this helpful

if there is I done wrong way please do let me know it will help me and others Thanks

Hanzla Habib
  • 3,457
  • 25
  • 25
0

If you run your JS script with forever then you have the option to define a log file as parameter which will handle all your console.log messages. Not to mention the benefit of keeping your nodejs app live permanently.

Alternatively try this:

sudo forever start myapp.js 2>&1 /home/someuser/myapp/myapp.log
swogger
  • 1,079
  • 14
  • 30
  • Redirecting standard and error output will only log the verbose output of forever when you start it. You will never be able to catch the logs of your app.. – Greg Ballot Mar 04 '16 at 17:33
-1

use forever with below options

forever start -o out.log -e err.log server.js

fatih tekin
  • 959
  • 10
  • 21
-6

The best answer was in the comments and is mentioned in a previous question here: stackoverflow.com/questions/2496710/nodejs-write-to-file

It is as follows:

var fs = require('fs');
fs.writeFile("/tmp/test", "Hey there!", function(err) {
    if(err) {
        console.log(err);
    } else {
        console.log("The file was saved!");
    }
}); 
user220755
  • 4,358
  • 16
  • 51
  • 68
  • 4
    So your question wasn't as much related to `child_process` and outputting to a logfile, but more 'how do I write to a file in Node.js'? – robertklep Apr 26 '13 at 09:07
  • @robertklep yeah, sorry thought the title (Redirecting output to a log file using node.js) was clear :( – user220755 Apr 26 '13 at 18:39