Last active
September 13, 2015 17:57
-
-
Save smorcuend/00760b3497717d1829dc to your computer and use it in GitHub Desktop.
AngularJS - Disable logging
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
(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'); | |
}]); | |
}); |
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
(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