0

I've recently started working on an application that uses, among other things, AngularJS. One task, which seemed somewhat simple at first, is turning out to be quite the headache.

We have a lot of console.log() in our JS code. I am to replace them with the using Angular's $log. I thought that it would be fairly straightforward, but it's not, probably because I am not familiar with Angular at all in the first place.

I've read the documentation, and the $log, $logProvider and $service pages in particular. I tried to apply the examples given to my code, to no avail.

From what I've understood from the app code (I just started working on it), we use a "main" module dedicated to configuration. It looks like this:

var mainApp = angular.module('mainApp'
['ui.router','datatables','ngMockE2E', 'ngResource','angularSpinner', 'ngCookies',
'pascalprecht.translate','ui.bootstrap', 'ngFileUpload', 'angular-jwt']);

mainApp.config(function($stateProvider, $urlRouterProvider, $httpProvider, $translateProvider) {

// Example of what we can find here
$translateProvider.useStaticFilesLoader(fileNameConvention);
$translateProvider.useSanitizeValueStrategy('escaped');

};

Each HTML file we have (or almost all) is linked to a controller, in which mainApp is referenced, like this:

mainApp.controller('ContractsController', ContractsController);

From what I've understood, to avoid calling log in each controller, I should inject it in mainApp. The thing is, I've tried several methods, none of them working (actually, I've even tried using $log directly in a controller, and it didn't work either).

If someone could be so kind as to point me in the right direction, I would really appreciate it.

1 Answer 1

1

Injecting $log in your controller should work. Did you see this example

 angular.module('app', [])

.controller('MainCtrl', ['$log', function($log){
    $log.debug('Hello Debug!');
}]);
Sign up to request clarification or add additional context in comments.

1 Comment

I got this to work! I thought I needed to create a module, add it to my mainApp and everything... I'll need to do this anyway to avoid repeating this code in every controller, but it's good to have something that works to go off.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.