node.js 中的内置访问日志(快速框架)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19681226/
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 16:07:45  来源:igfitidea点击:

Built in access logs in node.js (express framework)

node.jsloggingexpressaccess-log

提问by Ivan Longin

I was wondering if node.js(or expressframework) has any kind of built in access logging like grails has for example?

我想知道node.js(或express框架)是否有像 grails 那样的内置访问日志?

I have grails application that runs on tomcat and it automatically generates /apache-tomcat-7.0.42/logs/localhost_access_log.2013.10.30.txtfile in which are logs about request response like this one:

我有在 tomcat 上运行的 grails 应用程序,它会自动生成/apache-tomcat-7.0.42/logs/localhost_access_log.2013.10.30.txt文件,其中包含有关请求响应的日志,如下所示:

[30/Oct/2013:00:00:01 +0000] [my-ip-address] [http-bio-8080-exec-18] "GET /my-service/check HTTP/1.0" [200] [took: 1 milis]  

This logs are written automatically by system and I don't have to worry about that.

此日志由系统自动写入,我不必担心。

So what about node.js?

那么node.js呢?

Thanks for any help!

谢谢你的帮助!

Ivan

伊万

采纳答案by Ludwig Magnusson

editAs of express 4.0.0this solution is apparently no longer enough. Check out the answer from whirlwin for an updated solution.

编辑截至表达4.0.0此解决方案显然不再足够。查看 whirlwin 的答案以获取更新的解决方案。

You can use app.use(express.logger());

您可以使用 app.use(express.logger());

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

记录在这里:http: //www.senchalabs.org/connect/logger.html

回答by whirlwin

In newer versions of Express (4.0.0at the time of writing), the logger is no longer part of Express, so you have to include it as a dependency manually. It is now called morgan.

在较新版本的 Express(撰写本文时为4.0.0)中,记录器不再是 Express 的一部分,因此您必须手动将其作为依赖项包含在内。它现在被称为摩根

So, in package.json, add morgan as a dependency:

因此,在 中package.json,添加 morgan 作为依赖项:

"dependencies": {
  ...
  "morgan": "*"
  ...
}

And in your Express configuration, add:

在您的 Express 配置中,添加:

app.use(require('morgan')('dev'));

Now logging should work more or less like before. :-)

现在日志应该或多或少像以前一样工作。:-)

回答by Ivan Longin

As of now, most middleware (like logger) is no longer bundled with express and must be installed separately.

截至目前,大多数中间件(如记录器)不再与 express 捆绑在一起,必须单独安装。

Short answer: First, install morgan:

简短回答:首先,安装morgan

npm install morgan

Then use it for logging:

然后将其用于日志记录:

app = express();
var morgan  = require('morgan')
...
app.use(morgan('combined'))

Documentation is here.

文档在这里

回答by Manuel Darveau

It's not built in but really easy to configure. You can use express-winstonand add to the express middleware stack. morgandoes not let you log the request bodybut expressWinston does:

它不是内置的,但很容易配置。您可以使用express-winston并添加到 express 中间件堆栈中。morgan不允许您记录请求正文,但 expressWinston 可以:

expressWinston.requestWhitelist.push('body');
expressWinston.responseWhitelist.push('body');

Example in coffeescript:

咖啡脚本中的示例:

expressWinston.requestWhitelist.push('body')
expressWinston.responseWhitelist.push('body')
app.use(expressWinston.logger({
      transports: [
        new winston.transports.Console({
          json: true,
          colorize: true
        })
      ],
      meta: true, // optional: control whether you want to log the meta data about the request (default to true)
      msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
      expressFormat: true, // Use the default Express/morgan request formatting, with the same colors. Enabling this will override any msg and colorStatus if true. Will only output colors on transports with colorize set to true
      colorStatus: true, // Color the status code, using the Express/morgan color palette (default green, 3XX cyan, 4XX yellow, 5XX red). Will not be recognized if expressFormat is true
      ignoreRoute: function (req, res) { return false; } // optional: allows to skip some log messages based on request and/or response
    }));