Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement persistent debugLevel #43

Open
wants to merge 13 commits into
base: develop
Choose a base branch
from
38 changes: 33 additions & 5 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,50 @@ function Configuration(options) {
self.agent = new Agent({
log: self._log
});


}

util.inherits(Configuration, EventEmitter);

var confProto = Configuration.prototype;

confProto.setDebugLevel = function (level) {

level = typeof level === 'string' ? level : level === true ? 'info' : null;
var _DEBUG_LEVEL_KEY = 'http2-cache.configuration._debugLevel',
_DEBUG_LEVEL_DEFAULT = 'log';

confProto.setDebugLevel = function (debugLevel, persist) {

debugLevel = typeof debugLevel === 'string' ? debugLevel : debugLevel === true ? 'log' : null;

// Setter with persist but not 'auto'
if (debugLevel !== 'auto' && persist === true) {

_DEBUG_LEVEL_DEFAULT = debugLevel;

// Use localStorage if available
if (typeof localStorage !== 'undefined') {
localStorage.setItem(_DEBUG_LEVEL_KEY, _DEBUG_LEVEL_DEFAULT);
}

// Compute default value from storet then const
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

storet typo?

} else if (debugLevel === 'auto') {

// Use localStorage if available
if (typeof localStorage !== 'undefined') {
debugLevel = localStorage.getItem(_DEBUG_LEVEL_KEY, _DEBUG_LEVEL_DEFAULT);
}

debugLevel = debugLevel || _DEBUG_LEVEL_DEFAULT;
}

// Init debug/log
if (this._log && this._log.hasOwnProperty('debugLevel')) {
this._log.debugLevel = level;
this._log.debugLevel = debugLevel;
}

this.debug = debugLevel;

// Chainable
return this;
};

confProto.configuring = function () {
Expand Down
10 changes: 6 additions & 4 deletions lib/http2-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
var Configuration = require('./configuration.js'),
enableXHROverH2 = require('./xhr.js').enableXHROverH2;

var configuration = new Configuration({
debug: false // true='info' or (info|debug|trace)
});
enableXHROverH2(XMLHttpRequest, configuration);
enableXHROverH2(XMLHttpRequest, new Configuration({
debug: 'auto' // true='log' or (auto|log|debug|trace)
}));

// To update debug level after injection:
//- XMLHttpRequest.configuration.setDebugLevel('debug');
//- XMLHttpRequest.configuration.setDebugLevel('trace');

// To persit update debug level after injection:
//- XMLHttpRequest.configuration.setDebugLevel('debug', true);

}(this));
32 changes: 24 additions & 8 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,25 @@ var defaultLogger = {
};

var consoleLogger = {
debugLevel: 'info', // 'info|debug|trace'
debugLevel: 'log', // 'log|debug|trace'
log: console.log,
fatal: console.error,
error: console.error,
warn : console.warning || console.info,
info : console.info,
//info : console.info,
// Displayed when debugLevel is (trace|debug)
info: function (data, ctx) {
var info = console.info;
if (
consoleLogger.debugLevel === 'trace' ||
consoleLogger.debugLevel === 'debug'
) {
var args = (typeof ctx === 'string' ? [ctx, data] : arguments);
info.apply(console, args);
}
return this;
},
// Displayed when debugLevel is (trace|debug)
debug: function (data, ctx) {
var debug = console.debug || console.info;
if (
Expand All @@ -27,24 +41,26 @@ var consoleLogger = {
var args = (typeof ctx === 'string' ? [ctx, data] : arguments);
debug.apply(console, args);
}
return this;
},
// Trace only
// Displayed when debugLevel is trace only
trace: function (data, ctx) {
var debug = console.debug || console.info;
if (consoleLogger.debugLevel === 'trace') {
consoleLogger.debug(data, ctx);
return consoleLogger.debug(data, ctx);
}
return this;
},
// Trace only
child: function(msg) {
if (consoleLogger.debugLevel === 'trace') {
console.info(msg);
return console.info(msg);
}
return this;
return this;
}
};

module.exports = {
consoleLogger: consoleLogger,
defaultLogger: defaultLogger
consoleLogger: consoleLogger,
defaultLogger: defaultLogger
};