6

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

            }])
})();
2
  • That's only supposed to turn off $log.debug(). Are those still showing up? Commented Aug 27, 2014 at 20:00
  • 1
    Yes, still shows up. What can I be doing wrong? I edited to add the way I'm calling the logs. Commented Aug 28, 2014 at 8:21

3 Answers 3

6

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

Sign up to request clarification or add additional context in comments.

3 Comments

But I don't want to edit the way the logs are showing up. I just want to disable all the logs...
If you remove line or add if statement to origDebug.apply(null, args) nothing will be shown. Same with other methods. This way you can easy turn on/off login on page using global variable
This is the best way to control your logs. You can easy turn it on/off control level of logging etc. BTW if you think that it answer your question mark it as answer, if you think it is useful vote it up
2

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.

Comments

0

debugEnabled function should disable only $log.debug() messages. 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.

You can see an example here http://jsfiddle.net/jccrosby/N2B6R/light/

1 Comment

With me $log.debug continues to showing up... However, what is the best way to disable all the different logs? I want to keep the others because I like the fancy way they show on the console.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.