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?
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.
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.
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.
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.
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/
$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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With