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