如何使用 winston 日志记录创建事件 ID?

How to create event id using winston logging?

我们正在渲染来自 kafka 总线的事件并使用 winston logger 写入文件系统,现在为了增强用户想要从文件中搜索事件的一些功能,所以出于这个特殊原因,我想为我们处理的每个事件生成一些 id正在写入日志文件。所以我的问题是,当我们登录到文件时,是否可以使用 winston 生成某种 ID。

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.

首先你可以生成UUID (npm install node-uuid --save):

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

,然后我们有 2 个解决方案:

  1. 通过字符串插值将其添加到日志消息中:

    ...
     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. 通过元将其添加到日志消息中 - 这允许两种传输之间的一致性:

       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);
      } }