سؤال

In Socket.IO I could adjust the "verbosity" of the logger by editing the log level option:

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

Now I'm using Sock.js. My log file gets filled by these messages:

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 

I want to filter them. How could I do this in Sock.js? The only solution is an override of the log function? (with the log setting), and then with a switch filter the messages with the severity?

هل كانت مفيدة؟

المحلول

I've resolved this issue, by making a custom log function:

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

And when creating the socks.js server:

socksjs_server = sockjs.createServer({
    log: make_socks_log(0) // only error messages will be logged
});
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top