Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AngularJS: What's the difference between $log and console.log?

I was going through the angularJs doc and came to know about log, warn, error etc. Now to see the output we need to open the console, so my question are

if already console.log() is there to see the errors in console, then what is the use of $log, where is the place/scenerio that I must involve the use of $log in my angularJs application.

How can I make use of $log to store information in file about my logging activities.

like image 334
shreyansh Avatar asked Oct 18 '22 15:10

shreyansh


1 Answers

Well, basic difference is $log is the AngularJs implementation of logging and a lot more than just spitting bits of strings and data to console, whereas console.log() is basic java script.

You pass $log as a service to your controllers, factories and other services. The plus about using $log is that you can extend and customize it.

For starters, I really like this for when I have to check what controller loaded and where things failed in a big application I can do so, by $log.info():

myApp.config(['$stateProvider', '$urlRouterProvider', '$logProvider', function ($stateProvider, $urlRouterProvider, $logProvider)
{
    $urlRouterProvider.otherwise("/home");
    $logProvider.debugEnabled(isDebugMode);
}]);

See the $logProvider that enables or disables logging depending on environment.

A lot more on extending the existing logger:

  • Here

  • And Here

like image 120
Alok Avatar answered Oct 21 '22 07:10

Alok