IT박스

AngularJS에서 $ log.debug를 켜고 끄는 방법

itboxs 2020. 10. 14. 07:39
반응형

AngularJS에서 $ log.debug를 켜고 끄는 방법


$ log.debug ( "Foo")를 사용하려고합니다. 어떻게 끌 수 있습니까? 어디에서도 샘플을 찾을 수 없습니다. 구성에서 설정해야한다고 생각하지만 작동하지 않는 것 같습니다.

켜고 끄는 스위치를 어디에 설정합니까?


$ logProvider.debugEnabled (true)

AngularJs 1.1.2 이상에서만 사용할 수 있습니다.

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

다음은 설정되는 예입니다.

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

기본적으로 켜져 있습니다.


데코레이터로 기본 $ log 동작을 재정 의하여 로그 수준을 향상시킬 수 있습니다. 다음은 예입니다.

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;
    });
});

이것은 http://www.thekuroko.com/using-angulars-log-provider/ 에서 John Crosby의 작업에서 영감을 받았습니다 .


나는 같은 문제에 직면했지만 코딩으로 해결할 문제가 아니라 브라우저 콘솔에서 활성화하십시오.

Go to console of browser and set level to verbose


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;
});

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.

참고URL : https://stackoverflow.com/questions/15561853/how-to-turn-on-off-log-debug-in-angularjs

반응형