在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");
                };

            }])
})();
皮特·斯塔普

您可以“覆盖”如下所示的日志记录方法(在此全文):

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/以了解如何创建完整的日志记录提供程序,并且可以在运行时进行配置

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章