I am using boiler plate code of mean.io and starting the server with command
我正在使用mean.io的锅炉板代码并使用命令启动服务器
node server.js
How do I log stdout and stderr of my express application?
如何记录我的快递申请的stdout和stderr?
Following is my server.js
以下是我的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;
3 个解决方案
#1
32
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.
process.stdout和process.stderr都是流,因此您甚至可以将流传输到它们中。有关详细信息,请参阅流上的Node.js文档。
#2
#3
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.
我能想到的唯一方法就是生成一个子进程(比如fork系统调用),然后你可以将stderr,stdout的输出“管道”到文件中。
var out = fs.openSync('./output.log', 'a')
, err = fs.openSync('./error.log', 'a');
require('child_process').spawn('./server', [], {
detatched : true,
stdio : ['ignore', out, err]
});
#1
32
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.
process.stdout和process.stderr都是流,因此您甚至可以将流传输到它们中。有关详细信息,请参阅流上的Node.js文档。
#2
5
You can do this by writing to stdout and stderr streams
您可以通过写入stdout和stderr流来完成此操作
process.stdout.write('Hello')
or
要么
process.stderr.write('Error')
Better will be to use some thirdparty logging module like winston or bunyan
更好的方法是使用像winston或bunyan这样的第三方记录模块
#3
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.
我能想到的唯一方法就是生成一个子进程(比如fork系统调用),然后你可以将stderr,stdout的输出“管道”到文件中。
var out = fs.openSync('./output.log', 'a')
, err = fs.openSync('./error.log', 'a');
require('child_process').spawn('./server', [], {
detatched : true,
stdio : ['ignore', out, err]
});