UNPKG

4.13 kBJavaScriptView Raw
1"use strict";
2/*
3 * Copyright The OpenTelemetry Authors
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 * https://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 */
17Object.defineProperty(exports, "__esModule", { value: true });
18exports.DiagAPI = void 0;
19const ComponentLogger_1 = require("../diag/ComponentLogger");
20const logLevelLogger_1 = require("../diag/internal/logLevelLogger");
21const types_1 = require("../diag/types");
22const global_utils_1 = require("../internal/global-utils");
23const API_NAME = 'diag';
24/**
25 * Singleton object which represents the entry point to the OpenTelemetry internal
26 * diagnostic API
27 */
28class DiagAPI {
29 /**
30 * Private internal constructor
31 * @private
32 */
33 constructor() {
34 function _logProxy(funcName) {
35 return function (...args) {
36 const logger = (0, global_utils_1.getGlobal)('diag');
37 // shortcut if logger not set
38 if (!logger)
39 return;
40 return logger[funcName](...args);
41 };
42 }
43 // Using self local variable for minification purposes as 'this' cannot be minified
44 const self = this;
45 // DiagAPI specific functions
46 const setLogger = (logger, optionsOrLogLevel = { logLevel: types_1.DiagLogLevel.INFO }) => {
47 var _a, _b, _c;
48 if (logger === self) {
49 // There isn't much we can do here.
50 // Logging to the console might break the user application.
51 // Try to log to self. If a logger was previously registered it will receive the log.
52 const err = new Error('Cannot use diag as the logger for itself. Please use a DiagLogger implementation like ConsoleDiagLogger or a custom implementation');
53 self.error((_a = err.stack) !== null && _a !== void 0 ? _a : err.message);
54 return false;
55 }
56 if (typeof optionsOrLogLevel === 'number') {
57 optionsOrLogLevel = {
58 logLevel: optionsOrLogLevel,
59 };
60 }
61 const oldLogger = (0, global_utils_1.getGlobal)('diag');
62 const newLogger = (0, logLevelLogger_1.createLogLevelDiagLogger)((_b = optionsOrLogLevel.logLevel) !== null && _b !== void 0 ? _b : types_1.DiagLogLevel.INFO, logger);
63 // There already is an logger registered. We'll let it know before overwriting it.
64 if (oldLogger && !optionsOrLogLevel.suppressOverrideMessage) {
65 const stack = (_c = new Error().stack) !== null && _c !== void 0 ? _c : '<failed to generate stacktrace>';
66 oldLogger.warn(`Current logger will be overwritten from ${stack}`);
67 newLogger.warn(`Current logger will overwrite one already registered from ${stack}`);
68 }
69 return (0, global_utils_1.registerGlobal)('diag', newLogger, self, true);
70 };
71 self.setLogger = setLogger;
72 self.disable = () => {
73 (0, global_utils_1.unregisterGlobal)(API_NAME, self);
74 };
75 self.createComponentLogger = (options) => {
76 return new ComponentLogger_1.DiagComponentLogger(options);
77 };
78 self.verbose = _logProxy('verbose');
79 self.debug = _logProxy('debug');
80 self.info = _logProxy('info');
81 self.warn = _logProxy('warn');
82 self.error = _logProxy('error');
83 }
84 /** Get the singleton instance of the DiagAPI API */
85 static instance() {
86 if (!this._instance) {
87 this._instance = new DiagAPI();
88 }
89 return this._instance;
90 }
91}
92exports.DiagAPI = DiagAPI;
93//# sourceMappingURL=diag.js.map
\No newline at end of file