【问题标题】:How to exclude cookies with express-winston?如何使用 express-winston 排除 cookie?
【发布时间】:2020-12-02 11:22:46
【问题描述】:

阅读express-winston README 似乎很容易从记录的行中删除 headers:我们可以简单地对requestWhitelist 选项进行操作,但这将禁止记录所有标头。

有没有办法只禁用 cookie 标头?

【问题讨论】:

    标签: javascript express winston


    【解决方案1】:

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

    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
      })
    

    希望对你有帮助! 干杯。

    【讨论】:

    • 除了customRequestFilter 可以简化很多(正如我编辑的那样),正是我想要的。
    猜你喜欢
    • 1970-01-01
    • 2018-06-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2022-06-30
    相关资源
    最近更新 更多