index.cjs.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. 'use strict';
  2. Object.defineProperty(exports, '__esModule', { value: true });
  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. */
  19. /**
  20. * A container for all of the Logger instances
  21. */
  22. const instances = [];
  23. /**
  24. * The JS SDK supports 5 log levels and also allows a user the ability to
  25. * silence the logs altogether.
  26. *
  27. * The order is a follows:
  28. * DEBUG < VERBOSE < INFO < WARN < ERROR
  29. *
  30. * All of the log types above the current log level will be captured (i.e. if
  31. * you set the log level to `INFO`, errors will still be logged, but `DEBUG` and
  32. * `VERBOSE` logs will not)
  33. */
  34. exports.LogLevel = void 0;
  35. (function (LogLevel) {
  36. LogLevel[LogLevel["DEBUG"] = 0] = "DEBUG";
  37. LogLevel[LogLevel["VERBOSE"] = 1] = "VERBOSE";
  38. LogLevel[LogLevel["INFO"] = 2] = "INFO";
  39. LogLevel[LogLevel["WARN"] = 3] = "WARN";
  40. LogLevel[LogLevel["ERROR"] = 4] = "ERROR";
  41. LogLevel[LogLevel["SILENT"] = 5] = "SILENT";
  42. })(exports.LogLevel || (exports.LogLevel = {}));
  43. const levelStringToEnum = {
  44. 'debug': exports.LogLevel.DEBUG,
  45. 'verbose': exports.LogLevel.VERBOSE,
  46. 'info': exports.LogLevel.INFO,
  47. 'warn': exports.LogLevel.WARN,
  48. 'error': exports.LogLevel.ERROR,
  49. 'silent': exports.LogLevel.SILENT
  50. };
  51. /**
  52. * The default log level
  53. */
  54. const defaultLogLevel = exports.LogLevel.INFO;
  55. /**
  56. * By default, `console.debug` is not displayed in the developer console (in
  57. * chrome). To avoid forcing users to have to opt-in to these logs twice
  58. * (i.e. once for firebase, and once in the console), we are sending `DEBUG`
  59. * logs to the `console.log` function.
  60. */
  61. const ConsoleMethod = {
  62. [exports.LogLevel.DEBUG]: 'log',
  63. [exports.LogLevel.VERBOSE]: 'log',
  64. [exports.LogLevel.INFO]: 'info',
  65. [exports.LogLevel.WARN]: 'warn',
  66. [exports.LogLevel.ERROR]: 'error'
  67. };
  68. /**
  69. * The default log handler will forward DEBUG, VERBOSE, INFO, WARN, and ERROR
  70. * messages on to their corresponding console counterparts (if the log method
  71. * is supported by the current log level)
  72. */
  73. const defaultLogHandler = (instance, logType, ...args) => {
  74. if (logType < instance.logLevel) {
  75. return;
  76. }
  77. const now = new Date().toISOString();
  78. const method = ConsoleMethod[logType];
  79. if (method) {
  80. console[method](`[${now}] ${instance.name}:`, ...args);
  81. }
  82. else {
  83. throw new Error(`Attempted to log a message with an invalid logType (value: ${logType})`);
  84. }
  85. };
  86. class Logger {
  87. /**
  88. * Gives you an instance of a Logger to capture messages according to
  89. * Firebase's logging scheme.
  90. *
  91. * @param name The name that the logs will be associated with
  92. */
  93. constructor(name) {
  94. this.name = name;
  95. /**
  96. * The log level of the given Logger instance.
  97. */
  98. this._logLevel = defaultLogLevel;
  99. /**
  100. * The main (internal) log handler for the Logger instance.
  101. * Can be set to a new function in internal package code but not by user.
  102. */
  103. this._logHandler = defaultLogHandler;
  104. /**
  105. * The optional, additional, user-defined log handler for the Logger instance.
  106. */
  107. this._userLogHandler = null;
  108. /**
  109. * Capture the current instance for later use
  110. */
  111. instances.push(this);
  112. }
  113. get logLevel() {
  114. return this._logLevel;
  115. }
  116. set logLevel(val) {
  117. if (!(val in exports.LogLevel)) {
  118. throw new TypeError(`Invalid value "${val}" assigned to \`logLevel\``);
  119. }
  120. this._logLevel = val;
  121. }
  122. // Workaround for setter/getter having to be the same type.
  123. setLogLevel(val) {
  124. this._logLevel = typeof val === 'string' ? levelStringToEnum[val] : val;
  125. }
  126. get logHandler() {
  127. return this._logHandler;
  128. }
  129. set logHandler(val) {
  130. if (typeof val !== 'function') {
  131. throw new TypeError('Value assigned to `logHandler` must be a function');
  132. }
  133. this._logHandler = val;
  134. }
  135. get userLogHandler() {
  136. return this._userLogHandler;
  137. }
  138. set userLogHandler(val) {
  139. this._userLogHandler = val;
  140. }
  141. /**
  142. * The functions below are all based on the `console` interface
  143. */
  144. debug(...args) {
  145. this._userLogHandler && this._userLogHandler(this, exports.LogLevel.DEBUG, ...args);
  146. this._logHandler(this, exports.LogLevel.DEBUG, ...args);
  147. }
  148. log(...args) {
  149. this._userLogHandler &&
  150. this._userLogHandler(this, exports.LogLevel.VERBOSE, ...args);
  151. this._logHandler(this, exports.LogLevel.VERBOSE, ...args);
  152. }
  153. info(...args) {
  154. this._userLogHandler && this._userLogHandler(this, exports.LogLevel.INFO, ...args);
  155. this._logHandler(this, exports.LogLevel.INFO, ...args);
  156. }
  157. warn(...args) {
  158. this._userLogHandler && this._userLogHandler(this, exports.LogLevel.WARN, ...args);
  159. this._logHandler(this, exports.LogLevel.WARN, ...args);
  160. }
  161. error(...args) {
  162. this._userLogHandler && this._userLogHandler(this, exports.LogLevel.ERROR, ...args);
  163. this._logHandler(this, exports.LogLevel.ERROR, ...args);
  164. }
  165. }
  166. function setLogLevel(level) {
  167. instances.forEach(inst => {
  168. inst.setLogLevel(level);
  169. });
  170. }
  171. function setUserLogHandler(logCallback, options) {
  172. for (const instance of instances) {
  173. let customLogLevel = null;
  174. if (options && options.level) {
  175. customLogLevel = levelStringToEnum[options.level];
  176. }
  177. if (logCallback === null) {
  178. instance.userLogHandler = null;
  179. }
  180. else {
  181. instance.userLogHandler = (instance, level, ...args) => {
  182. const message = args
  183. .map(arg => {
  184. if (arg == null) {
  185. return null;
  186. }
  187. else if (typeof arg === 'string') {
  188. return arg;
  189. }
  190. else if (typeof arg === 'number' || typeof arg === 'boolean') {
  191. return arg.toString();
  192. }
  193. else if (arg instanceof Error) {
  194. return arg.message;
  195. }
  196. else {
  197. try {
  198. return JSON.stringify(arg);
  199. }
  200. catch (ignored) {
  201. return null;
  202. }
  203. }
  204. })
  205. .filter(arg => arg)
  206. .join(' ');
  207. if (level >= (customLogLevel !== null && customLogLevel !== void 0 ? customLogLevel : instance.logLevel)) {
  208. logCallback({
  209. level: exports.LogLevel[level].toLowerCase(),
  210. message,
  211. args,
  212. type: instance.name
  213. });
  214. }
  215. };
  216. }
  217. }
  218. }
  219. exports.Logger = Logger;
  220. exports.setLogLevel = setLogLevel;
  221. exports.setUserLogHandler = setUserLogHandler;
  222. //# sourceMappingURL=index.cjs.js.map