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