UNPKG

5.08 kBJavaScriptView Raw
1import { Level } from './level';
2import { Display } from './display';
3import { Helpers } from './helpers';
4export class Logger {
5 constructor(name, color, developmentMode, allowed, isMuted, fixedWidth) {
6 this.name = name;
7 this.color = color;
8 this.developmentMode = developmentMode;
9 this.allowed = allowed;
10 this.isMuted = isMuted;
11 this.fixedWidth = fixedWidth;
12 /** @deprecated Use data(...)
13 * @see data
14 */
15 this.d = (name, ...data) => this._data(name, data);
16 /** @deprecated Use error(...)
17 * @see error
18 */
19 this.er = (name, ...data) => this._error(name, data);
20 /** @deprecated Use info(...)
21 * @see info
22 */
23 this.i = (name, ...data) => this._info(name, data);
24 /** @deprecated Use warn(...)
25 * @see warn
26 */
27 this.w = (name, ...data) => this._warn(name, data);
28 /**
29 * Logs message and data with the level=data
30 * @param message The message
31 * @param otherParams Additional parameters
32 */
33 this.data = (message, ...otherParams) => { return this._data(message, otherParams); };
34 /**
35 * Logs message and data with the level=error
36 * @param message The message
37 * @param otherParams Additional parameters
38 */
39 this.error = (message, ...otherParams) => this._error(message, otherParams);
40 /**
41 * Logs message and data with the level=info
42 * @param message The message
43 * @param otherParams Additional parameters
44 */
45 this.info = (message, ...otherParams) => this._info(message, otherParams);
46 /**
47 * Logs message and data with the level=warn
48 * @param message The message
49 * @param otherParams Additional parameters
50 */
51 this.warn = (message, ...otherParams) => this._warn(message, otherParams);
52 }
53 setLevel(l) {
54 this._level = l;
55 return this;
56 }
57 get isProductionMode() {
58 return !this.developmentMode;
59 }
60 setProductionMode(productionMode) {
61 this.developmentMode = !productionMode;
62 return this;
63 }
64 mute() {
65 this.isMuted = true;
66 return this;
67 }
68 onlyWhen(expression) {
69 if (typeof expression === 'function') {
70 this.isMuted = !expression();
71 }
72 else if (typeof expression === 'boolean') {
73 this.isMuted = !expression;
74 }
75 }
76 _data(name, ...data) {
77 if (this.isMuted)
78 return this;
79 if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING)
80 && !Helpers.contain(this.allowed, Level.DATA))
81 return this;
82 if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.DATA)) {
83 Display.msg.apply(void 0, [
84 name,
85 ...data,
86 this.name,
87 this.color,
88 Level.DATA,
89 this.fixedWidth,
90 this.isProductionMode,
91 ]);
92 }
93 return this;
94 }
95 _error(name, ...data) {
96 if (this.isMuted)
97 return this;
98 if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING)
99 && !Helpers.contain(this.allowed, Level.ERROR))
100 return this;
101 if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.ERROR)) {
102 Display.msg.apply(void 0, [
103 name,
104 ...data,
105 this.name,
106 this.color,
107 Level.ERROR,
108 this.fixedWidth,
109 this.isProductionMode,
110 ]);
111 }
112 return this;
113 }
114 _info(name, ...data) {
115 if (this.isMuted)
116 return this;
117 if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING)
118 && !Helpers.contain(this.allowed, Level.INFO))
119 return this;
120 if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.INFO)) {
121 Display.msg.apply(void 0, [
122 name,
123 ...data,
124 this.name,
125 this.color,
126 Level.INFO,
127 this.fixedWidth,
128 this.isProductionMode,
129 ]);
130 }
131 return this;
132 }
133 _warn(name, ...data) {
134 if (this.isMuted)
135 return this;
136 if (this.allowed.length >= 1 && Helpers.contain(this.allowed, Level.__NOTHING)
137 && !Helpers.contain(this.allowed, Level.WARN))
138 return this;
139 if (this.allowed.length === 0 || Helpers.contain(this.allowed, Level.WARN)) {
140 Display.msg.apply(void 0, [
141 name,
142 ...data,
143 this.name,
144 this.color,
145 Level.WARN,
146 this.fixedWidth,
147 this.isProductionMode,
148 ]);
149 }
150 return this;
151 }
152}
153//# sourceMappingURL=logger.js.map
\No newline at end of file