Disable logging on AngularJS

I use the angularJS service in my code for logging ($ log.error (), $ log.debug (), $ log.info (), etc.) and it works fine.

Now I am trying to disable all logs. I already tried this:

var app = angular.module('app', []); app.config( ['$logProvider', function ($logProvider) { $logProvider.debugEnabled(false); }] ); 

But it does nothing, magazines keep showing ...

What is the best way to disable all angularJS logs that I entered in my code?

EDIT:

I call the logs as follows:

 (function () { app.controller('MyController', ['$log', function($log) { this.testFunction = function() { $log.debug("debug"); $log.info("info"); $log.error("error"); }; }]) })(); 
+5
source share
3 answers

You can override logging methods as shown below ( full post here ):

 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 learn how to create a complete logging provider that you can configure on the fly

+6
source

Here are 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 install boolean and do it.

+1
source

debugEnabled function should only disable $log.debug() messages. Therefore, if you want to disable logging with a simple config , as you wrote, rename all your debugging calls to $log.debug and not to $log.log or $log.error or $log.info or $log.whatever .

Here you can see an example http://jsfiddle.net/jccrosby/N2B6R/light/

0
source

Source: https://habr.com/ru/post/1201269/


All Articles