2017-03-02 5 views
0

私たちはkafkaバスからイベントをレンダリングし、winston loggerを使用してファイルシステムに書き込んでいます。そのため、特定の機能を強化するために、ファイルからイベントを検索する必要があります。ログファイル。だから私の質問は、私たちがファイルにログオンするときにwinstonを使ってIDの何らかの種類を生成することが可能かどうかです。winston loggingを使用してイベント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を生成しています.2番目の問題は、IDがコンソールの 'logger.log'から印刷していますidsを 'server.log'ファイル – hussain

+0

Yeop、私は一度修正して家に到着します - それはlogIdでなければなりません:uuid.v4()はメソッド呼び出しのパラメータの右にあります。私は、転送APIをチェックし、それがファイルに記録する場所を明記するように修正します。 – BlackStork

+0

さて、私は新しいIDを参照していますが、まだIDがログファイルのIDであることを目的としたログファイルには書かれていません – hussain

関連する問題