user220755
user220755

Reputation: 4446

Redirecting output to a log file using node.js

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

Upvotes: 34

Views: 40621

Answers (5)

Hanzla Habib
Hanzla Habib

Reputation: 3703

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

Upvotes: 7

fatih tekin
fatih tekin

Reputation: 989

use forever with below options

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

Upvotes: -1

swogger
swogger

Reputation: 1119

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

Upvotes: 0

user220755
user220755

Reputation: 4446

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

Upvotes: -7

generalhenry
generalhenry

Reputation: 17319

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

Upvotes: 56

Related Questions