Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
276 views
in Technique[技术] by (71.8m points)

javascript - How to log every message in new line using winston logger?

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);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

The split module:


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...