node.js 将 express js 登录到输出文件?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5489815/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 13:58:59  来源:igfitidea点击:

Logging in express js to a output file?

node.jsexpress

提问by Lalith

What is best way to log my express js webserver? The inbuilt express.logger() just displays logs on screen. Can I also log them into a file in /log folder? Also the current logger automatically logs the request and responses. I need to log some application data into the log files. Can this be done using express.logger?

记录我的 express js 网络服务器的最佳方法是什么?内置的 express.logger() 只是在屏幕上显示日志。我也可以将它们登录到 /log 文件夹中的文件中吗?此外,当前记录器会自动记录请求和响应。我需要将一些应用程序数据记录到日志文件中。这可以使用 express.logger 来完成吗?

Regards, Lalith

问候, 拉利斯

采纳答案by Chad Williams

Look at the connect middleware that express extends. The express.logger() is the same as the connect.logger():

看一下express扩展的connect中间件。express.logger() 与 connect.logger() 相同:

http://expressjs.com/api.html#middleware

http://expressjs.com/api.html#middleware

http://www.senchalabs.org/connect/logger.html

http://www.senchalabs.org/connect/logger.html

The logger has a stream option that can be set where you want the output to go. By default it sends it to stdout. Also you can specify the log format you want to use.

记录器有一个流选项,可以设置您希望输出的位置。默认情况下,它将它发送到标准输出。您也可以指定要使用的日志格式。

回答by Did

To send the express or connect logs to a file use Node's writeStream. For example to send the express logs to ./myLogFile.log:

要将 express 或 connect 日志发送到文件,请使用 Node 的writeStream。例如将快速日志发送到./myLogFile.log

open the stream to your file in append mode with :

以追加模式将流打开到您的文件:

var logFile = fs.createWriteStream('./myLogFile.log', {flags: 'a'}); //use {flags: 'w'} to open in write mode

then, in your express config use :

然后,在您的快速配置中使用:

app.use(express.logger({stream: logFile}));

should also work for connect.logger.

也应该适用于 connect.logger。

回答by generalhenry

You should try winston

你应该试试温斯顿

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)(),
    new (winston.transports.File)({ filename: 'somefile.log' })
  ]
});

回答by tjholowaychuk

winston is kinda silly, multi-transport logging == tee(1), or just tail the file and transfer the data off, easy as pie

winston 有点傻,多传输日志 == tee(1),或者只是拖尾文件并传输数据,就像馅饼一样简单

回答by elwarren

Use log4js:

使用 log4js:

var log4js = require('log4js');
log4js.configure({
    appenders: [{type: 'console'},
                {type: 'file', filename: 'express.log', category: 'dev'}]
});

var logger = log4js.getLogger('dev');
logger.setLevel('DEBUG');

app.use(log4js.connectLogger(logger, {level: log4js.levels.DEBUG}));

回答by neurosnap

For HTTP request logging: https://github.com/expressjs/morgan#write-logs-to-a-file

对于 HTTP 请求日志记录:https: //github.com/expressjs/morgan#write-logs-to-a-file

var express = require('express')
var fs = require('fs')
var morgan = require('morgan')

var app = express()

// create a write stream (in append mode)
var accessLogStream = fs.createWriteStream(__dirname + '/access.log', {flags: 'a'})

// setup the logger
app.use(morgan('combined', {stream: accessLogStream}))

app.get('/', function (req, res) {
  res.send('hello, world!')
})

回答by Len Xu

you should try cluster http://learnboost.github.com/cluster/for Node. Use express to build the app, while the cluster take over the rest tasks including logging.

你应该为 Node尝试集群http://learnboost.github.com/cluster/。使用 express 构建应用程序,而集群接管包括日志记录在内的其余任务。

  1. app.use(express.logger()); // in your express apps, ex: app.js
  2. cluster.use(cluster.logger('logs')) ; // in your cluster server, ex: server.js
  1. app.use(express.logger()); // 在你的 Express 应用中,例如:app.js
  2. cluster.use(cluster.logger('logs')); // 在你的集群服务器中,例如:server.js