Skip to content

Commit

Permalink
Fixed logging options, closes socketio#540
Browse files Browse the repository at this point in the history
  • Loading branch information
3rd-Eden committed Oct 10, 2011
1 parent 07b84f4 commit 0b7ed64
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 3 deletions.
3 changes: 2 additions & 1 deletion lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ var Logger = module.exports = function (opts) {
opts = opts || {}
this.colors = false !== opts.colors;
this.level = 3;
this.enabled = true;
};

/**
Expand All @@ -71,7 +72,7 @@ var Logger = module.exports = function (opts) {
Logger.prototype.log = function (type) {
var index = levels.indexOf(type);

if (index > this.level)
if (index > this.level || !this.enabled)
return this;

console.log.apply(
Expand Down
6 changes: 4 additions & 2 deletions lib/manager.js
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ function Manager (server, options) {
, transports: defaultTransports
, authorization: false
, 'log level': 3
, 'log colors': true
, 'close timeout': 25
, 'heartbeat timeout': 15
, 'heartbeat interval': 20
Expand Down Expand Up @@ -147,10 +148,11 @@ Manager.prototype.__defineGetter__('store', function () {
*/

Manager.prototype.__defineGetter__('log', function () {
if (this.disabled('log')) return;

var logger = this.get('logger');

logger.level = this.get('log level') || -1;
logger.colors = this.get('log colors');
logger.enabled = this.enabled('log');

return logger;
});
Expand Down
43 changes: 43 additions & 0 deletions test/manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,49 @@ module.exports = {

io.get('resource').should.equal('/my resource');
io.get('custom').should.equal('opt');
io.server.close();
done();
},

'test disabling the log': function (done) {
var port = ++ports
, io = sio.listen(port, { log: false })
, _console = console.log
, calls = 0;

// the logger uses console.log to output data, override it to see if get's
// used
console.log = function () { ++calls };

io.log.debug('test');
io.log.log('testing');

console.log = _console;
calls.should.equal(0);

io.server.close();
done();
},

'test disabling logging with colors': function (done) {
var port = ++ports
, io = sio.listen(port, { 'log colors': false })
, _console = console.log
, calls = 0;

// the logger uses console.log to output data, override it to see if get's
// used
console.log = function (data) {
++calls;
data.indexOf('\033').should.equal(-1);
};

io.log.debug('test');
io.log.log('testing');

console.log = _console;
calls.should.equal(2);

io.server.close();
done();
}
Expand Down

0 comments on commit 0b7ed64

Please sign in to comment.