将 winston 添加到 express 应用程序

Ann*_*lee 3 javascript node.js winston

我想在我的快递应用程序中使用winston

我已经像下面这样配置了 winston:

const path = require('path')
const winston = require('winston')

module.exports = () => {
  process.on('uncaughtException', err => winston.error('uncaught exception: ', err))
  process.on('unhandledRejection', (reason, p) => winston.error('unhandled rejection: ', reason, p))

  winston.emitErrs = true
  winston.exitOnError = false
  winston.level = process.env.NODE_ENV === 'production' ? 'info' : 'debug'
  winston.remove(winston.transports.Console)

  winston.add(winston.transports.Console, {
    level: process.env.NODE_ENV === 'production' ? 'info' : 'debug',
    handleExceptions: true,
    prettyPrint: true,
    humanReadableUnhandledException: false,
    json: false,
    colorize: true,
    timestamp: new Date(),
  })

  winston.add(winston.transports.File, {
    level: 'info',
    filename: path.join(__dirname, '../logs/app.log'),
    handleExceptions: true,
    humanReadableUnhandledException: true,
    json: false,
    maxsize: 10242880, // ~10MB
    maxFiles: 3,
    colorize: false,
    timestamp: new Date(),
  })
}
Run Code Online (Sandbox Code Playgroud)

在我的快递应用程序中,我想添加 winston 作为中间件来记录控制台:

const express = require('express')
const winston = require("./config/winston")
const app = express()

//initialize winston
app.use(winston)

app.get('/', function (req, res) {
  res.send('Hello World!')
})

//Start Server
const port = process.env.APP_PORT || 3000
const host = process.env.APP_HOST || "localhost"

app.listen(port, function() {
  console.log("Listening on " + host + ":" + port)
})
Run Code Online (Sandbox Code Playgroud)

我的问题是我的应用程序无法加载并返回以下错误消息:

Error: Transport already attached: file, assign a different name
    at exports.Logger.Logger.add (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\winston\lib\winst
on\logger.js:487:11)
    at Object.winston.(anonymous function) [as add] (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_module
s\winston\lib\winston.js:86:34)
    at module.exports (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\src\t02-loggingToFolder.js:23:11)
    at Layer.handle [as handle_request] (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\express\li
b\router\layer.js:95:5)
    at trim_prefix (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\express\lib\router\index.js:317
:13)
    at C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\express\lib\router\
index.js:335:12)
    at next (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\express\lib\router\index.js:275:10)
    at expressInit (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\express\lib\middleware\init.js:
40:5)
    at Layer.handle [as handle_request] (C:\Users\user\Desktop\Coding Projects\learning_npm_winston\node_modules\express\li
b\router\layer.js:95:5)
Run Code Online (Sandbox Code Playgroud)

任何建议我做错了什么?

我感谢您的回复!

rob*_*lep 6

您的模块导出一个设置 winston 的函数。该函数被传递给 Express:

app.use(winston)
Run Code Online (Sandbox Code Playgroud)

因此,对于每个请求,都会调用该函数,这意味着每次执行 winston 设置时,都会添加传输ConsoleFile传输。

出于某种原因,您Console首先删除传输,因此不会触发错误,但是当File添加传输(一次又一次等)时,您将收到错误:“传输已附加:文件,分配一个不同的名字”(换句话说:你已经有了一个File附加到记录器传输,如果你明确更改它的名称,你只能添加另一个)。

这里的主要问题是导出的函数不是 Express 中间件函数,因此您无法将其传递给app.use()并期望它正常工作。

如果您打算使用 winston 记录 HTTP 请求,则应使用适当的中间件模块,例如express-winston.