Nodejs:express-winston 日志记录问题

时间:2021-03-26 07:31:29

标签: node.js express amazon-dynamodb winston

我使用 express-winstonwinston-dynamodb 将 http 请求和响应存储到 dynamodb。

https://www.npmjs.com/package/winston-dynamodb

这是表的架构

-hostname
-id
-level(partition key)
-meta (the request and response detail will be stored here)
-msg
-timestamp(sort key)

将数据存入dynamodb成功但格式不理想
是否可以使用以下格式存储数据?

{
"id" : ObjectId("5a12537d81b6b634cb7d4696"),
"timestamp" : ISODate("2017-11-20T04:01:01.229Z"),
"level" : "info",
"status": 200,
"requestUrl": '/',
"meta" : {}
}

类似于下面的SO问题,但我使用的是dynamodb而不是mongodb
Logging with winston-mongodb, express-winston and winston

winston-logger.js

const winston = require('winston');
const { DynamoDB } = require('winston-dynamodb');
const expressWinston = require('express-winston');

const logger = expressWinston.logger({
  transports: [
    new DynamoDB({
      accessKeyId: "accessKey",
      secretAccessKey: "secretKey",
      region: "myRegion",
      tableName: "test"
    })
  ],
  format: winston.format.combine(
    winston.format.colorize(),
    winston.format.timestamp(),
    winston.format.json()
  ),
  metaField: null,
  expressFormat: true,
  requestWhitelist: ['headers', 'body', 'query'],
  responseWhitelist: [,'body'],
})

module.exports = logger

app.js

const winstonLogger = require('./winston-logger');
app.use(winstonLogger)

0 个答案:

没有答案