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

我是研究僧i 提交于 2019-11-27 09:43:17

问题


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?


回答1:


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




回答2:


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/




回答3:


I have faced the same issue but it's not an issue to be solved by coding instead just enable it from browser console

Go to console of browser and set level to verbose




回答4:


Based on Diego's response but adding some env config and making it shorter. You can simply run your app using: NODE_ENV=development or NODE_ENV=production

E.g.1. NODE_ENV=development webpack-dev-server

E.g.2. 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;
});



回答5:


Well, Since the solution requires us to turn on Verbose flag, I think the best way to handle logging in angular would be to simply alter the native console.log function in Production Environment for the entire application.

angular.module("myModule")
.config(function(){
    //if production environment
    console.log = ()=>{};
})

That's it. In production environment, this should disable logging everywhere. Also there is no need to inject $log in every controller now. Simply console.log("logging message") works!

You can also disable console.info, console.warn, console.error and console.debug the same way as per your need.



来源:https://stackoverflow.com/questions/15561853/how-to-turn-on-off-log-debug-in-angularjs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!