【问题标题】:Sails.js HOWTO: implement logging for HTTP requestsSails.js HOWTO:实现 HTTP 请求的日志记录
【发布时间】:2014-03-09 05:55:06
【问题描述】:

Sails.js 的默认日志记录很差,没有显示 http 请求日志(即使是详细的)。将http请求记录到控制台的最佳方法是什么,以便我可以查看是否收到格式错误的请求? Expressjs 的默认日志记录就足够了。

我更喜欢 Sails.js 配置方式,而不是更改源代码方法。

有没有人有这方面的经验。我的谷歌搜索似乎奇怪地缺乏信息。

在 Mac OSX 上运行 Sails v0.9.8。

【问题讨论】:

    标签: sails.js


    【解决方案1】:

    没有用于记录每个请求的 Sails 配置选项,但您可以在 config/routes.js 的顶部添加一个快速记录路由,这应该可以解决问题:

    // config/routes.js
    '/*': function(req, res, next) {sails.log.verbose(req.method, req.url); next();}
    

    【讨论】:

    • 如果您使用的是 Sails v.011,那么一个配置选项。见Eagle's answer
    • 不能反驳,考虑到我写了那个代码 :) 事实上,如果你想记录 所有 HTTP 请求(包括静态资产),使用中间件是更好的方法。不过,它不会记录通过套接字发出的请求。
    【解决方案2】:

    也许为时已晚,但对于未来的参考,我使用的是 Sails 0.11,您可以在中间件的 config/http.js 文件中配置它

    添加这个功能(其实就是作为例子来的)

    // Logs each request to the console
    requestLogger: function (req, res, next) {
        console.log("Requested :: ", req.method, req.url);
        return next();
    },
    

    并在order var: 上设置它:

    order: [
      'startRequestTimer',
      'cookieParser',
      'session',
      'requestLogger',  // Just here
      'bodyParser',
      'handleBodyParserError',
      'compress',
      'methodOverride',
      'poweredBy',
      '$custom',
      'router',
      'www',
      'favicon',
      '404',
      '500'
    ]
    

    【讨论】:

    • 但是我找不到在中间件中使用我的风帆记录器的方法。
    • 你能更好地解释这个问题吗?对我来说,它工作得很好
    • 这个解决方案可以正常工作,如果它足以使用 console.log 输出日志。但是,如果我想使用sails.log.* 输出日志信息,那么在中间件中无法访问sails,并且在我漂亮的sails 日志条目之间存在未格式化的console.log 条目。我可以想象我可以手动加载记录器,但这很不方便。
    • 如何修改 requestLogger 函数以输出到文件而不是 consol?
    【解决方案3】:

    我 fork sails-hook-requestlogger 模块将所有请求日志(访问日志)写入文件。

    sails-hook-requestlogger-file

    您只需npm install sails-hook-requestlogger-file 就可以开始了!

    用法

    只需像往常一样启动您的应用程序,您的所有服务器请求都会被记录,其中包含响应时间等有用信息,直接发送到您的控制台。默认情况下,它在您的开发环境中激活,但在生产环境中停用。

    配置

    默认情况下,配置位于sails.config.requestloggerfile 中 您可以创建 config/requestlogger.js 并覆盖这些默认值:

    Parameter        Type        Details
    format           ((string))  Defines which logging format to use. Deaults to dev.
    logLocation      ((string))  Defines where to log: console or file. Defaults to console.
    fileLocation     ((string))  Location of file relative to project root (if file is specified in logLocation. This has no effect if console is specified in logLocation.
    inDevelopment    ((boolean)) Whether or not to log requests in development environment. Defaults to true.
    inProduction     ((boolean)) Whether or not to log requests in production environment Defaults to false.
    

    示例config/requestlogger.js文件:

    module.exports.requestloggerfile = {
     //see: https://github.com/expressjs/morgan#predefined-formats for more formats
      format: ':remote-addr - [:date[clf]] ":method :url" :status :response-time ms ":user-agent"',
      logLocation: 'file',
      fileLocation: '/var/log/myapp/access.log',
      inDevelopment: true,
      inProduction: true
    };
    

    希望对某人有所帮助:)

    【讨论】:

      【解决方案4】:

      我发现这符合我的需求 - 它使用 Morgan 模块进行 Express 并为您连接所有内容:https://www.npmjs.com/package/sails-hook-requestlogger

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-09-12
        • 2014-06-13
        • 1970-01-01
        • 1970-01-01
        • 2017-08-07
        • 2022-01-08
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多