consoleLogger.js 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. /*
  2. * Copyright The OpenTelemetry Authors
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * https://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. const consoleMap = [
  17. { n: 'error', c: 'error' },
  18. { n: 'warn', c: 'warn' },
  19. { n: 'info', c: 'info' },
  20. { n: 'debug', c: 'debug' },
  21. { n: 'verbose', c: 'trace' },
  22. ];
  23. /**
  24. * A simple Immutable Console based diagnostic logger which will output any messages to the Console.
  25. * If you want to limit the amount of logging to a specific level or lower use the
  26. * {@link createLogLevelDiagLogger}
  27. */
  28. export class DiagConsoleLogger {
  29. constructor() {
  30. function _consoleFunc(funcName) {
  31. return function (...args) {
  32. if (console) {
  33. // Some environments only expose the console when the F12 developer console is open
  34. // eslint-disable-next-line no-console
  35. let theFunc = console[funcName];
  36. if (typeof theFunc !== 'function') {
  37. // Not all environments support all functions
  38. // eslint-disable-next-line no-console
  39. theFunc = console.log;
  40. }
  41. // One last final check
  42. if (typeof theFunc === 'function') {
  43. return theFunc.apply(console, args);
  44. }
  45. }
  46. };
  47. }
  48. for (let i = 0; i < consoleMap.length; i++) {
  49. this[consoleMap[i].n] = _consoleFunc(consoleMap[i].c);
  50. }
  51. }
  52. }
  53. //# sourceMappingURL=consoleLogger.js.map