簡體   English   中英

如何使用 winston 日志記錄創建事件 ID?

[英]How to create event id using winston logging?

我們正在從 kafka 總線渲染事件並使用 winston logger 寫入文件系統,現在為了增強一些功能,用戶想要從文件中搜索事件,因此出於這個特殊原因,我想為我們寫入日志文件的每個事件生成一些 id . 所以我的問題是,當我們登錄到文件時,是否可以使用winston生成某種 ID。

溫斯頓服務器.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);
    }
}

服務器日志

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM