Last active
October 11, 2015 20:58
-
-
Save Wizek/3918660 to your computer and use it in GitHub Desktop.
Superpowered logging for AngularJS
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
// Superpowered logging for AngularJS. | |
angular.module('logFactory', ['ng']) | |
.value('logFactory_whiteList', /.*/) | |
//.value('logFactory_whiteList', /!|.*Ctrl|run/) | |
.value('logFactory_piercingMethods', {warn:true, error:true}) | |
.factory('logFactory', ['$log', 'logFactory_whiteList' , 'logFactory_piercingMethods', function ($log, whiteList, piercing) { | |
piercing = piercing || {} | |
whiteList = whiteList || /.*/ | |
return function (prefix, parentLog) { | |
var log = parentLog || $log | |
var match = prefix.match(whiteList) | |
function e(fnName) { | |
if (!log[fnName]) { | |
fnName = 'log' | |
} | |
return (piercing[fnName] || match) | |
? log[fnName].bind(log, '[' + prefix + ']') | |
: angular.noop | |
} | |
return ( | |
{ debug: e('debug') | |
, info: e('info') | |
, log: e('log') | |
, warn: e('warn') | |
, error: e('error') | |
} | |
) | |
} | |
}]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage:
Which outputs: