Why use angular $ log instead of console.log?

I understand that in angular it is better to use $log instead of console.log . However, I cannot find good documentation explaining the reasons. Why should a developer use $log ?

+59
angularjs logging
Jun 12 '14 at 1:35
source share
2 answers

$log first checks to see if the browser supports console.log (e.g. IE 8). This prevents errors in IE 8. Note: this does not mean that it will write anything in IE 8, it just means that it will not throw an error.

In addition, it also allows you to decorate and layout $log for expansion and testing, if you are so inclined. You could, for example, decorate it to enter an array to support IE 8.

Bonus feature: if you pass it a JavaScript Error instance, it will try to format it nicely. You can find this out by reading the source code .

EDIT: "It's not that IE 8 does not support console.log. It just does not create a console object until the dev tools are open." See comments below for more details.

+73
Jun 12 '14 at 13:40
source share

To complete the @Steve answer (which is correct), $log also has the advantage of disabling . Using this code, you can disable logging from $log :

 app.config(function($logProvider) { $logProvider.debugEnabled(true); }); 

This is very convenient if you want to disable all logs at the same time, rather than manually deleting them line by line.

+4
Apr 24 '17 at 14:10
source share



All Articles