Extending the AngularJS $logProvider

Add your own to the $logProvider.

by sundaramkumar

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>

JavaScript

angular.module('app', [])

.config(['$provide', function ($provide) {
    $provide.decorator('$log', ['$delegate', function ($delegate) {
        // Keep track of the original debug method, we'll need it later.
        var origDebug = $delegate.debug;
        /*
         * Intercept the call to $log.debug() so we can add on 
         * our enhancement. We're going to add on a date and 
         * time stamp to the message that will be logged.
         */
        $delegate.debug = function () {
            var args = [].slice.call(arguments);
            args[0] = [new Date().toString(), ': ', args[0]].join('');
            
            // Send on our enhanced message to the original debug method.
            origDebug.apply(null, args)
        };

        return $delegate;
    }]);
}])

.controller('MainCtrl', ['$log', function ($log) {
    $log.debug('Hello Debug!');
}]);