当前位置: 动力学知识库 > 问答 > 编程问答 >

node.js - Logging stdout and stderr of node

问题描述:

I am using boiler plate code of mean.io and starting the server with command

node server.js

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

Following is my 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;

网友答案:

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.

网友答案:

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

网友答案:

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('./out.log', 'a')
, err = fs.openSync('./out.log', 'a');

require('child_process').spawn('./server', [], {
    detatched   : true,
    stdio       : ['ignore', out, err]
});
分享给朋友:
您可能感兴趣的文章:
随机阅读: