【问题标题】:Need Morgan & Winston to log api response with btyesize需要 Morgan 和 Winston 使用 btyesize 记录 api 响应
【发布时间】:2019-11-12 20:10:02
【问题描述】:

我最近将 Winston 添加到我的 nodejs 后端并将它与 morgan 结合起来记录每个 API 调用。但是我错过了 bytesize 属性,它让我知道每个响应大小只使用 morgan 有多大。它可以让我更全面地了解哪个 API 获取了太多数据。任何帮助都会很棒:)

仅摩根 api 示例

GET /v1/users/notifications/5cb5ce7297fc767b4cdf667b?role=user&type=HUBFE&page_size=10&page_num=1 200 - 3252 - 25.553 ms

现在用winston实现

info: ::ffff:192.168.0.166 - - [02/Jul/2019:08:09:50 +0000] "GET /v1/searches/nearby?page_size=4&page_num=1 HTTP/1.1" 304 - "http://localhost:8100/" "Mozilla/5.0 (iPhone; CPU iPhone OS 11_0 like Mac OS X) AppleWebKit/604.1.38 (KHTML, like Gecko) Version/11.0 Mobile/15A372 Safari/604.1" {"timestamp":"2019-07-02 04:09:50 pm"}

我在我的 app.js 文件中像这样组合 morgan 和 winston

app.use(morgan('combined', {
  stream: winston.stream
}));

【问题讨论】:

    标签: node.js winston morgan


    【解决方案1】:

    注意:我们可以更改日志格式。

    使用 morgan-json 并在 json 函数中使用 :res[content-length] bytes

    const morgan = require('morgan');
    const json = require('morgan-json');
    const format = json(':method :url :status :res[content-length] bytes :response-time ms');
    app.use(morgan(format));
    

    更多日志令牌请参考https://www.npmjs.com/package/morgan#tokens

    【讨论】:

    • 很高兴知道格式关键字是bytes for morgan。恕我直言,但我将摩根与温斯顿结合起来。还能用吗?
    猜你喜欢
    • 2015-09-26
    • 2015-03-10
    • 2019-06-12
    • 2015-09-17
    • 2019-07-03
    • 2019-11-16
    • 2018-10-07
    • 1970-01-01
    • 2018-06-20
    相关资源
    最近更新 更多