2017-03-02 85 views
0

我們從kafka總線渲染事件並使用winston記錄器寫入文件系統,現在爲了增強某些功能,用戶希望從文件中搜索事件,所以出於這個特定原因,我想爲每個正在寫入的事件生成一些id日誌文件。所以我的問題是,當我們登錄到文件時,是否有可能使用winston生成某種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. 

回答

1

首先,你可以生成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); 
        } } 
    
+0

它爲所有的日誌生成相同的ID,第二個問題是我看到ID是從控制檯中的'logger.log'打印的,但它不寫ID到'server.log'文件 – hussain

+0

Yeop,我將糾正一次將到達家庭 - 它應該是logId:uuid.v4()正確的方法調用參數。我將檢查傳輸API,並將其更正以指明它在何處登錄到文件。 – BlackStork

+0

是的,現在我看到新的ID,但它仍然沒有寫入ID文件,這是日誌文件的全部目的,所以我們可以搜索它 – hussain