-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathetools-logs-mixin.js
97 lines (87 loc) · 2.9 KB
/
etools-logs-mixin.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import {dedupingMixin} from '@polymer/polymer/lib/utils/mixin.js';
/**
* Can be used to log errors, warnings and other info like this.
*
* Each method can have 3 args:
* - the message you want to log
* - a prefix for log message
* - additional data, anything
*
* ## Available logging levels (the levels are cumulative)
* - OFF (default)
* - ERROR - only errors will be displayed
* - WARN - errors and warning will be displayed
* - INFO - errors, warning and info logs displayed
*
* To configure the logging level in your app config do this:
* window.EtoolsLogsLevel = window.EtoolsLogsLevel || 'INFO';
*
* @polymer
* @mixinFunction
* @demo demo/index.html
*/
const EtoolsLogsMixin = dedupingMixin(
(baseClass) =>
class extends baseClass {
static get properties() {
return {
/**
* Available log levels:
* OFF
* ERROR
* WARN
* INFO
*/
etoolsLogsLevel: {
type: String,
value: ''
}
};
}
_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';
}
_initAndGetLogLevel(forceLevelInit) {
if (!this.etoolsLogsLevel || forceLevelInit) {
this.etoolsLogsLevel = this._getLogLevel();
}
return this.etoolsLogsLevel;
}
_getEtoolsLogMessages(logPrefix, message, messagePrefix) {
const currentElementName = this.is ? '[' + this.is + ']' : '';
let msg = '[' + logPrefix + ']' + currentElementName;
if (messagePrefix) {
msg += '[' + messagePrefix.toString() + '] ';
}
msg += message;
return msg;
}
_canLog(levels, forceLevelInit) {
return levels.indexOf(this._initAndGetLogLevel(forceLevelInit)) > -1;
}
logError(message, messagePrefix, other, forceLevelInit) {
if (this._canLog(['ERROR', 'WARN', 'INFO'], forceLevelInit)) {
// eslint-disable-next-line
console.error(this._getEtoolsLogMessages('ERROR', message, messagePrefix), other ? other : '');
}
}
logWarn(message, messagePrefix, other, forceLevelInit) {
if (this._canLog(['WARN', 'INFO'], forceLevelInit)) {
// eslint-disable-next-line
console.warn(this._getEtoolsLogMessages('WARN', message, messagePrefix), other ? other : '');
}
}
logInfo(message, messagePrefix, other, forceLevelInit) {
if (this._canLog(['INFO'], forceLevelInit)) {
// eslint-disable-next-line
console.log(this._getEtoolsLogMessages('INFO', message, messagePrefix), other ? other : '');
}
}
}
);
export default EtoolsLogsMixin;