Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
AngularJS - Disable logging
(function() {
var app = angular.module('myApp', []);
app.config(['$provide', function($provide) {
// decorates the $log instance to disable logging
$provide.decorator('$log', ['$delegate',
function($delegate) {
var $log, enabled = true;
$log = {
debugEnabled: function(flag) {
enabled = !!flag;
}
};
// methods implemented by Angular's $log service
['log', 'warn', 'info', 'error'].forEach(function(methodName) {
$log[methodName] = function() {
if (!enabled) return;
var logger = $delegate;
logger[methodName].apply(logger, arguments);
}
});
return $log;
}
]);
}]);
// AngularJS 1.0.x series
app.run(['$log', function($log) {
$log.debugEnabled(false);
}]);
app.controller('LoggyController', ['$log', function($log) {
// will do nothing as well
$log.log('LOLz');
}]);
});
(function() {
var app = angular.module('myApp', []);
// AngularJS 1.1.x series
app.config(['$logProvider', function($logProvider) {
$logProvider.debugEnabled(false);
}]);
app.controller('LoggyController', ['$log', function($log) {
// will do nothing
$log.log('LOLz');
}]);
}());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment