繁体   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