简体   繁体   中英

In AngularJS, what is the difference between console.log and $log.info?

I have seen both used, and I do not see any difference in their output. Is there an advantage to one over the other, besides console.log not requiring injection of $log?

var url = "http://www.google.com";
console.log(url);
$log.info(url);

Both statements return the string to the console window. If url were an object, both would return the properties of that object to the console window.

The main purpose of the ng.$log service is so you have a stable implementation (no differences in availability across browsers/node) and so you can switch off all debugging output using $logProvider (eg in a production build or during unit tests). It is also conceivable that you inject an alternative logger implementation which sends your logs to an aggregation service, or something along those lines. The default $log service acts exactly like console .

Basically: console.log == hardcoded function, $log == overridable service

$log is a simple service for logging. Default implementation safely writes the message to the browser's console (if present).

To remove console.log logs you have to comment the console.log statements. But if you are using $log service you can disable the logs

   var app = angular.module('testModule', []);

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

By default debugEnabled is set to true.

Reference

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM