Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to turn on/off $log.debug in AngularJS

I'm trying to use $log.debug("Foo"). How can it be turned on off. I can't find a sample anywhere. I think it need to be set in the config, but I cant seem to get that to work either.

Where does one set the on and off switch?

like image 992
Eric Rohlfs Avatar asked Mar 22 '13 02:03

Eric Rohlfs


People also ask

How do I disable debug log?

In your case you must set logging. level. root on one of level from INFO, WARN, ERROR,FATAL or OFF to turn off all logging.

What is toggle debug logging?

Debug logging is a troubleshooting process that gathers a large amount of information and system logs to help find problems. We recommend only enabling this for a short time, as the log files can become very large on the end device.

How are logs maintained in AngularJS?

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

What is rootScope in AngularJS?

All applications have a $rootScope which is the scope created on the HTML element that contains the ng-app directive. The rootScope is available in the entire application. If a variable has the same name in both the current scope and in the rootScope, the application uses the one in the current scope.


2 Answers

You can override the default $log behavior with a decorator to enhace the log level. This is an example:

angular.module('app').config(function($logProvider, $provide){      $logProvider.debugEnabled(false);      $provide.decorator('$log', function ($delegate) {         //Original methods         var origInfo = $delegate.info;         var origLog = $delegate.log;          //Override the default behavior         $delegate.info = function () {              if ($logProvider.debugEnabled())                 origInfo.apply(null, arguments)         };          //Override the default behavior             $delegate.log = function () {              if ($logProvider.debugEnabled())                 origLog.apply(null, arguments)         };          return $delegate;     }); }); 

This was inspired from the work of John Crosby at http://www.thekuroko.com/using-angulars-log-provider/

like image 43
Diego Avatar answered Sep 22 '22 16:09

Diego


$logProvider.debugEnabled(true)

This is only available in AngularJs 1.1.2 or later.

https://github.com/angular/angular.js/pull/1625

Here is an example of it being set.

var app = angular.module('plunker', []);  app.config(function($logProvider){   $logProvider.debugEnabled(true); });  app.controller('MainCtrl', function($scope, $log ) {   $scope.name = 'World';   $scope.model = {value: "test"};   $log.debug('TEST Log'); }); 

http://plnkr.co/edit/HZCAoS?p=preview

By default it is on.

like image 191
rgaskill Avatar answered Sep 18 '22 16:09

rgaskill