-
Notifications
You must be signed in to change notification settings - Fork 1
/
etools-logging.js
60 lines (51 loc) · 1.6 KB
/
etools-logging.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Available log levels:
* OFF
* ERROR
* WARN
* INFO
*/
let etoolsLogsLevel = '';
function _getLogLevel() {
const availableLogLevels = ['OFF', 'ERROR', 'WARN', 'INFO'];
if (window.EtoolsLogsLevel && availableLogLevels.indexOf(window.EtoolsLogsLevel) === -1) {
// wrong log level set
return 'OFF';
}
return window.EtoolsLogsLevel || 'OFF';
}
function _initAndGetLogLevel(forceLevelInit) {
if (!etoolsLogsLevel || forceLevelInit) {
etoolsLogsLevel = _getLogLevel();
}
return etoolsLogsLevel;
}
function _getEtoolsLogMessages(logLevel, message, messagePrefix) {
let msg = '[' + logLevel + ']';
if (messagePrefix) {
msg += '[' + messagePrefix.toString() + '] ';
}
msg += message;
return msg;
}
function _canLog(levels, forceLevelInit) {
return levels.indexOf(_initAndGetLogLevel(forceLevelInit)) > -1;
}
export function logError(message, messagePrefix, other, forceLevelInit) {
if (_canLog(['ERROR', 'WARN', 'INFO'], forceLevelInit)) {
// eslint-disable-next-line
console.error(_getEtoolsLogMessages('ERROR', message, messagePrefix), other ? other : '');
}
}
export function logWarn(message, messagePrefix, other, forceLevelInit) {
if (_canLog(['WARN', 'INFO'], forceLevelInit)) {
// eslint-disable-next-line
console.warn(_getEtoolsLogMessages('WARN', message, messagePrefix), other ? other : '');
}
}
export function logInfo(message, messagePrefix, other, forceLevelInit) {
if (_canLog(['INFO'], forceLevelInit)) {
// eslint-disable-next-line
console.log(_getEtoolsLogMessages('INFO', message, messagePrefix), other ? other : '');
}
}