2013-10-08 41 views
0

調整Sock.js的記錄詳細程度,就像在Socket.IO,我可以通過編輯log level選項調整記錄器的「冗長」 Sock.js.我的日誌文件被這些消息填充:在Socket.io

POST /733/o1q4zdmo/xhr_send?t=1380900035633 5ms 204 
POST /733/o1q4zdmo/xhr_send?t=1380900036926 6ms 204 
POST /733/o1q4zdmo/xhr_send?t=1380900041212 4ms 204 
POST /733/o1q4zdmo/xhr_send?t=1380900045510 1ms 204 

我想過濾它們。我怎麼能在Sock.js中做到這一點?唯一的解決方案是重寫日誌功能? (使用log設置),然後使用switch過濾嚴重性爲?

回答

1

我已經解決了這個問題,通過進行自定義日誌功能:

// return a function that ouputs log messages from socks.js, filtered on 
// verbosity level. With a value of 0 it prints only errors, 1 info messages 
// too, and to print everything, including debug messages, use a value of 2. 

function make_socks_log(verbosity) { 
    return function(severity, message) { 
     /* Severity could be the following values: 
      * - `debug` (miscellaneous logs), 
      * - `info` (requests logs), 
      * - `error` (serious errors, consider filing an issue). 
      */ 
      switch(severity) { 
       case 'debug': 
       if(verbosity >= 2) { 
        console.log(message); 
       } 
       break; 
       case 'info': 
       if(verbosity >= 1) { 
        console.log(message); 
       } 
       break; 
       case 'error': 
       console.log(message); 
       break; 
      } 
    } 
} 

和創建socks.js服務器時:

socksjs_server = sockjs.createServer({ 
    log: make_socks_log(0) // only error messages will be logged 
});