我在我的代码中使用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");
};
}])
})();
答案 0 :(得分:6)
您可以“覆盖”下面的记录方法(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/以了解如何创建可以在飞行中配置的完整日志记录提供程序
答案 1 :(得分:1)
这是我的两分钱:
var IN_DEVELOPMENT = true;
$provide.decorator('$log', ['$delegate', function ($delegate)
{
var originals = {};
var methods = ['info' , 'debug' , 'warn' , 'error'];
angular.forEach(methods , function(method)
{
originals[method] = $delegate[method];
$delegate[method] = function()
{
if (IN_DEVELOPMENT) {
var args = [].slice.call(arguments);
var timestamp = new Date().toString();
args[0] = [timestamp.substring(4 , 24), ': ', args[0]].join('');
originals[method].apply(null , args);
}
};
});
return $delegate;
}]);
只需设置布尔值并完成。
答案 2 :(得分:0)
debugEnabled
函数应仅禁用$log.debug()
条消息。因此,如果您希望使用简单的config
禁用日志记录,则将所有调试调用重命名为$log.debug
,而不是$log.log
或$log.error
或{{1} }或$log.info
。