如何将文件名传递给wiston logger构造函数?

时间:2013-04-18 07:23:50

标签: javascript winston

log.js

var winston = require('winston');

var logger = new (winston.Logger)({
  transports: [
    new (winston.transports.Console)({ json: false, timestamp: true }),
    new winston.transports.File({ filename: **Get from outside**, json: false })
  ],
});

module.exports = logger;

a.js ,想要登录自己的日志文件

var logger = require('./log')('log_to_this_file'); <=== How can I pass a filename in?

logger.info('log to file');

b.js ,也想登录自己的日志文件

var logger = require('./log')('log_to_another_file'); <=== How can I pass a filename in?

logger.info('log to file');

我不知道如何编写log.js以从外部获取文件名。

我怎么能这样?

1 个答案:

答案 0 :(得分:0)

你能不能在b.js中添加一个新的记录器?

  var winston = require('winston');

  //
  // Configure the logger for `category1`
  //
  winston.loggers.add('category1', {
    console: {
      level: 'silly',
      colorize: 'true',
      label: 'category one'
    },
    file: {
      filename: '/path/to/some/file'
    }
  });