Sharing Top Content from the Angular-sphere.

AngularJS : why you should use the $log service instead of console.log()

  • $log is a simple service for logging purposes.
  • $log first checks if console.log is available.
  • Besides this the $log service makes it possible to easily to decorate or mock itself

    Also the $log service support a JavaScript Error instance.

  • With $log it will always show angular.js as the source of the log in your console.
  • or using a decorater on the $log service in combination with a module constant.

Creativity , Interactivity and Technology

@Creative_Geeks: AngularJS : why you should use the $log service instead of console.log() #angularjs #webdev #javascript

$log is a simple service for logging purposes. Default implementation safely writes the message into the browser’s console (if present). The main purpose of this service is to simplify debugging and troubleshooting.

$log first checks if console.log is available. In IE 8, for instance, the console object is created when the dev tools are opened, meaning when the dev tools is never opened – there will never be a console object available.

It also allows you to decorate and mock $log for extending and testing purposes, if you are so inclined. You could for example decorate it to log to an array for IE 8 support.

Using $log has a downside as well… If you use the console.log approach you can see the file and the line number of where the actual logging is invoked from. With $log it will always show angular.js as the source of the log in your console.

AngularJS : why you should use the $log service instead of console.log()

Comments are closed, but trackbacks and pingbacks are open.