This repository has been archived on 2022-01-02. You can view files and clone it, but cannot push or open issues or pull requests.
AriaNg/src/scripts/services/ariaNgLogService.js

77 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-11-06 15:41:28 +01:00
(function () {
'use strict';
2018-04-05 18:58:14 +02:00
angular.module('ariaNg').factory('ariaNgLogService', ['$log', 'moment', 'ariaNgConstants', 'ariaNgSettingService', function ($log, moment, ariaNgConstants, ariaNgSettingService) {
var cachedDebugLogs = [];
var createNewCacheLogItem = function (msg, level, obj) {
return {
time: moment(),
level: level,
content: msg,
attachment: obj
};
};
var pushLogToCache = function (msg, level, obj) {
if (!ariaNgSettingService.isEnableDebugMode()) {
return;
}
if (cachedDebugLogs.length >= ariaNgConstants.cachedDebugLogsLimit) {
cachedDebugLogs.shift();
}
cachedDebugLogs.push(createNewCacheLogItem(msg, level, obj));
};
2016-11-06 15:41:28 +01:00
return {
debug: function (msg, obj) {
if (ariaNgSettingService.isEnableDebugMode()) {
2017-03-20 15:43:55 +01:00
if (obj) {
$log.debug('[AriaNg Debug]' + msg, obj);
} else {
$log.debug('[AriaNg Debug]' + msg);
}
2018-04-05 18:58:14 +02:00
pushLogToCache(msg, 'DEBUG', obj);
2016-11-06 15:41:28 +01:00
}
},
info: function (msg, obj) {
2017-03-20 15:43:55 +01:00
if (obj) {
$log.info('[AriaNg Info]' + msg, obj);
} else {
$log.info('[AriaNg Info]' + msg);
}
2018-04-05 18:58:14 +02:00
pushLogToCache(msg, 'INFO', obj);
2016-11-06 15:41:28 +01:00
},
warn: function (msg, obj) {
2017-03-20 15:43:55 +01:00
if (obj) {
$log.warn('[AriaNg Warn]' + msg, obj);
} else {
$log.warn('[AriaNg Warn]' + msg);
}
2018-04-05 18:58:14 +02:00
pushLogToCache(msg, 'WARN', obj);
2016-11-06 15:41:28 +01:00
},
error: function (msg, obj) {
2017-03-20 15:43:55 +01:00
if (obj) {
$log.error('[AriaNg Error]' + msg, obj);
} else {
$log.error('[AriaNg Error]' + msg);
}
2018-04-05 18:58:14 +02:00
pushLogToCache(msg, 'ERROR', obj);
},
getDebugLogs: function () {
if (ariaNgSettingService.isEnableDebugMode()) {
return cachedDebugLogs;
} else {
return [];
}
2016-11-06 15:41:28 +01:00
}
};
}]);
}());