Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将winston添加到express应用程序_Javascript_Node.js_Winston - Fatal编程技术网

Javascript 将winston添加到express应用程序

Javascript 将winston添加到express应用程序,javascript,node.js,winston,Javascript,Node.js,Winston,我想在我的express应用程序中使用 我对winston进行了如下配置: const path = require('path') const winston = require('winston') module.exports = () => { process.on('uncaughtException', err => winston.error('uncaught exception: ', err)) process.on('unhandledRejectio

我想在我的express应用程序中使用

我对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(),
  })
}
在我的express应用程序中,我想将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)
})
我的问题是我的应用程序未加载,并返回以下错误消息:

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)
有什么建议我做错了什么吗


谢谢你的回复

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

app.use(winston)
因此,对于每个请求,都会调用该函数,这意味着每次执行winston设置时,都会添加
控制台
文件
传输

出于某种原因,您首先要删除
控制台
传输,这样不会触发错误,但是当添加
文件
传输时(一次又一次,等等),您会收到错误:“传输已附加:文件,分配其他名称”(换句话说:您已经将一个
文件
传输附加到记录器,并且只有在明确更改其名称时才能添加另一个文件)

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

如果您打算使用winston记录HTTP请求,那么应该使用适当的中间件模块,如