UNPKG

@dynatrace/opentelemetry-core

Version:

Dynatrace OpenTelemetry core package

161 lines (158 loc) 5.79 kB
"use strict"; /* Copyright 2022 Dynatrace LLC Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. */ Object.defineProperty(exports, "__esModule", { value: true }); exports.logger = exports.ComponentLogger = exports.DtLogger = exports.LogKind = void 0; const core_1 = require("@opentelemetry/core"); const Types_1 = require("./Types"); const StartupConfig_1 = require("./configuration/StartupConfig"); const LoggingUtils_1 = require("./utils/LoggingUtils"); const StartupBanner_1 = require("./utils/StartupBanner"); const ThreadId_1 = require("./utils/ThreadId"); // ============================================================================ var LogKind; (function (LogKind) { LogKind["Info"] = "info "; LogKind["Warn"] = "warning"; LogKind["Error"] = "error "; LogKind["Debug"] = "debug "; })(LogKind = exports.LogKind || (exports.LogKind = {})); class DtLogger { constructor() { /** * Helper to log an info message. * @see log */ this.info = (component, msg) => this.log(LogKind.Info, component, msg); /** * Helper to log a warning message. * @see log */ this.warn = (component, msg) => this.log(LogKind.Warn, component, msg); /** * Helper to log an error message. * @see log */ this.error = (component, msg) => this.log(LogKind.Error, component, msg); /** * Helper to log a debug message. * @see log */ this.debug = (component, msg) => this.log(LogKind.Debug, component, msg); this._flags = Object.create(null); } /** * Returns true if logger is enabled. * Optionally the logging flag is checked * @param flag Logging flag to check * @returns true if log message will be sent out */ enabled(flag) { return this._logFn != null && ((flag == null) ? true : (this._flags[flag] === true)); } /** * Configure the logger. * @param destination The logging destination to use * @param flags The logging flags */ configure(destination = Types_1.LoggingDestination.Off, flags = Object.create(null)) { switch (destination) { case Types_1.LoggingDestination.StdErr: // eslint-disable-next-line no-console this._logFn = console.error; break; case Types_1.LoggingDestination.StdOut: // eslint-disable-next-line no-console this._logFn = console.log; break; default: this._logFn = undefined; break; } this._flags = flags; } /** * Formats and sends a log message. * @param kind The kind of message (e.g. error/warning) * @param component The component logging the message * @param msg The actual message */ log(kind, component, msg) { if (this._logFn == null) { return; } // ISO format is yyyy-MM-DDTHH:MM:SS.sssZ // our format is yyyy-MM-DD HH:MM:SS.sss UTC const date = `${new Date((0, core_1.hrTimeToMilliseconds)((0, core_1.hrTime)())).toISOString().replace(/(T|Z$)/g, " ")}UTC`; const message = `[Dynatrace] ${date} [${process.pid}-${(0, ThreadId_1.getThreadId)()}] ${kind} [${component}] ${msg}`; this._logFn(message); } /** * Helper to log an agent exception as warning. * @param component The component logging the message * @param e the exception to log */ logAgentException(component, e) { this.warn(component, `exception in agent code: ${(0, LoggingUtils_1.verboseFormatException)(e)}`); } } exports.DtLogger = DtLogger; // ============================================================================ /** * convenience class for component level logging */ class ComponentLogger { constructor(componentName, debugFlagName) { this.componentName = componentName; this.debugFlagName = (debugFlagName == null) ? componentName : debugFlagName; } get enabled() { return exports.logger.enabled(); } get debugEnabled() { return exports.logger.enabled(this.debugFlagName); } info(msg) { exports.logger.info(this.componentName, msg); } warn(msg) { exports.logger.warn(this.componentName, msg); } error(msg) { exports.logger.error(this.componentName, msg); } verbose(msg) { this.debug(msg); } /** * Helper to log a debug message. * Note: check if debug logging is enabled before calling this API to avoid expensive * log message composition * @see log */ debug(msg) { if (this.debugEnabled) { exports.logger.debug(this.componentName, msg); } } logAgentException(e) { exports.logger.logAgentException(this.componentName, e); } } exports.ComponentLogger = ComponentLogger; // now create Logger instance, configure it and log the startup Banner exports.logger = new DtLogger(); const config = (0, StartupConfig_1.getConfiguration)(); exports.logger.configure(config.loggingDestination, config.loggingFlags); (0, StartupBanner_1.logStartupBanner)(); //# sourceMappingURL=Logger.js.map