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?
我已经看过两个用过了,我看不出它们的输出有什么不同。除了console.log不需要注入$ 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.
两个语句都将字符串返回到控制台窗口。如果url是一个对象,则两者都会将该对象的属性返回到控制台窗口。
2 个解决方案
#1
3
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
(e.g. 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
.
ng。$ log服务的主要目的是让您拥有稳定的实现(跨浏览器/节点的可用性没有差异),因此您可以使用$ logProvider关闭所有调试输出(例如,在生产版本中或在单元测试期间) 。您还可以考虑注入另一个记录器实现,该实现将您的日志发送到聚合服务,或者沿着这些行发送。默认的$ log服务与控制台完全相同。
Basically: console.log
== hardcoded function, $log
== overridable service
基本上:console.log ==硬编码功能,$ log == overridable service
#2
1
$log is a simple service for logging. Default implementation safely writes the message to the browser's console (if present).
$ log是一个简单的日志服务。默认实现安全地将消息写入浏览器的控制台(如果存在)。
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
要删除console.log日志,您必须注释console.log语句。但是,如果您使用的是$ log服务,则可以禁用日志
var app = angular.module('testModule', []);
app.config(function($logProvider){
$logProvider.debugEnabled(true);
});
By default debugEnabled is set to true.
默认情况下,debugEnabled设置为true。
参考
#1
3
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
(e.g. 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
.
ng。$ log服务的主要目的是让您拥有稳定的实现(跨浏览器/节点的可用性没有差异),因此您可以使用$ logProvider关闭所有调试输出(例如,在生产版本中或在单元测试期间) 。您还可以考虑注入另一个记录器实现,该实现将您的日志发送到聚合服务,或者沿着这些行发送。默认的$ log服务与控制台完全相同。
Basically: console.log
== hardcoded function, $log
== overridable service
基本上:console.log ==硬编码功能,$ log == overridable service
#2
1
$log is a simple service for logging. Default implementation safely writes the message to the browser's console (if present).
$ log是一个简单的日志服务。默认实现安全地将消息写入浏览器的控制台(如果存在)。
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
要删除console.log日志,您必须注释console.log语句。但是,如果您使用的是$ log服务,则可以禁用日志
var app = angular.module('testModule', []);
app.config(function($logProvider){
$logProvider.debugEnabled(true);
});
By default debugEnabled is set to true.
默认情况下,debugEnabled设置为true。
参考