UNPKG

9.72 kBJavaScriptView Raw
1import { __spreadArray } from 'tslib';
2
3/**
4 * @license
5 * Copyright 2017 Google LLC
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 */
19var _a;
20/**
21 * A container for all of the Logger instances
22 */
23var instances = [];
24/**
25 * The JS SDK supports 5 log levels and also allows a user the ability to
26 * silence the logs altogether.
27 *
28 * The order is a follows:
29 * DEBUG < VERBOSE < INFO < WARN < ERROR
30 *
31 * All of the log types above the current log level will be captured (i.e. if
32 * you set the log level to `INFO`, errors will still be logged, but `DEBUG` and
33 * `VERBOSE` logs will not)
34 */
35var 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 = {}));
44var 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 * The default log level
54 */
55var defaultLogLevel = LogLevel.INFO;
56/**
57 * By default, `console.debug` is not displayed in the developer console (in
58 * chrome). To avoid forcing users to have to opt-in to these logs twice
59 * (i.e. once for firebase, and once in the console), we are sending `DEBUG`
60 * logs to the `console.log` function.
61 */
62var 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 * The default log handler will forward DEBUG, VERBOSE, INFO, WARN, and ERROR
71 * messages on to their corresponding console counterparts (if the log method
72 * is supported by the current log level)
73 */
74var 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(["[" + now + "] " + instance.name + ":"], args));
86 }
87 else {
88 throw new Error("Attempted to log a message with an invalid logType (value: " + logType + ")");
89 }
90};
91var Logger = /** @class */ (function () {
92 /**
93 * Gives you an instance of a Logger to capture messages according to
94 * Firebase's logging scheme.
95 *
96 * @param name The name that the logs will be associated with
97 */
98 function Logger(name) {
99 this.name = name;
100 /**
101 * The log level of the given Logger instance.
102 */
103 this._logLevel = defaultLogLevel;
104 /**
105 * The main (internal) log handler for the Logger instance.
106 * Can be set to a new function in internal package code but not by user.
107 */
108 this._logHandler = defaultLogHandler;
109 /**
110 * The optional, additional, user-defined log handler for the Logger instance.
111 */
112 this._userLogHandler = null;
113 /**
114 * Capture the current instance for later use
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 \"" + val + "\" assigned to `logLevel`");
125 }
126 this._logLevel = val;
127 },
128 enumerable: false,
129 configurable: true
130 });
131 // Workaround for setter/getter having to be the same type.
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 * The functions below are all based on the `console` interface
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));
167 this._logHandler.apply(this, __spreadArray([this, LogLevel.DEBUG], args));
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));
175 this._logHandler.apply(this, __spreadArray([this, LogLevel.VERBOSE], args));
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));
183 this._logHandler.apply(this, __spreadArray([this, LogLevel.INFO], args));
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));
191 this._logHandler.apply(this, __spreadArray([this, LogLevel.WARN], args));
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));
199 this._logHandler.apply(this, __spreadArray([this, LogLevel.ERROR], args));
200 };
201 return Logger;
202}());
203function setLogLevel(level) {
204 instances.forEach(function (inst) {
205 inst.setLogLevel(level);
206 });
207}
208function 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
265export { LogLevel, Logger, setLogLevel, setUserLogHandler };
266//# sourceMappingURL=index.esm5.js.map