GSI - Employe Self Service Mobile
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.

266 lines
9.3 KiB

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