【问题标题】:Logging stdout and stderr of Node记录Node的stdout和stderr
【发布时间】:2014-07-14 17:49:45
【问题描述】:

我正在使用mean.io 的样板代码并使用以下命令启动我的服务器:

node server.js

如何记录我的 Express 应用程序的 stdoutstderr

这是我的文件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;

【问题讨论】:

标签: node.js express mean-stack


【解决方案1】:

这个怎么样?

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

注意:这两个函数都会自动在您的输入中添加新行。

如果您不希望将这些换行符附加到您的输入中,请执行此操作

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

process.stdoutprocess.stderr 都是流,因此您甚至可以通过管道将流导入它们。请参阅Node.js docs on streams 了解更多信息。

【讨论】:

    【解决方案2】:

    您可以通过写入 stdout 和 stderr 流来做到这一点

    process.stdout.write('Hello')
    

    process.stderr.write('Error')
    

    最好使用一些第三方日志记录模块,例如 winstonbunyan

    【讨论】:

      【解决方案3】:

      我能想到的唯一方法是生成一个child process(如 fork 系统调用),然后您可以将stderrstdout 的输出“管道”到文件。

      var out = fs.openSync('./output.log', 'a')
        , err = fs.openSync('./error.log', 'a');
      
      require('child_process').spawn('./server', [], {
          detached    : true,
          stdio       : ['ignore', out, err]
      });
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-29
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2013-06-09
        • 2015-12-15
        • 2022-10-15
        相关资源
        最近更新 更多