Extending the AngularJS $logProvider

Add your own to the $logProvider.

by gavinfoley

HTML

<div ng-app="app">
  <div ng-controller="MainCtrl">
    <h3>Make sure to open a javascript console so you can see the output.</h3>

  </div>
</div>

TypeScript

angular.module('app', [])

.config(['$provide', function($provide) {


  $provide.decorator('$log', ['$delegate', $delegate => {
    // Keep track of the original methods, we'll need them later.
    const logFuncs = ['info', 'warn', 'log', 'debug', 'error'];

    angular.forEach(logFuncs, (i) => {

      var origLogFunc = $delegate[i];

      $delegate[i] = function() {
        var args = [].slice.call(arguments);
        args[0] = [new Date().toLocaleTimeString(), ' - ', args[0]].join('');

        // Send on our enhanced message to the original log method.
        origLogFunc.apply(null, args);
      };


    });


    return $delegate;
  }]);




}])

.controller('MainCtrl', ['$log', function($log) {
  $log.debug('Hello Debug!', {test : 13123, "frrr": {asdasd: "test"}});
}]);