0

log level在 Socket.IO 中,我可以通过编辑选项来调整记录器的“详细程度” :

The amount of detail that the server should output to the logger.
0 - error
1 - warn
2 - info
3 - debug

现在我正在使用 Sock.js。我的日志文件被这些消息填充:

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过滤器过滤具有严重性的消息?

4

1 回答 1

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
});
于 2013-10-08T14:16:44.413 回答