ComponentLogger.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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. import { getGlobal } from '../internal/global-utils';
  17. /**
  18. * Component Logger which is meant to be used as part of any component which
  19. * will add automatically additional namespace in front of the log message.
  20. * It will then forward all message to global diag logger
  21. * @example
  22. * const cLogger = diag.createComponentLogger({ namespace: '@opentelemetry/instrumentation-http' });
  23. * cLogger.debug('test');
  24. * // @opentelemetry/instrumentation-http test
  25. */
  26. export class DiagComponentLogger {
  27. constructor(props) {
  28. this._namespace = props.namespace || 'DiagComponentLogger';
  29. }
  30. debug(...args) {
  31. return logProxy('debug', this._namespace, args);
  32. }
  33. error(...args) {
  34. return logProxy('error', this._namespace, args);
  35. }
  36. info(...args) {
  37. return logProxy('info', this._namespace, args);
  38. }
  39. warn(...args) {
  40. return logProxy('warn', this._namespace, args);
  41. }
  42. verbose(...args) {
  43. return logProxy('verbose', this._namespace, args);
  44. }
  45. }
  46. function logProxy(funcName, namespace, args) {
  47. const logger = getGlobal('diag');
  48. // shortcut if logger not set
  49. if (!logger) {
  50. return;
  51. }
  52. args.unshift(namespace);
  53. return logger[funcName](...args);
  54. }
  55. //# sourceMappingURL=ComponentLogger.js.map