34

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

5 Answers 5

56

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);
});
Sign up to request clarification or add additional context in comments.

4 Comments

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."
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?
Apparently, the output order of STDERR and STDOUT written in the file is fairly random. This makes it impossible to use for anything adequate. I believe it's due to asynchronous trait of Node. Is it possible to force the order?
Late reply, but you'll want to lock the area in code that writes to the logs (node semaphore package works well). Unfortunately, that doesn't scale well, so a better option would be to ship the logs somewhere they will (or can) be properly ordered.
8

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

Comments

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

1 Comment

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..
-1

use forever with below options

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

Comments

-7

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

2 Comments

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 yeah, sorry thought the title (Redirecting output to a log file using node.js) was clear :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.