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
45 changes: 36 additions & 9 deletions lib/configuration.js
Original file line number Diff line number Diff line change
Expand Up @@ -78,21 +78,47 @@ confProto.setOptions = function (options) {

confProto.debug = false;

confProto.setDebugLevel = function (level) {
var that = this;
var _DEBUG_LEVEL_KEY = 'http2-cache.configuration._debugLevel',
_DEBUG_LEVEL_DEFAULT = false;

confProto.setDebugLevel = function (debugLevel, persist) {

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

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

level = typeof level === 'string' ? level : level === true ? 'info' : null;
_DEBUG_LEVEL_DEFAULT = debugLevel;

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

// Compute default value from store then const
} 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 (that._log && that._log.hasOwnProperty('debugLevel')) {
that._log.debugLevel = level;
if (this._log && this._log.hasOwnProperty('debugLevel')) {
this._log.debugLevel = debugLevel;
}

// Sync current options value
that.options.debug = level;
this.options.debug = debugLevel;

// Sync and cast has boolean
that.debug = !!that.options.debug;
this.debug = !!debugLevel;

// Chainable
return this;
};

confProto.configuring = function () {
Expand Down Expand Up @@ -141,7 +167,8 @@ confProto.getTransport = function (url) {
if (uri.protocol === 'ws:' || uri.protocol === 'wss:') {
activeTransportConnections[uri.href] = websocket(uri.href, "h2", {
// TODO, maybe enable perMessageDeflate in production or on debug??
perMessageDeflate: that.debug === false
// Do perMessageDeflate false only on browser and if debug cause if make nodeJS spec fail
perMessageDeflate: typeof process !== 'undefined' || that.debug === false
});

} else if(uri.protocol === 'tcp:') {
Expand Down
5 changes: 4 additions & 1 deletion lib/http2-cache.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
enableXHROverH2 = require('./xhr.js').enableXHROverH2;

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

// To update debug level after injection:
Expand All @@ -20,4 +20,7 @@
//- XMLHttpRequest.configuration.setDebugLevel('trace');
}

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

}(this));
17 changes: 10 additions & 7 deletions lib/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ 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,
Expand All @@ -37,24 +38,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
};