Include AngularJS module and controller names in $ log output?

I am exploring logging options and conventions in AngularJS. I would like my output to $logcontain names moduleand controller(similar to loggers in Log4j). With the code below, I was able to enter a name controllerin$scope

var app = angular.module("demo", []);

app.config(['$provide', function ($provide) {
    $provide.decorator('$controller', [
        '$delegate',
        function ($delegate) {
            return function(constructor, locals) {
                if (angular.isString(constructor)) {
                    locals.$scope.controllerName =  constructor;
                }
                return $delegate(constructor, locals);
            }
        }]);
}]);

app.controller('SampleCtrl', ['$scope', '$log', function ($scope, $log) {
    $log.log("[" + app.name + "." + $scope.controllerName +"] got here");
}]);

I would like to solve it better. In particular:

  • Is there a better way to access a name controllerthat adds it to $scope?
  • Is there a way to get the name moduleinstead of selecting "demo" from the .namemodule property app?
  • $log AngularJs , , , "[module. ]" $log.log()?
+4

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


All Articles