gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 04:53:13 24 4
gpt4 key购买 nike

我们正在渲染来自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);
}
}

服务器日志

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

关于javascript - 如何使用winston 日志记录创建事件ID?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42559229/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com