Pino记录中的编辑请求标头

时间:2019-10-02 22:59:33

标签: logging koa

我使用的是koa-pino-logger,默认情况下,请求完成后,我会获得完整的请求详细信息:

req: {
  "id": 1,
  "method": "POST",
  "url": "/v1/applications/c2cc6b32-1533-4c4c-b8b6-b581855839bb/applicants",
  "headers": {
    "content-type": "application/json",
    "authorization": "Bearer eyJ0eXAiOiJK...",
    "user-agent": "PostmanRuntime/7.17.1",
    "accept": "*/*",
    "cache-control": "no-cache",
    "postman-token": "ac990b73-0eb7-4ab1-9767-6e36e96fd101",
    "host": "localhost:8080",
    "accept-encoding": "gzip, deflate",
    "content-length": "406",
    "connection": "keep-alive"
  },
  "remoteAddress": "::ffff:172.18.0.1",
  "remotePort": 54348
}
res: {
  "statusCode": 201,
  "headers": {
    "x-dns-prefetch-control": "off",
    "x-frame-options": "SAMEORIGIN",
    "strict-transport-security": "max-age=15552000; includeSubDomains",
    "x-download-options": "noopen",
    "x-content-type-options": "nosniff",
    "x-xss-protection": "1; mode=block",
    "content-type": "application/json; charset=utf-8",
    "content-length": "68"
  }
}
responseTime: 1185

我尝试使用修订(https://github.com/pinojs/pino/blob/master/docs/redaction.md)删除要求的标头


import logger = require('koa-pino-logger');

const app: any = new Koa()
  .use(logger({
    redact: ['req.headers','req.headers.authorization'] // This is not working >:()
  }))
  .use(helmet())
  .use(bodyParser())
  .use(coreErrorsMiddleware)
  .use(
    oas({
      file: `${__dirname}/docs/openapi.yaml`,
      uiEndpoint: '/swagger',
      endpoint: '/openapi.json',
      errorHandler,
      validateResponse: false,
      enableUi: isDevelopmentEnvironment,
    })
  )
  .use(reload(() => import('./routes')));

这根本没有任何作用,虽然有效的是serializers,但是如果我尝试修改请求日志并返回它,则会收到架构验证错误:


  .use(logger({
    serializers: {
      ["req"]: (val) => {
        // retracting req logging
        val.headers = 'redacted';
        return val
      },
    },
  }))

产生

RequestValidationError: Schema validation error

所以我现在正在使用的是通过完全不编辑req而不返回任何内容:

  .use(logger({
    serializers: {
      ["req"]: (val) => {
        // retracting req logging
      },
    },
  }))

其结果是:

enter image description here

我使用编校方法有误吗? 还有其他记录器可以用来实现我的目标吗?

1 个答案:

答案 0 :(得分:0)

我会检查您安装的pino的版本。 redactpino的功能之一。如果您有v5.0.0版的<pino,则需要使用v5.*