如何使用winston logger记录新行中的每条消息?

时间:2017-03-22 17:45:02

标签: javascript node.js logging winston

我们使用winston logger将事件记录到nodejs fs,我想使用winston将每个事件写入新行,是否可以使用winston库或任何其他与nodejs一起使用的方法来实现该任务。

ctrl.js

var winston = require('winston');
var consumer = new ConsumerGroup(options, topics);
        console.log("Consumer topics:", getConsumerTopics(consumer).toString());
        logger = new (winston.Logger)({
            level: null,
            transports: [
//                new (winston.transports.Console)(),
                new (winston.transports.File)({
                    filename: './logs/st/server.log',
                    maxsize: 1024 * 1024 * 20,//15728640 is 15 MB
                    timestamp: false,
                    json: false,
                    formatter: function (options) {
                        return options.message;
                    }
                })
            ]
        });
        function startConsumer(consumer) {
            consumer.on('message', function (message) {
                logger.log('info', message.value);
                //callback(message.value);
                io.io().emit('StConsumer', message.value);
            });
            consumer.on('error', function (err) {
                console.log('error', err);
            });
        };
        startConsumer(consumer);

1 个答案:

答案 0 :(得分:0)

我使用split模块使用类似的东西:

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

winston.emitErrs = false;

const logger = new winston.Logger({
  transports: [
    new winston.transports.File({
      level: 'debug',
      filename: 'server.log',
      handleExceptions: true,
      json: false,
      maxsize: 5242880,
      maxFiles: 5,
      colorize: false,
      timestamp: true,
    }),
    new winston.transports.Console({
      level: 'debug',
      handleExceptions: true,
      json: false,
      colorize: true,
      timestamp: true,
    }),
  ],
  exitOnError: false,
});

logger.stream = split().on('data', message => logger.info(message));

module.exports = logger;

根据我的需要,你的里程可能会有所不同。

split模块: