javascript - 在 AngularJS 上禁用日志记录

标签 javascript angularjs

我在我的代码中使用 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/

相关文章:

javascript - 单选按钮不会对 ng-change 指令使用react

AngularJs ui-router 重载模板

javascript - 如何确保 Fetch API 中的数组已填充?

javascript - 如何在对象数组中设置每个对象的属性?

javascript - AngularJS - 在指令中绑定(bind)多个类

angularjs - Angular Js+ typescript : How to create a Dynamic table

angularjs - 始终在 "Access-Control-Request-Headers"下添加自定义 HTTP header

javascript - 在 Angular 单元测试中应该如何处理运行 block ?

javascript - AngularJS 表达式不起作用

javascript - 函数声明和变量声明的顶部是否有助于提高性能?