简体   繁体   English

禁用AngularJS上的日志记录

[英]Disable Logging on AngularJS

I'm using in my code the angularJS service for logging ($log.error(), $log.debug(), $log.info(), etc) and it works fine. 我在我的代码中使用angularJS服务进行日志记录($ log.error(),$ log.debug(),$ log.info()等),它工作正常。

Now, I'm trying to disable all the logs. 现在,我正在尝试禁用所有日志。 I already tried this: 我已经尝试过了:

var app = angular.module('app', []);

app.config(
    ['$logProvider',
    function ($logProvider) {
        $logProvider.debugEnabled(false);
    }]
);

But this does nothing, the logs continues to show... 但这没有任何作用,日志继续显示......

What is the best way to disable all the angularJS logs that I put in my code? 禁用我放入代码中的所有angularJS日志的最佳方法是什么?

EDIT: 编辑:

I'm calling the logs like this: 我正在调用这样的日志:

(function () {
    app.controller('MyController', ['$log',

            function($log) {
                 this.testFunction = function() {
                    $log.debug("debug");
                    $log.info("info");
                    $log.error("error");
                };

            }])
})();

You can "override" logging methods like below ( 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;
    }]);

You should also read http://blog.projectnibble.org/2013/12/23/enhance-logging-in-angularjs-the-simple-way/ to see how to create full logging provider wich you can configure on fly 您还应该阅读http://blog.projectnibble.org/2013/12/23/enhance-logging-in-angularjs-the-simple-way/以了解如何创建可以在飞行中配置的完整日志记录提供程序

Here is my two cents: 这是我的两分钱:

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;
}]);

Just set boolean and done. 只需设置布尔值并完成。

debugEnabled function should disable only $log.debug() messages. debugEnabled函数应仅禁用$log.debug()消息。 So if you want to disable logging with simple config as you have written, then rename all your debug calls to $log.debug , not to $log.log or $log.error or $log.info or $log.whatever . 因此,如果要在编写时使用简单config禁用日志记录,则将所有调试调用重命名为$log.debug ,而不是$log.log$log.error$log.info$log.whatever

You can see an example here http://jsfiddle.net/jccrosby/N2B6R/light/ 你可以在这里看到一个例子http://jsfiddle.net/jccrosby/N2B6R/light/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM