@devopsplaybook.io/otel-utils
Version:
Utility to simplify integration with Open Telemetry
51 lines (50 loc) • 1.99 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.ModuleLogger = void 0;
const api_logs_1 = require("@opentelemetry/api-logs");
class ModuleLogger {
constructor(module, standardLogger) {
this.module = module;
this.standardLogger = standardLogger;
}
info(message, context) {
this.display("info", message, api_logs_1.SeverityNumber.INFO, null, context);
}
warn(message, context) {
this.display("warn", message, api_logs_1.SeverityNumber.WARN, null, context);
}
error(message, error, context) {
this.display("error", message, api_logs_1.SeverityNumber.ERROR, error, context);
}
display(level, message, severityNumber = api_logs_1.SeverityNumber.INFO, error, context) {
var _a, _b;
let formattedMessage = message;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const attributes = { "log.type": "custom" };
formattedMessage = message;
if (error) {
attributes["exception.type"] = error.name;
attributes["exception.message"] = error.message;
attributes["exception.stacktrace"] = error.stack;
formattedMessage += "\n" + error.stack;
}
if (context) {
const spanCtx = context.spanContext();
if (spanCtx) {
attributes["span.id"] = spanCtx.spanId;
attributes["trace.id"] = spanCtx.traceId;
}
}
console.log(`[${level}] [${this.module}] ${formattedMessage}`);
if (!((_a = this.standardLogger) === null || _a === void 0 ? void 0 : _a.getLogger())) {
return;
}
(_b = this.standardLogger.getLogger()) === null || _b === void 0 ? void 0 : _b.emit({
severityNumber,
severityText: level,
body: `[${this.module}] ${formattedMessage}`,
attributes,
});
}
}
exports.ModuleLogger = ModuleLogger;
;