node.js - 需要 Morgan & Winston 使用 btyesize 记录 api 响应

标签 node.js winston morgan

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

仅 morgan API 示例

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

现在用winston 来实现

信息:::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 像 Mac OS X ) AppleWebKit/604.1.38(KHTML,如 Gecko)版本/11.0 Mobile/15A372 Safari/604.1" {“时间戳”:“2019-07-02 04:09:50 下午”}

我将 Morgan 和 Winston 组合到我的 app.js 文件中,如下所示

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

最佳答案

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

使用 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获取更多日志标记

关于node.js - 需要 Morgan & Winston 使用 btyesize 记录 api 响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56848038/

相关文章:

node.js - 在 Passport.js 和 Express 路由器中使用 next

node.js - 当 mongodb 的好处应该是无模式时,为什么 mongoose 使用模式?

javascript - 在 node.js 中重新创建 MCRYPT_RIJNDAEL_128

node.js - Winston 没有记录任何日志级别,可能是什么错误?

javascript - 创建您自己的 Winston 记录器文件

node.js - Winston/Node.js 如何只为特定环境添加一个传输?

node.js - 将 morgan 与 logger 一起使用时,stream.write 不是函数

node.js - Node 和快速路由 - 404 错误

node.js - 在 NodeJS/Express/React 堆栈中记录错误

node.js - 在 azure 上部署 Node 应用程序时出现意外 token )?