How to enable / disable $ log.debug in AngularJS

I am trying to use $ log.debug ("Foo"). How to disable it. I can’t find a sample anywhere. I think it needs to be installed in config, but I cannot get it to work.

Where to set the on and off switch?

+69
angularjs logging angularjs-log
Mar 22 '13 at 2:44
source share
4 answers

$ 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 how to install it.

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 enabled.

+126
Mar 23 '13 at 1:08
source share

You can override the default behavior of $ log with a decorator to increase the log level. That's 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 by the work of John Crosby at http://www.thekuroko.com/using-angulars-log-provider/

+13
Sep 10 '15 at 16:23
source share

I came across the same problem, but this is not a problem that needs to be solved by encoding, but simply enable it from the browser console.

Go to the browser console and set the level to detailed

+3
Jun 28 '17 at 15:24
source share

Based on Diego's answer, but adding some env configuration and making it shorter. You can simply launch your application using: NODE_ENV=development or NODE_ENV=production

Eg1. NODE_ENV=development webpack-dev-server

Eg2. NODE_ENV=production node app.js

 $logProvider.debugEnabled(process.env.NODE_ENV === 'development'); $provide.decorator('$log', function($delegate) { $delegate.info = $logProvider.debugEnabled() ? $delegate.info : function() {}; $delegate.log = $logProvider.debugEnabled() ? $delegate.log : function() {}; return $delegate; }); 
+1
Feb 17 '17 at 6:23
source share



All Articles