49

I am using the boilerplate code of mean.io and starting my server with the command:

node server.js

How do I log stdout and stderr of my Express application?

Here's my file server.js:

'use strict';

/**
 * Module dependencies.
 */
var mongoose = require('mongoose'),
    passport = require('passport'),
    logger = require('mean-logger');

/**
 * Main application entry file.
 * Please note that the order of loading is important.
 */

// Initializing system variables
var config = require('./server/config/config');
var db = mongoose.connect(config.db);

// Bootstrap Models, Dependencies, Routes and the app as an express app
var app = require('./server/config/system/bootstrap')(passport, db);

// Start the app by listening on <port>, optional hostname
app.listen(config.port, config.hostname);
console.log('Mean app started on port ' + config.port + ' (' + process.env.NODE_ENV + ')');

// Initializing logger
logger.init(app, passport, mongoose);

// Expose app
exports = module.exports = app;
Max
  • 1,054
  • 1
  • 12
  • 20
raju
  • 4,788
  • 15
  • 64
  • 119
  • What do you mean `log`? All output goes to STDOUT_FILENO already. – Ryan May 26 '14 at 02:40
  • possible duplicate of [How to redirect stderr and stdout to different files in the same line of bash?](http://stackoverflow.com/questions/7901517/how-to-redirect-stderr-and-stdout-to-different-files-in-the-same-line-of-bash) – Farid Nouri Neshat May 26 '14 at 03:56
  • 1
    If you want to keep your logs then I am assuming logs are somehow valuable to you. I will recommend using some higher level logging package like Winston (https://github.com/flatiron/winston) instead of console.log which writes to stdout. – sarveshseri May 26 '14 at 12:10
  • 2
    To understand more about loggin in node.js, please refer to this excellant article - http://devgigs.blogspot.in/2014/01/mastering-nodejs-logging.html – sarveshseri May 26 '14 at 12:13

3 Answers3

84

What about this?

console.log("I will goto the STDOUT");
console.error("I will goto the STDERR");

Note: both of these functions automatically add new line to your input.

If you don't want those newlines appended to your input, do this

process.stdout.write("I will goto the STDOUT")
process.stderr.write("I will goto the STDERR")

Both process.stdout and process.stderr are streams, so you can even pipe a stream into them. See Node.js docs on streams for more info.

Steel Brain
  • 4,321
  • 28
  • 38
19

You can do this by writing to stdout and stderr streams

process.stdout.write('Hello')

or

process.stderr.write('Error')

Better will be to use some thirdparty logging module like winston or bunyan

ashu
  • 1,019
  • 7
  • 9
4

The only way I can think of to do this is to spawn a child process (like the fork system call), which then you can "pipe" the output of stderr, stdout to files.

var out = fs.openSync('./output.log', 'a')
  , err = fs.openSync('./error.log', 'a');

require('child_process').spawn('./server', [], {
    detached    : true,
    stdio       : ['ignore', out, err]
});
Michael Mrozek
  • 169,610
  • 28
  • 168
  • 175
Ryan
  • 14,392
  • 8
  • 62
  • 102