Skip to content

Instantly share code, notes, and snippets.

@pinalbhatt
Last active October 2, 2015 09:37
Show Gist options
  • Save pinalbhatt/3175f18b6d8024110e69 to your computer and use it in GitHub Desktop.
Save pinalbhatt/3175f18b6d8024110e69 to your computer and use it in GitHub Desktop.
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')
.controller('LoggerTestController', LoggerTestController);
LoggerTestController.$inject = ['$scope', 'Logger'];
function LoggerTestController($scope, Logger) {
$scope.title = 'Toastr';
$scope.pop = function () {
Logger.info('msg', 'ttl');
}
}
})();
/*
*/
(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);
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment