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

81 lines
2.4 KiB
JavaScript
Raw Normal View History

2016-11-06 15:41:28 +01:00
(function () {
'use strict';
2018-08-12 10:47:07 +02:00
angular.module('ariaNg').factory('ariaNgLogService', ['$log', 'ariaNgConstants', function ($log, ariaNgConstants) {
2018-08-12 10:22:59 +02:00
var enableDebugLog = false;
2018-04-05 18:58:14 +02:00
var cachedDebugLogs = [];
var createNewCacheLogItem = function (msg, level, obj) {
return {
2018-08-12 10:47:07 +02:00
time: new Date(),
2018-04-05 18:58:14 +02:00
level: level,
content: msg,
attachment: obj
};
};
var pushLogToCache = function (msg, level, obj) {
2018-08-12 10:22:59 +02:00
if (!enableDebugLog) {
2018-04-05 18:58:14 +02:00
return;
}
if (cachedDebugLogs.length >= ariaNgConstants.cachedDebugLogsLimit) {
cachedDebugLogs.shift();
}
cachedDebugLogs.push(createNewCacheLogItem(msg, level, obj));
};
2016-11-06 15:41:28 +01:00
return {
2018-08-12 10:22:59 +02:00
setEnableDebugLog: function (value) {
enableDebugLog = value;
},
2016-11-06 15:41:28 +01:00
debug: function (msg, obj) {
2018-08-12 10:22:59 +02:00
if (enableDebugLog) {
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 () {
2018-08-12 10:22:59 +02:00
if (enableDebugLog) {
2018-04-05 18:58:14 +02:00
return cachedDebugLogs;
} else {
return [];
}
2016-11-06 15:41:28 +01:00
}
};
}]);
}());