-
Notifications
You must be signed in to change notification settings - Fork 450
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #258 from share/logger-override
Allow custom logger overrides
- Loading branch information
Showing
10 changed files
with
129 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
var Logger = require('./logger'); | ||
var logger = new Logger(); | ||
module.exports = logger; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
var SUPPORTED_METHODS = [ | ||
'info', | ||
'warn', | ||
'error' | ||
]; | ||
|
||
function Logger() { | ||
this.setMethods(console); | ||
} | ||
module.exports = Logger; | ||
|
||
Logger.prototype.setMethods = function (overrides) { | ||
overrides = overrides || {}; | ||
var logger = this; | ||
|
||
SUPPORTED_METHODS.forEach(function (method) { | ||
if (typeof overrides[method] === 'function') { | ||
logger[method] = overrides[method]; | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
var Logger = require('../lib/logger/logger'); | ||
var expect = require('expect.js'); | ||
var sinon = require('sinon'); | ||
|
||
describe('Logger', function () { | ||
describe('Stubbing console.warn', function () { | ||
beforeEach(function () { | ||
sinon.stub(console, 'warn'); | ||
}); | ||
|
||
afterEach(function () { | ||
sinon.restore(); | ||
}); | ||
|
||
it('logs to console by default', function () { | ||
var logger = new Logger(); | ||
logger.warn('warning'); | ||
expect(console.warn.calledOnceWithExactly('warning')).to.be(true); | ||
}); | ||
|
||
it('overrides console', function () { | ||
var customWarn = sinon.stub(); | ||
var logger = new Logger(); | ||
logger.setMethods({ | ||
warn: customWarn | ||
}); | ||
|
||
logger.warn('warning'); | ||
|
||
expect(console.warn.notCalled).to.be(true); | ||
expect(customWarn.calledOnceWithExactly('warning')).to.be(true); | ||
}); | ||
|
||
it('only overrides if provided with a method', function () { | ||
var badWarn = 'not a function'; | ||
var logger = new Logger(); | ||
logger.setMethods({ | ||
warn: badWarn | ||
}); | ||
|
||
logger.warn('warning'); | ||
|
||
expect(console.warn.calledOnceWithExactly('warning')).to.be(true); | ||
}); | ||
}); | ||
}); |