hussain
hussain

Reputation: 7113

How to create event id using winston logging?

we are rendering events from kafka bus and writing into file systems using winston logger , Now to enhance some functionality user want to search events from the file so for that particular reason i want to generate some id with each event that we are writing to log file. SO my question is , is it possible to generate some sort of id's using winston when we log to file.

winstonServer.js

var Consumer = {
    start: function() {
        var logger = new(winston.Logger)({
            level: null,
            transports: [
                new(winston.transports.Console)(),
                new(winston.transports.File)({
                    filename: './logs/dit/server.log',
                    maxsize: 1024 * 1024 * 15, // 15MB
                    timestamp: false,
                    maxFiles: 10,
                    json: false,
                    formatter: function(options) {
                        return options.message;
                    }
                })
            ]
        });

        function startConsumer(consumer) {
            consumer.on('message', function(message) {
                logger.log('info', message.value);
                io.io().emit('ditConsumer', message.value);
            });
            consumer.on('error', function(err) {
                console.log('error', err);
            });
        };
        startConsumer(consumer);
    }
}

server.log

testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took 43fd41a7-d1fb-4c19-9de6-19170aee171f a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took 43fd41a7-d1fb-4c19-9de6-19170aee171f a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took 43fd41a7-d1fb-4c19-9de6-19170aee171f a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took 43fd41a7-d1fb-4c19-9de6-19170aee171f a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took 43fd41a7-d1fb-4c19-9de6-19170aee171f a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took 43fd41a7-d1fb-4c19-9de6-19170aee171f a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.
testid Lorem test Ipsum is simply dummy text text of the printing  and typesetting industry. Lorem Ipsum has been the industrys standard dummy text ever since the 1500s, when an unknown printer took 43fd41a7-d1fb-4c19-9de6-19170aee171f a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged.

Upvotes: 0

Views: 1000

Answers (1)

BlackStork
BlackStork

Reputation: 8371

First you can generate UUID (npm install node-uuid --save):

 const uuid = require('node-uuid');

, and then we have 2 solutions:

  1. add it to log message via string interpolation :

    ...
     function startConsumer(consumer) {
        consumer.on('message', function(message) {
            logger.log('info', `[ID:${uuid.v4()}]${message.value}`);
            io.io().emit('ditConsumer', message.val);
        });
        consumer.on('error', function(err) {
            console.log('error', err);
        });
    };
    startConsumer(consumer);
    

    ...

  2. add it to log message via meta - this allows uniformity between both transports :

       var Consumer = {
         start: function() {
         const formatter = function(options) {
                    return `[ID:${options.meta.ID}]${options.value || options.meta.value}`;
                };
         var logger = new(winston.Logger)({
          level: null,
          transports: [
              new(winston.transports.Console)({formatter}),
              new(winston.transports.File)({
                filename: './logs/dit/server.log',
                maxsize: 1024 * 1024 * 15, // 15MB
                timestamp: false,
                maxFiles: 10,
                json: false,
                formatter
            })
        ]
    });
    
    function startConsumer(consumer) {
        consumer.on('message', function(message) {
            logger.log('info', message.value, {ID:uuid.v4(), value:message:value});
            io.io().emit('ditConsumer', message.value );
        });
        consumer.on('error', function(err) {
            console.log('error', err);
        });
      };
     startConsumer(consumer);
      } }
    

Upvotes: 1

Related Questions