(function () { 'use strict'; angular.module('common').factory('logger', ['$log', logger]); //service to log messages function logger($log) { var service = { getLogFn: getLogFn, log: log, logError: logError, logSuccess: logSuccess, logWarning: logWarning }; return service; function getLogFn(moduleId, fnName) { fnName = fnName || 'log'; switch (fnName.toLowerCase()) { // convert aliases case 'success': fnName = 'logSuccess'; break; case 'error': fnName = 'logError'; break; case 'warn': fnName = 'logWarning'; break; case 'warning': fnName = 'logWarning'; break; } var logFn = service[fnName] || service.log; return function (msg, data, showToast) { logFn(msg, data, moduleId, (showToast === undefined) ? true : showToast); }; } function log(message, data, source, showToast) { logIt(message, data, source, showToast, 'info'); } function logWarning(message, data, source, showToast) { logIt(message, data, source, showToast, 'warning'); } function logSuccess(message, data, source, showToast) { logIt(message, data, source, showToast, 'success'); } function logError(message, data, source, showToast) { logIt(message, data, source, showToast, 'error'); } function logIt(message, data, source, showToast, toastType) { var successTerms = ['Success!', 'Hoorah!', 'Voila!', 'Hooray!', 'Woo-hoo!', 'Wahey!', 'Yippie!']; if (data && data.exception && isAcceptableException(data.exception)) return; var write = (toastType === 'error') ? $log.error : $log.log; source = source ? '[' + source + '] ' : ''; write(source, message, data); var f = '
'; } if (showToast) { if (toastType === 'error' && message.indexOf('$location:ihshprfx') === -1) toastr.error(f + getTitle('Oops, something went wrong...') + r); else if (toastType === 'warning') toastr.warning(f + getTitle('Warning') + message + r); else if (toastType === 'success') toastr.success(f + getTitle(successTerms[Math.floor((Math.random() * successTerms.length))]) + message + r); else if (toastr.options.debug) toastr.info(f + message + r); } } //some errors happen in IE and have no side effect on the app - we can hide those from the log function isAcceptableException(exception) { if (exception.codeName && exception.codeName === 'INDEX_SIZE_ERR') //cannot work out why this error happens in IE, it doesn't have any side effects so hide the error return true; if (exception.message && exception.message === 'Invalid argument.') return true; return false; } } })();