UNPKG

10.1 kBJavaScriptView Raw
1'use strict';
2
3Object.defineProperty(exports, '__esModule', { value: true });
4
5var tslib = require('tslib');
6
7/**
8 * @license
9 * Copyright 2017 Google LLC
10 *
11 * Licensed under the Apache License, Version 2.0 (the "License");
12 * you may not use this file except in compliance with the License.
13 * You may obtain a copy of the License at
14 *
15 * http://www.apache.org/licenses/LICENSE-2.0
16 *
17 * Unless required by applicable law or agreed to in writing, software
18 * distributed under the License is distributed on an "AS IS" BASIS,
19 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20 * See the License for the specific language governing permissions and
21 * limitations under the License.
22 */
23var _a;
24/**
25 * A container for all of the Logger instances
26 */
27var instances = [];
28/**
29 * The JS SDK supports 5 log levels and also allows a user the ability to
30 * silence the logs altogether.
31 *
32 * The order is a follows:
33 * DEBUG < VERBOSE < INFO < WARN < ERROR
34 *
35 * All of the log types above the current log level will be captured (i.e. if
36 * you set the log level to `INFO`, errors will still be logged, but `DEBUG` and
37 * `VERBOSE` logs will not)
38 */
39exports.LogLevel = void 0;
40(function (LogLevel) {
41 LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
42 LogLevel[LogLevel["VERBOSE"] = 1] = "VERBOSE";
43 LogLevel[LogLevel["INFO"] = 2] = "INFO";
44 LogLevel[LogLevel["WARN"] = 3] = "WARN";
45 LogLevel[LogLevel["ERROR"] = 4] = "ERROR";
46 LogLevel[LogLevel["SILENT"] = 5] = "SILENT";
47})(exports.LogLevel || (exports.LogLevel = {}));
48var levelStringToEnum = {
49 'debug': exports.LogLevel.DEBUG,
50 'verbose': exports.LogLevel.VERBOSE,
51 'info': exports.LogLevel.INFO,
52 'warn': exports.LogLevel.WARN,
53 'error': exports.LogLevel.ERROR,
54 'silent': exports.LogLevel.SILENT
55};
56/**
57 * The default log level
58 */
59var defaultLogLevel = exports.LogLevel.INFO;
60/**
61 * By default, `console.debug` is not displayed in the developer console (in
62 * chrome). To avoid forcing users to have to opt-in to these logs twice
63 * (i.e. once for firebase, and once in the console), we are sending `DEBUG`
64 * logs to the `console.log` function.
65 */
66var ConsoleMethod = (_a = {},
67 _a[exports.LogLevel.DEBUG] = 'log',
68 _a[exports.LogLevel.VERBOSE] = 'log',
69 _a[exports.LogLevel.INFO] = 'info',
70 _a[exports.LogLevel.WARN] = 'warn',
71 _a[exports.LogLevel.ERROR] = 'error',
72 _a);
73/**
74 * The default log handler will forward DEBUG, VERBOSE, INFO, WARN, and ERROR
75 * messages on to their corresponding console counterparts (if the log method
76 * is supported by the current log level)
77 */
78var defaultLogHandler = function (instance, logType) {
79 var args = [];
80 for (var _i = 2; _i < arguments.length; _i++) {
81 args[_i - 2] = arguments[_i];
82 }
83 if (logType < instance.logLevel) {
84 return;
85 }
86 var now = new Date().toISOString();
87 var method = ConsoleMethod[logType];
88 if (method) {
89 console[method].apply(console, tslib.__spreadArray(["[" + now + "] " + instance.name + ":"], args));
90 }
91 else {
92 throw new Error("Attempted to log a message with an invalid logType (value: " + logType + ")");
93 }
94};
95var Logger = /** @class */ (function () {
96 /**
97 * Gives you an instance of a Logger to capture messages according to
98 * Firebase's logging scheme.
99 *
100 * @param name The name that the logs will be associated with
101 */
102 function Logger(name) {
103 this.name = name;
104 /**
105 * The log level of the given Logger instance.
106 */
107 this._logLevel = defaultLogLevel;
108 /**
109 * The main (internal) log handler for the Logger instance.
110 * Can be set to a new function in internal package code but not by user.
111 */
112 this._logHandler = defaultLogHandler;
113 /**
114 * The optional, additional, user-defined log handler for the Logger instance.
115 */
116 this._userLogHandler = null;
117 /**
118 * Capture the current instance for later use
119 */
120 instances.push(this);
121 }
122 Object.defineProperty(Logger.prototype, "logLevel", {
123 get: function () {
124 return this._logLevel;
125 },
126 set: function (val) {
127 if (!(val in exports.LogLevel)) {
128 throw new TypeError("Invalid value \"" + val + "\" assigned to `logLevel`");
129 }
130 this._logLevel = val;
131 },
132 enumerable: false,
133 configurable: true
134 });
135 // Workaround for setter/getter having to be the same type.
136 Logger.prototype.setLogLevel = function (val) {
137 this._logLevel = typeof val === 'string' ? levelStringToEnum[val] : val;
138 };
139 Object.defineProperty(Logger.prototype, "logHandler", {
140 get: function () {
141 return this._logHandler;
142 },
143 set: function (val) {
144 if (typeof val !== 'function') {
145 throw new TypeError('Value assigned to `logHandler` must be a function');
146 }
147 this._logHandler = val;
148 },
149 enumerable: false,
150 configurable: true
151 });
152 Object.defineProperty(Logger.prototype, "userLogHandler", {
153 get: function () {
154 return this._userLogHandler;
155 },
156 set: function (val) {
157 this._userLogHandler = val;
158 },
159 enumerable: false,
160 configurable: true
161 });
162 /**
163 * The functions below are all based on the `console` interface
164 */
165 Logger.prototype.debug = function () {
166 var args = [];
167 for (var _i = 0; _i < arguments.length; _i++) {
168 args[_i] = arguments[_i];
169 }
170 this._userLogHandler && this._userLogHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.DEBUG], args));
171 this._logHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.DEBUG], args));
172 };
173 Logger.prototype.log = function () {
174 var args = [];
175 for (var _i = 0; _i < arguments.length; _i++) {
176 args[_i] = arguments[_i];
177 }
178 this._userLogHandler && this._userLogHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.VERBOSE], args));
179 this._logHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.VERBOSE], args));
180 };
181 Logger.prototype.info = function () {
182 var args = [];
183 for (var _i = 0; _i < arguments.length; _i++) {
184 args[_i] = arguments[_i];
185 }
186 this._userLogHandler && this._userLogHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.INFO], args));
187 this._logHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.INFO], args));
188 };
189 Logger.prototype.warn = function () {
190 var args = [];
191 for (var _i = 0; _i < arguments.length; _i++) {
192 args[_i] = arguments[_i];
193 }
194 this._userLogHandler && this._userLogHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.WARN], args));
195 this._logHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.WARN], args));
196 };
197 Logger.prototype.error = function () {
198 var args = [];
199 for (var _i = 0; _i < arguments.length; _i++) {
200 args[_i] = arguments[_i];
201 }
202 this._userLogHandler && this._userLogHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.ERROR], args));
203 this._logHandler.apply(this, tslib.__spreadArray([this, exports.LogLevel.ERROR], args));
204 };
205 return Logger;
206}());
207function setLogLevel(level) {
208 instances.forEach(function (inst) {
209 inst.setLogLevel(level);
210 });
211}
212function setUserLogHandler(logCallback, options) {
213 var _loop_1 = function (instance) {
214 var customLogLevel = null;
215 if (options && options.level) {
216 customLogLevel = levelStringToEnum[options.level];
217 }
218 if (logCallback === null) {
219 instance.userLogHandler = null;
220 }
221 else {
222 instance.userLogHandler = function (instance, level) {
223 var args = [];
224 for (var _i = 2; _i < arguments.length; _i++) {
225 args[_i - 2] = arguments[_i];
226 }
227 var message = args
228 .map(function (arg) {
229 if (arg == null) {
230 return null;
231 }
232 else if (typeof arg === 'string') {
233 return arg;
234 }
235 else if (typeof arg === 'number' || typeof arg === 'boolean') {
236 return arg.toString();
237 }
238 else if (arg instanceof Error) {
239 return arg.message;
240 }
241 else {
242 try {
243 return JSON.stringify(arg);
244 }
245 catch (ignored) {
246 return null;
247 }
248 }
249 })
250 .filter(function (arg) { return arg; })
251 .join(' ');
252 if (level >= (customLogLevel !== null && customLogLevel !== void 0 ? customLogLevel : instance.logLevel)) {
253 logCallback({
254 level: exports.LogLevel[level].toLowerCase(),
255 message: message,
256 args: args,
257 type: instance.name
258 });
259 }
260 };
261 }
262 };
263 for (var _i = 0, instances_1 = instances; _i < instances_1.length; _i++) {
264 var instance = instances_1[_i];
265 _loop_1(instance);
266 }
267}
268
269exports.Logger = Logger;
270exports.setLogLevel = setLogLevel;
271exports.setUserLogHandler = setUserLogHandler;
272//# sourceMappingURL=index.cjs.js.map