mperdeck/JSNLog.AngularJS

more elegant way to implement this

Closed this issue · 2 comments

Hi

Here is more convenient to implement this

http://vinnylinck.tumblr.com/post/58833687265/angularjs-how-to-override-log-implementation

    angular.module('bk.jsnlog', [])
        .config(['$provide', function ($provide) {
            $provide.decorator('$log', ['$delegate', '$logShadowLogger', function ($delegate, $logShadowLogger) {
                return $logShadowLogger($delegate);
            }]);
        }])
        .factory('$logShadowLogger', function () {

            return function ($delegate) {

                return {
                    log: function () {
                        $delegate.log.apply($delegate, arguments);
                    },
                    info: function () {
                        $delegate.info.apply($delegate, arguments);
                    },
                    error: function () {
                        $delegate.error.apply($delegate, arguments);
                    },
                    warn: function () {
                        $delegate.warn.apply($delegate, arguments);
                    }
                };

            };
        });

Actually overriding the default $log implementation removes the original $logProvider's functionality, so for example you can't call $logProvider.debugEnabled().
So using a decorator is not only a more convenient way to implement it, but the current implementation is a breaking change.

This is too old, it was a year ago !