我在我的代码中使用 angularJS 服务进行日志记录($log.error()、$log.debug()、$log.info() 等),它工作正常。
现在,我正在尝试禁用所有日志。我已经试过了:
var app = angular.module('app', []);
app.config(
['$logProvider',
function ($logProvider) {
$logProvider.debugEnabled(false);
}]
);
但这没有任何作用,日志继续显示...
禁用我放入代码中的所有 angularJS 日志的最佳方法是什么?
编辑:
我这样调用日志:
(function () {
app.controller('MyController', ['$log',
function($log) {
this.testFunction = function() {
$log.debug("debug");
$log.info("info");
$log.error("error");
};
}])
})();
最佳答案
您可以“覆盖”如下所示的日志记录方法 ( here full post ):
angular.module('app', [])
.config(['$provide', function ($provide) {
$provide.decorator('$log', ['$delegate', function ($delegate) {
// Keep track of the original debug method, we'll need it later.
var origDebug = $delegate.debug;
/*
* Intercept the call to $log.debug() so we can add on
* our enhancement. We're going to add on a date and
* time stamp to the message that will be logged.
*/
$delegate.debug = function () {
var args = [].slice.call(arguments);
args[0] = [new Date().toString(), ': ', args[0]].join('');
// Send on our enhanced message to the original debug method.
origDebug.apply(null, args)
};
return $delegate;
}]);
您还应该阅读 http://blog.projectnibble.org/2013/12/23/enhance-logging-in-angularjs-the-simple-way/查看如何创建可以动态配置的完整日志记录提供程序
关于javascript - 在 AngularJS 上禁用日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25535628/