nodejs 日志模块winston的使用方法

2020-06-17 07:15:54易采站长站整理

let ignoreArr = ['/api', '.js', '.css', '.png', '.jpg', '.gif'] ignoreArr.forEach(item => {
if (req.url.indexOf(item) > -1) notPageRequest = true
})
return notPageRequest
} // optional: allows to skip some log messages based on request and/or response
})

export let apiRequestLogger = (req, res, next) => {
let send = res.send
let content = ''
let query = req.query || {}
let body = req.body || {}
res.send = function () {
content = arguments[0] send.apply(res, arguments)
}
expressWinston.logger({
transports: [
DailyRotateFileTransport('api-request')
],
meta: true, // optional: control whether you want to log the meta data about the request (default to true)
msg () {
return `HTTP ${req.method} ${req.url} query ${JSON.stringify(query)} body ${JSON.stringify(body)} resData ${content} `
},
colorize: true, // 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) {
if (req.headers.self) return true
return false
} // optional: allows to skip some log messages based on request and/or response
})(req, res, next)
}