-
Notifications
You must be signed in to change notification settings - Fork 445
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This change adds the ability to override ShareDB's logging behaviour. By default, ShareDB will still log to `console`. However, this default can be overridden with custom methods on both the backend and in the client. ## Supported methods The ShareDB logger only supports the following methods: - `info` - `warn` - `error` Any method that is not overridden will default to `console`. ## Backend The backend methods can be overridden when instantiating `Backend`: ```javascript var share = new Backend({ logger: { info: () => {}, // Silence info warn: () => alerts.warn(arguments), // Forward warnings error: () => alerts.critical(arguments) // Map errors to critical } }); ``` ## Client Client methods can also be overridden: ```javascript var ShareDB = require('sharedb/lib/client'); ShareDB.logger.setMethods({ info: () => {}, // etc. }); ```
- Loading branch information
Alec Gibson
committed
Nov 15, 2018
1 parent
a67ad42
commit 002c4c2
Showing
10 changed files
with
132 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); | ||
}); | ||
}); | ||
}); |