pinalbhatt
3/25/2015 - 2:33 PM

AngularJS Logger Factory

AngularJS Logger Factory

/*

*/
(function () {
    'use strict';

    angular.module('PBDesk.Logger', ['PBDesk.Toastr'])
    	.factory('Logger', logger);
    	
    logger.$inject = ['$log', 'Toastr'];
    
    function logger($log, toastr) {
        var service = {
            showToasts: true,

            error: error,
            info: info,
            success: success,
            warning: warning,

            // straight to console; bypass toastr
            log: $log.log
        };

        return service;
        /////////////////////

        function error(message, data, title) {
            $log.error('Error: ' + message, data);
            if (service.showToasts) {
                toastr.error(message, title);
            }
            
        }

        function info(message, data, title) {
            $log.info('Info: ' + message, data);
            if (service.showToasts) {
                toastr.info(message, title);
            }
            
        }

        function success(message, data, title) {
            $log.info('Success: ' + message, data);
            if (service.showToasts) {
                toastr.success(message, title);
            }
            
        }

        function warning(message, data, title) {
            $log.warn('Warning: ' + message, data);
            if (service.showToasts) {
                toastr.warning(message, title);
            }
            
        }
    }
})();
/*

*/
(function () {
    'use strict';

    angular.module('PBDesk.Logger', [])
    	.factory('Logger', logger);
    	
    logger.$inject = ['$log'];
    
    function logger($log) {
        var service = {
            showToasts: false,

            error: error,
            info: info,
            success: success,
            warning: warning,

            // straight to console; bypass toastr
            log: $log.log
        };

        return service;
        /////////////////////

        function error(message, data, title) {
            $log.error('Error: ' + message, data);
            
        }

        function info(message, data, title) {
            $log.info('Info: ' + message, data);
            
        }

        function success(message, data, title) {
            $log.info('Success: ' + message, data);
            
        }

        function warning(message, data, title) {
            $log.warn('Warning: ' + message, data);
            
        }
    }
})();
(function () {
    'use strict';

    angular
        .module('PBDesk.Logger')
        .controller('LoggerTestController', LoggerTestController);

    LoggerTestController.$inject = ['$scope', 'Logger'];

    function LoggerTestController($scope, Logger) {
    	$scope.title = 'Toastr';

    	$scope.pop = function () {
    		Logger.info('msg', 'ttl');
    	}
    }
 
})();