Skip to content

Commit 8d0957c

Browse files
Break down and add NODE_ENV awareness to the default logger
1 parent 97c6a60 commit 8d0957c

File tree

3 files changed

+21
-1
lines changed

3 files changed

+21
-1
lines changed

packages/optimizely-sdk/lib/plugins/logger/index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,15 @@ Logger.prototype.setLogLevel = function(logLevel) {
7878
* @private
7979
*/
8080
Logger.prototype.__shouldLog = function(targetLogLevel) {
81+
try {
82+
if (process.env.NODE_ENV === 'test') {
83+
// Suppress logs during testing.
84+
return false;
85+
}
86+
} catch (e) {
87+
// `process` is likely a ReferenceError in non-Node.js environments
88+
}
89+
8190
return targetLogLevel >= this.logLevel;
8291
};
8392

packages/optimizely-sdk/lib/plugins/logger/index.tests.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,20 @@ describe('lib/plugins/logger', function() {
3333
});
3434

3535
describe('log', function() {
36+
var nodeEnv;
37+
3638
beforeEach(function() {
3739
defaultLogger = logger.createLogger({logLevel: LOG_LEVEL.INFO});
3840
sinon.stub(defaultLogger, '__consoleLog');
41+
42+
// The usual rules of turning off logging don't apply here,
43+
// since the code under test _is the logger_.
44+
nodeEnv = process.env.NODE_ENV;
45+
process.env.NODE_ENV = 'production';
46+
});
47+
48+
afterEach(function() {
49+
process.env.NODE_ENV = nodeEnv;
3950
});
4051

4152
it('should log the given message', function() {

packages/optimizely-sdk/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "lib/index.node.js",
66
"browser": "lib/index.browser.js",
77
"scripts": {
8-
"test": "mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js --recursive",
8+
"test": "NODE_ENV=test mocha --reporter dot './lib/**/*.tests.js'",
99
"test-xbrowser": "karma start karma.bs.conf.js --single-run",
1010
"lint": "eslint lib/**",
1111
"cover": "istanbul cover _mocha ./lib/*.tests.js ./lib/**/*.tests.js ./lib/**/**/*tests.js",

0 commit comments

Comments
 (0)