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.esm2017.js 7.2KB

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