1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 |
|
18 |
|
19 |
|
20 | const instances = [];
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 |
|
31 |
|
32 | var LogLevel;
|
33 | (function (LogLevel) {
|
34 | LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
|
35 | LogLevel[LogLevel["VERBOSE"] = 1] = "VERBOSE";
|
36 | LogLevel[LogLevel["INFO"] = 2] = "INFO";
|
37 | LogLevel[LogLevel["WARN"] = 3] = "WARN";
|
38 | LogLevel[LogLevel["ERROR"] = 4] = "ERROR";
|
39 | LogLevel[LogLevel["SILENT"] = 5] = "SILENT";
|
40 | })(LogLevel || (LogLevel = {}));
|
41 | const levelStringToEnum = {
|
42 | 'debug': LogLevel.DEBUG,
|
43 | 'verbose': LogLevel.VERBOSE,
|
44 | 'info': LogLevel.INFO,
|
45 | 'warn': LogLevel.WARN,
|
46 | 'error': LogLevel.ERROR,
|
47 | 'silent': LogLevel.SILENT
|
48 | };
|
49 |
|
50 |
|
51 |
|
52 | const defaultLogLevel = LogLevel.INFO;
|
53 |
|
54 |
|
55 |
|
56 |
|
57 |
|
58 |
|
59 | const ConsoleMethod = {
|
60 | [LogLevel.DEBUG]: 'log',
|
61 | [LogLevel.VERBOSE]: 'log',
|
62 | [LogLevel.INFO]: 'info',
|
63 | [LogLevel.WARN]: 'warn',
|
64 | [LogLevel.ERROR]: 'error'
|
65 | };
|
66 |
|
67 |
|
68 |
|
69 |
|
70 |
|
71 | const defaultLogHandler = (instance, logType, ...args) => {
|
72 | if (logType < instance.logLevel) {
|
73 | return;
|
74 | }
|
75 | const now = new Date().toISOString();
|
76 | const method = ConsoleMethod[logType];
|
77 | if (method) {
|
78 | console[method](`[${now}] ${instance.name}:`, ...args);
|
79 | }
|
80 | else {
|
81 | throw new Error(`Attempted to log a message with an invalid logType (value: ${logType})`);
|
82 | }
|
83 | };
|
84 | class Logger {
|
85 | |
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 | constructor(name) {
|
92 | this.name = name;
|
93 | |
94 |
|
95 |
|
96 | this._logLevel = defaultLogLevel;
|
97 | |
98 |
|
99 |
|
100 |
|
101 | this._logHandler = defaultLogHandler;
|
102 | |
103 |
|
104 |
|
105 | this._userLogHandler = null;
|
106 | |
107 |
|
108 |
|
109 | instances.push(this);
|
110 | }
|
111 | get logLevel() {
|
112 | return this._logLevel;
|
113 | }
|
114 | set logLevel(val) {
|
115 | if (!(val in LogLevel)) {
|
116 | throw new TypeError(`Invalid value "${val}" assigned to \`logLevel\``);
|
117 | }
|
118 | this._logLevel = val;
|
119 | }
|
120 |
|
121 | setLogLevel(val) {
|
122 | this._logLevel = typeof val === 'string' ? levelStringToEnum[val] : val;
|
123 | }
|
124 | get logHandler() {
|
125 | return this._logHandler;
|
126 | }
|
127 | set logHandler(val) {
|
128 | if (typeof val !== 'function') {
|
129 | throw new TypeError('Value assigned to `logHandler` must be a function');
|
130 | }
|
131 | this._logHandler = val;
|
132 | }
|
133 | get userLogHandler() {
|
134 | return this._userLogHandler;
|
135 | }
|
136 | set userLogHandler(val) {
|
137 | this._userLogHandler = val;
|
138 | }
|
139 | |
140 |
|
141 |
|
142 | debug(...args) {
|
143 | this._userLogHandler && this._userLogHandler(this, LogLevel.DEBUG, ...args);
|
144 | this._logHandler(this, LogLevel.DEBUG, ...args);
|
145 | }
|
146 | log(...args) {
|
147 | this._userLogHandler &&
|
148 | this._userLogHandler(this, LogLevel.VERBOSE, ...args);
|
149 | this._logHandler(this, LogLevel.VERBOSE, ...args);
|
150 | }
|
151 | info(...args) {
|
152 | this._userLogHandler && this._userLogHandler(this, LogLevel.INFO, ...args);
|
153 | this._logHandler(this, LogLevel.INFO, ...args);
|
154 | }
|
155 | warn(...args) {
|
156 | this._userLogHandler && this._userLogHandler(this, LogLevel.WARN, ...args);
|
157 | this._logHandler(this, LogLevel.WARN, ...args);
|
158 | }
|
159 | error(...args) {
|
160 | this._userLogHandler && this._userLogHandler(this, LogLevel.ERROR, ...args);
|
161 | this._logHandler(this, LogLevel.ERROR, ...args);
|
162 | }
|
163 | }
|
164 | function setLogLevel(level) {
|
165 | instances.forEach(inst => {
|
166 | inst.setLogLevel(level);
|
167 | });
|
168 | }
|
169 | function setUserLogHandler(logCallback, options) {
|
170 | for (const instance of instances) {
|
171 | let customLogLevel = null;
|
172 | if (options && options.level) {
|
173 | customLogLevel = levelStringToEnum[options.level];
|
174 | }
|
175 | if (logCallback === null) {
|
176 | instance.userLogHandler = null;
|
177 | }
|
178 | else {
|
179 | instance.userLogHandler = (instance, level, ...args) => {
|
180 | const message = args
|
181 | .map(arg => {
|
182 | if (arg == null) {
|
183 | return null;
|
184 | }
|
185 | else if (typeof arg === 'string') {
|
186 | return arg;
|
187 | }
|
188 | else if (typeof arg === 'number' || typeof arg === 'boolean') {
|
189 | return arg.toString();
|
190 | }
|
191 | else if (arg instanceof Error) {
|
192 | return arg.message;
|
193 | }
|
194 | else {
|
195 | try {
|
196 | return JSON.stringify(arg);
|
197 | }
|
198 | catch (ignored) {
|
199 | return null;
|
200 | }
|
201 | }
|
202 | })
|
203 | .filter(arg => arg)
|
204 | .join(' ');
|
205 | if (level >= (customLogLevel !== null && customLogLevel !== void 0 ? customLogLevel : instance.logLevel)) {
|
206 | logCallback({
|
207 | level: LogLevel[level].toLowerCase(),
|
208 | message,
|
209 | args,
|
210 | type: instance.name
|
211 | });
|
212 | }
|
213 | };
|
214 | }
|
215 | }
|
216 | }
|
217 |
|
218 | export { LogLevel, Logger, setLogLevel, setUserLogHandler };
|
219 |
|