No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

index.cjs.js 9.7KB

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