javascript winston:尝试在没有传输的情况下写入日志 - 使用默认记录器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52017160/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 09:47:16  来源:igfitidea点击:

winston: Attempt to write logs with no transports - using default logger

javascriptnode.jswinston

提问by ekntrtmz

I followed a tutorial to set up winston (2.x) default logger in my express app. When updating to the current version of winston (3.0.0) I have a problem with adding the transports. I have followed the latest docsbut still I get the notice in console and no log files are created at all:

我按照教程在我的 express 应用程序中设置了 winston (2.x) 默认记录器。更新到当前版本的 winston (3.0.0) 时,我在添加传输时遇到问题。我遵循了最新的文档,但仍然在控制台中收到通知,并且根本没有创建日志文件:

[winston] Attempt to write logs with no transports

[winston] 尝试在没有传输的情况下写入日志

logging.js

日志记录.js

const winston = require('winston');

module.exports = function () {

  const files = new winston.transports.File({ filename: 'logfile.log' });
  const myconsole = new winston.transports.Console();

  winston.add(myconsole);
  winston.add(files);

}

index.js

索引.js

const winston = require('winston');
...

require('./logging');
winston.info("Give some info");

[winston] Attempt to write logs with no transports {"message":"Give some info","level":"info"}

[winston] 尝试在没有传输的情况下编写日志 {"message":"Give some info","level":"info"}

What am I doing wrong?

我究竟做错了什么?

采纳答案by GCQ

I also had a similar issue. If I recall correctly, I had to call the requirement as a function in my index.js.

我也有类似的问题。如果我没记错的话,我不得不在我的 index.js 中将需求作为函数调用。

require('./logging')();

回答by davewy

In winston 3 you need to create a loggerobject, then add transports to it.

在 winston 3 中,您需要创建一个logger对象,然后transport向其中添加s。

Winston 3 has many examples, but to adapt from the readme, do something like this:

Winston 3 有很多示例,但要根据自述文件进行调整,请执行以下操作:

const logger = winston.createLogger({
  level: 'info',
  format: winston.format.json(),
  transports: [
    new winston.transports.Console(),
    new winston.transports.File({ filename: 'logfile.log' })
  ]
});

logger.info('it works!!');

回答by Ssendikadiwa Stephen

If you want to use the default logger in winston v3 then you simply have to add this piece of code in your main file

如果您想在 winston v3 中使用默认记录器,那么您只需在主文件中添加这段代码

const winston = require('winston')

winston.add(new winston.transports.File({ filename: 'logfile.log' }))

回答by DiaMaBo

I had the same issue and by just looking at the documentation, I was able to write logs in a file by just adding the below code.

我遇到了同样的问题,只需查看文档,我就可以通过添加以下代码将日志写入文件。

1. create a file logger.js and paste the below code

1. 创建一个文件 logger.js 并粘贴以下代码

// => logger.js
const logger = winston.createLogger({
level: 'info',
format: winston.format.json(),
defaultMeta: { service: 'user-service' },
transports: [   
  new winston.transports.File({ filename: 'error.log', level: 'error' }),
  new winston.transports.File({ filename: 'combined.log' })
]
});

if (process.env.NODE_ENV !== 'production') {
   logger.add(new winston.transports.Console({
   format: winston.format.simple()
   }));
}

// just add the below to write into a file
winston.add(logger);

2. require it into your app.js file

2. 将它添加到你的 app.js 文件中

// => app.js
require('./middlewares/logger');

3. Test it by just writing the below code inside a function of one of your endpoints.

3. 只需在您的端点之一的函数中编写以下代码即可对其进行测试。

     router.get('/', async (req, res) => {
       throw new Error('Resource not found');  
       res.send({ error: 'Resource not found' });
     }); 

Et Voila ?a marche.Go inside your error log file to find this message there.

Et Voila ?一个游行。进入您的错误日志文件以在那里找到此消息。

{"message":"500 - Resource not found - /api/genres - GET - ::1","level":"error"}