【发布时间】:2020-12-02 11:22:46
【问题描述】:
阅读express-winston README 似乎很容易从记录的行中删除 headers:我们可以简单地对requestWhitelist 选项进行操作,但这将禁止记录所有标头。
有没有办法只禁用 cookie 标头?
【问题讨论】:
标签: javascript express winston
阅读express-winston README 似乎很容易从记录的行中删除 headers:我们可以简单地对requestWhitelist 选项进行操作,但这将禁止记录所有标头。
有没有办法只禁用 cookie 标头?
【问题讨论】:
标签: javascript express winston
据我所知,您可以创建一个自定义过滤器,例如:
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 可以简化很多(正如我编辑的那样),正是我想要的。