logger.js 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. /*!
  3. * Copyright 2018 Google Inc. All Rights Reserved.
  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. Object.defineProperty(exports, "__esModule", { value: true });
  18. exports.logger = logger;
  19. exports.setLogFunction = setLogFunction;
  20. exports.setLibVersion = setLibVersion;
  21. const util = require("util");
  22. const validate_1 = require("./validate");
  23. /*! The Firestore library version */
  24. let libVersion;
  25. /*! The external function used to emit logs. */
  26. let logFunction = null;
  27. /**
  28. * Log function to use for debug output. By default, we don't perform any
  29. * logging.
  30. *
  31. * @private
  32. * @internal
  33. */
  34. function logger(methodName, requestTag, logMessage, ...additionalArgs) {
  35. requestTag = requestTag || '#####';
  36. if (logFunction) {
  37. const formattedMessage = util.format(logMessage, ...additionalArgs);
  38. const time = new Date().toISOString();
  39. logFunction(`Firestore (${libVersion}) ${time} ${requestTag} [${methodName}]: ` +
  40. formattedMessage);
  41. }
  42. }
  43. /**
  44. * Sets or disables the log function for all active Firestore instances.
  45. *
  46. * @param logger A log function that takes a message (such as `console.log`) or
  47. * `null` to turn off logging.
  48. */
  49. function setLogFunction(logger) {
  50. if (logger !== null)
  51. (0, validate_1.validateFunction)('logger', logger);
  52. logFunction = logger;
  53. }
  54. /**
  55. * Sets the library version to be used in log messages.
  56. *
  57. * @private
  58. * @internal
  59. */
  60. function setLibVersion(version) {
  61. libVersion = version;
  62. }
  63. //# sourceMappingURL=logger.js.map