本文介绍了如何使用Winston logger记录新行中的每条消息?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们正在使用Winston记录器将事件记录到nodejs fs中,我想使用Winston将每个事件写入新行中,是否可以使用Winston库或与Node.js一起使用的任何其他方法来完成该任务.

we are using winston logger to log events to nodejs fs , i want to write every event into new line using winston, is it possible with to achieve that task using winston library or any other approach that works with 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);

推荐答案

我使用split模块使用这样的内容:

I use something like this, using the split module:

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;

根据您的行驶里程可能会有所不同,它可以很好地满足我的需求.

It works pretty well for my needs by your mileage may vary.

split模块:

这篇关于如何使用Winston logger记录新行中的每条消息?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-16 07:00