Skip to content

Instantly share code, notes, and snippets.

@hugosenari
Last active December 14, 2015 20:59
Show Gist options
  • Select an option

  • Save hugosenari/5148132 to your computer and use it in GitHub Desktop.

Select an option

Save hugosenari/5148132 to your computer and use it in GitHub Desktop.
javascript console log wrapper 1 - utls.console.iface.js: interface definition (required) 2 - utils.console.implementation.js: interface implementation (requires only when you want activate console logs)
//implementação do log
utils = utils || {};
utils.console = utils.console || {};
//define se deve ou não mostrar erro no log (para ver stack);
utils.console._showCaller = true;
//níveis de log
utils.console.logLevel = {
ALL: 7,
TRACE: 6,
LOG: 5,
DEBUG: 4,
INFO: 3,
WARN: 2,
ERROR: 1,
NONE: 0,
CURRENT: 2
};
utils.console._getLogLevelName = function (logLevelNumber) {
for (var name in utils.console.logLevel) {
if (utils.console.logLevel[name] == logLevelNumber)
return name;
}
}
//retorna a lista de funções que chamaram este
utils.console.getStackTrace = function () {
function fn2(f) {return !f ? [] : fn2(f).concat(f.caller);}
return fn2(arguments.callee.caller);
}
//função que inclue um erro para consulta de stack trace
utils.console._getCaller = function (args) {
//try { args = [arguments.callee.caller.caller.caller.caller].concat(args); } catch (e) {}
//funciona bem no firefox e não tão bem com o Chrome
return args;
};
utils.console._all = function (fn, args){
try {
var varArgs = [fn + ':'];
varArgs = varArgs.concat(Array.prototype.slice.call(args, 0));
try {
console[fn].apply(window.console, utils.console._getCaller(varArgs));
} catch (e) {
//tenta de novo para o chrome
window.console && console[fn] && console[fn](utils.console._getCaller(varArgs));
}
} catch (e) {}
}
//função que efetiva o log
utils.console._log = function (fn, args) {
if ( //existe o logger?
window.console && console[fn] &&
//o nível que esta habilitado o logger?
utils.console.logLevel.CURRENT >= utils.console.logLevel[fn.toUpperCase()]) {
utils.console._all(fn, args);
}
};
utils.console.all = function () { (function(args){utils.console._all('log', args);})(arguments); };
utils.console.trace = function () { utils.console._log('trace', arguments); };
utils.console.log = function () { utils.console._log('log', arguments); };
utils.console.info = function () { utils.console._log('info', arguments); };
utils.console.debug = function () { utils.console._log('debug', arguments); };
utils.console.warn = function () { utils.console._log('warn', arguments); };
utils.console.error = function () { utils.console._log('error', arguments); };
//definição básica do console
utils = utils || {};
//Se você deseja algum resultado inclua o utils.console no teu script
utils.console = utils.console || {
logLevel: {
ALL: 7,
TRACE: 6,
LOG: 5,
DEBUG: 4,
INFO: 3,
WARN: 2,
ERROR: 1,
NONE: 0,
CURRENT: 2
},
getStackTrace: function(){},
info: function () { },
log: function () { },
debug: function () { },
error: function () { },
warn: function () { },
trace: function () { }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment