Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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?

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

            }])
})();
like image 635
vale Avatar asked Aug 27 '14 19:08

vale


People also ask

What is logging in AngularJS?

AngularJs includes logging service $log, which logs the messages to the browser's console. The $log service includes different methods to log the error, information, warning or debug information. It can be useful in debugging and auditing.

How do I check angular logs?

You should be able to run the Angular application and click on the Log Test button and see the log messages published to all publishers marked as isActive in the JSON file.


1 Answers

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

like image 170
Piotr Stapp Avatar answered Sep 30 '22 16:09

Piotr Stapp