使用MongoDB记录Winston

时间:2018-11-28 14:39:47

标签: node.js mongodb express winston

下面是我的代码,注销到MongoDB。我想知道为什么要写入文件,但是我可以记录错误,但是在MongoDB中,我只能获取路由/请求。

每次发生错误时,我都希望将其记录到MongoDB。

这是我的文件:

var winston = require('winston');
require('winston-mongodb');

// define the custom settings for each transport (file, console)
var options = {
  file: {
    level: 'silly',
    filename: 'logs/combined.json',
    handleExceptions: true,
    json: true,
    maxsize: 5242880, // 5MB
    maxFiles: 5,
    colorize: false,
    prettyPrint: true
  },
  console: {
    level: 'silly',
    handleExceptions: true,
    json: false,
    colorize: true,
    prettyPrint: true
  },
};

// instantiate a new Winston Logger with the settings defined above

var logger = winston.createLogger({
  format: winston.format.json(),
  transports: [
    new winston.transports.File(options.file),
    new winston.transports.Console(options.console),
    new winston.transports.MongoDB({
      level: 'silly',
      db: 'mongodb://username:password@ds123456.mlab.com:23456/heroku_3edfgy6l',
      collection: 'log'
    })
  ],
  exitOnError: false, // do not exit on handled exceptions
});


// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
  write: function(message, encoding) {
    // use the 'silly' log level so the output will be picked up by both transports (file and console)
    logger.silly(message);
  }
};

module.exports = logger;

如何在此处专门将错误登录到MongoDB?我只得到一个“消息”对象,而没有一个错误对象。

我尝试更改日志级别等

谢谢!

0 个答案:

没有答案