logLevelLogger.js 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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 { DiagLogLevel } from '../types';
  17. export function createLogLevelDiagLogger(maxLevel, logger) {
  18. if (maxLevel < DiagLogLevel.NONE) {
  19. maxLevel = DiagLogLevel.NONE;
  20. }
  21. else if (maxLevel > DiagLogLevel.ALL) {
  22. maxLevel = DiagLogLevel.ALL;
  23. }
  24. // In case the logger is null or undefined
  25. logger = logger || {};
  26. function _filterFunc(funcName, theLevel) {
  27. var theFunc = logger[funcName];
  28. if (typeof theFunc === 'function' && maxLevel >= theLevel) {
  29. return theFunc.bind(logger);
  30. }
  31. return function () { };
  32. }
  33. return {
  34. error: _filterFunc('error', DiagLogLevel.ERROR),
  35. warn: _filterFunc('warn', DiagLogLevel.WARN),
  36. info: _filterFunc('info', DiagLogLevel.INFO),
  37. debug: _filterFunc('debug', DiagLogLevel.DEBUG),
  38. verbose: _filterFunc('verbose', DiagLogLevel.VERBOSE),
  39. };
  40. }
  41. //# sourceMappingURL=logLevelLogger.js.map