Skip to content

Instantly share code, notes, and snippets.

@smorcuend
Last active September 13, 2015 17:57
Show Gist options
  • Save smorcuend/00760b3497717d1829dc to your computer and use it in GitHub Desktop.
Save smorcuend/00760b3497717d1829dc to your computer and use it in GitHub Desktop.
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