|
| 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 | + |
| 17 | +import { |
| 18 | + DiagLogger, |
| 19 | + DiagLogFunction, |
| 20 | + createNoopDiagLogger, |
| 21 | + diagLoggerFunctions, |
| 22 | +} from '../diag/logger'; |
| 23 | +import { DiagLogLevel, createLogLevelDiagLogger } from '../diag/logLevel'; |
| 24 | +import { |
| 25 | + API_BACKWARDS_COMPATIBILITY_VERSION, |
| 26 | + GLOBAL_DIAG_LOGGER_API_KEY, |
| 27 | + makeGetter, |
| 28 | + _global, |
| 29 | +} from './global-utils'; |
| 30 | + |
| 31 | +/** Internal simple Noop Diag API that returns a noop logger and does not allow any changes */ |
| 32 | +function noopDiagApi(): DiagAPI { |
| 33 | + const noopApi = createNoopDiagLogger() as DiagAPI; |
| 34 | + |
| 35 | + noopApi.getLogger = () => noopApi; |
| 36 | + noopApi.setLogger = noopApi.getLogger; |
| 37 | + noopApi.setLogLevel = () => {}; |
| 38 | + |
| 39 | + return noopApi; |
| 40 | +} |
| 41 | + |
| 42 | +/** |
| 43 | + * Singleton object which represents the entry point to the OpenTelemetry internal |
| 44 | + * diagnostic API |
| 45 | + */ |
| 46 | +export class DiagAPI implements DiagLogger { |
| 47 | + /** Get the singleton instance of the DiagAPI API */ |
| 48 | + public static instance(): DiagAPI { |
| 49 | + let theInst = null; |
| 50 | + if (_global[GLOBAL_DIAG_LOGGER_API_KEY]) { |
| 51 | + // Looks like a previous instance was set, so try and fetch it |
| 52 | + theInst = _global[GLOBAL_DIAG_LOGGER_API_KEY]?.( |
| 53 | + API_BACKWARDS_COMPATIBILITY_VERSION |
| 54 | + ) as DiagAPI; |
| 55 | + } |
| 56 | + |
| 57 | + if (!theInst) { |
| 58 | + theInst = new DiagAPI(); |
| 59 | + _global[GLOBAL_DIAG_LOGGER_API_KEY] = makeGetter( |
| 60 | + API_BACKWARDS_COMPATIBILITY_VERSION, |
| 61 | + theInst, |
| 62 | + noopDiagApi() |
| 63 | + ); |
| 64 | + } |
| 65 | + |
| 66 | + return theInst; |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Private internal constructor |
| 71 | + * @private |
| 72 | + */ |
| 73 | + private constructor() { |
| 74 | + let _logLevel: DiagLogLevel = DiagLogLevel.INFO; |
| 75 | + let _filteredLogger: DiagLogger | null; |
| 76 | + let _logger: DiagLogger = createNoopDiagLogger(); |
| 77 | + |
| 78 | + function _logProxy(funcName: keyof DiagLogger): DiagLogFunction { |
| 79 | + return function () { |
| 80 | + const orgArguments = arguments as unknown; |
| 81 | + const theLogger = _filteredLogger || _logger; |
| 82 | + const theFunc = theLogger[funcName]; |
| 83 | + if (typeof theFunc === 'function') { |
| 84 | + return theFunc.apply( |
| 85 | + theLogger, |
| 86 | + orgArguments as Parameters<DiagLogFunction> |
| 87 | + ); |
| 88 | + } |
| 89 | + }; |
| 90 | + } |
| 91 | + |
| 92 | + // Using self local variable for minification purposes as 'this' cannot be minified |
| 93 | + const self = this; |
| 94 | + |
| 95 | + // DiagAPI specific functions |
| 96 | + |
| 97 | + self.getLogger = (): DiagLogger => { |
| 98 | + // Return itself if no existing logger is defined (defaults effectively to a Noop) |
| 99 | + return _logger; |
| 100 | + }; |
| 101 | + |
| 102 | + self.setLogger = (logger: DiagLogger): DiagLogger => { |
| 103 | + const prevLogger = _logger; |
| 104 | + if (prevLogger !== logger && logger !== self) { |
| 105 | + // Simple special case to avoid any possible infinite recursion on the logging functions |
| 106 | + _logger = logger || createNoopDiagLogger(); |
| 107 | + _filteredLogger = createLogLevelDiagLogger(_logLevel, _logger); |
| 108 | + } |
| 109 | + |
| 110 | + return prevLogger; |
| 111 | + }; |
| 112 | + |
| 113 | + self.setLogLevel = (maxLogLevel: DiagLogLevel) => { |
| 114 | + if (maxLogLevel !== _logLevel) { |
| 115 | + _logLevel = maxLogLevel; |
| 116 | + if (_logger) { |
| 117 | + _filteredLogger = createLogLevelDiagLogger(maxLogLevel, _logger); |
| 118 | + } |
| 119 | + } |
| 120 | + }; |
| 121 | + |
| 122 | + for (let i = 0; i < diagLoggerFunctions.length; i++) { |
| 123 | + const name = diagLoggerFunctions[i]; |
| 124 | + self[name] = _logProxy(name); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + /** |
| 129 | + * Return the currently configured logger instance, if no logger has been configured |
| 130 | + * it will return itself so any log level filtering will still be applied in this case. |
| 131 | + */ |
| 132 | + public getLogger!: () => DiagLogger; |
| 133 | + |
| 134 | + /** |
| 135 | + * Set the DiagLogger instance |
| 136 | + * @param logger - The DiagLogger instance to set as the default logger |
| 137 | + * @returns The previously registered DiagLogger |
| 138 | + */ |
| 139 | + public setLogger!: (logger: DiagLogger) => DiagLogger; |
| 140 | + |
| 141 | + /** Set the default maximum diagnostic logging level */ |
| 142 | + public setLogLevel!: (maxLogLevel: DiagLogLevel) => void; |
| 143 | + |
| 144 | + // DiagLogger implementation |
| 145 | + public verbose!: DiagLogFunction; |
| 146 | + public debug!: DiagLogFunction; |
| 147 | + public info!: DiagLogFunction; |
| 148 | + public warn!: DiagLogFunction; |
| 149 | + public startupInfo!: DiagLogFunction; |
| 150 | + public error!: DiagLogFunction; |
| 151 | + public critical!: DiagLogFunction; |
| 152 | + public terminal!: DiagLogFunction; |
| 153 | +} |
0 commit comments