如何排除带有 express-winston 的 cookie?

How to exclude cookies with express-winston?

阅读 express-winston 自述文件,似乎很容易从日志行中删除 headers:我们可以简单地对 requestWhitelist 采取行动选项,但这将禁止记录所有 headers。

有没有办法只禁用 cookie header?

据我所知,您可以创建自定义过滤器,例如:

function customRequestFilter(req, propName) {
  if(propName !== "headers") return req[propName];

  const { cookie, ...rest } = req.headers;

  return rest;
}

你的温斯顿选项应该是这样的:

expressWinston.logger({
    transports: [new winston.transports.Console()],
    // requestWhitelist: ['headers'],
    requestFilter: customRequestFilter,
    format: winston.format.combine(
      winston.format.colorize(),
      winston.format.json()
    ),
    meta: true, // optional: control whether you want to log the meta data about the request (default to true)
    msg: "HTTP {{req.method}} {{req.url}}", // optional: customize the default logging message. E.g. "{{res.statusCode}} {{req.method}} {{res.responseTime}}ms {{req.url}}"
    expressFormat: true, // Use the default Express/morgan request formatting. Enabling this will override any msg if true. Will only output colors with colorize set to true
    colorize: false, // Color the text and status code, using the Express/morgan color palette (text: gray, status: default green, 3XX cyan, 4XX yellow, 5XX red).
    ignoreRoute: function (req, res) {
      return false;
    }, // optional: allows to skip some log messages based on request and/or response
  })

希望对您有所帮助! 干杯。