@globalart/nestjs-logger 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -7261,6 +7261,30 @@ const DEFAULT_LOGGER_CONFIG = {
7261
7261
  logRequests: false
7262
7262
  };
7263
7263
 
7264
+ //#endregion
7265
+ //#region src/utils/opentelemetry-trace.ts
7266
+ let cachedGetter = void 0;
7267
+ function createGetter() {
7268
+ try {
7269
+ const api = require("@opentelemetry/api");
7270
+ return () => {
7271
+ const span = api.trace?.getSpan?.(api.context?.active?.());
7272
+ if (!span) return {};
7273
+ const { spanId, traceId } = span.spanContext?.() ?? {};
7274
+ return spanId && traceId ? {
7275
+ spanId,
7276
+ traceId
7277
+ } : {};
7278
+ };
7279
+ } catch {
7280
+ return () => ({});
7281
+ }
7282
+ }
7283
+ function getOpenTelemetryTraceIds() {
7284
+ if (cachedGetter === void 0) cachedGetter = createGetter();
7285
+ return cachedGetter?.() ?? {};
7286
+ }
7287
+
7264
7288
  //#endregion
7265
7289
  //#region \0@oxc-project+runtime@0.112.0/helpers/decorateMetadata.js
7266
7290
  function __decorateMetadata(k, v) {
@@ -7290,36 +7314,60 @@ let LoggerService = class LoggerService {
7290
7314
  this.context = context;
7291
7315
  }
7292
7316
  log(options) {
7293
- this.writeLog("info", options.message, options.context, options.metadata);
7317
+ this.writeLog("info", options);
7294
7318
  }
7295
7319
  error(options) {
7296
- this.writeLog("error", options.message, options.context, options.metadata, options.trace);
7320
+ this.writeLog("error", options);
7297
7321
  }
7298
7322
  warn(options) {
7299
- this.writeLog("warn", options.message, options.context, options.metadata);
7323
+ this.writeLog("warn", options);
7300
7324
  }
7301
7325
  debug(options) {
7302
- this.writeLog("debug", options.message, options.context, options.metadata);
7326
+ this.writeLog("debug", options);
7303
7327
  }
7304
7328
  verbose(options) {
7305
- this.writeLog("verbose", options.message, options.context, options.metadata);
7329
+ this.writeLog("verbose", options);
7306
7330
  }
7307
7331
  logHttpRequest(entry) {
7308
- const formatted = this.formatter.formatHttpRequest(entry);
7332
+ const enriched = this.enrichWithTraceIds(entry);
7333
+ const formatted = this.formatter.formatHttpRequest(enriched);
7309
7334
  this.writer.write(formatted);
7310
7335
  }
7311
- writeLog(level, message, context, metadata, trace) {
7336
+ writeLog(level, options) {
7337
+ const { traceId, spanId } = this.resolveTraceIds(options);
7312
7338
  const entry = {
7313
7339
  level,
7314
- message,
7340
+ message: options.message,
7315
7341
  timestamp: /* @__PURE__ */ new Date(),
7316
- context: context ?? this.context ?? this.contextResolver.resolve(),
7317
- metadata,
7318
- trace
7342
+ context: options.context ?? this.context ?? this.contextResolver.resolve(),
7343
+ metadata: options.metadata,
7344
+ trace: options.trace,
7345
+ traceId,
7346
+ spanId
7319
7347
  };
7320
7348
  const formatted = this.formatter.format(entry);
7321
7349
  this.writer.write(formatted);
7322
7350
  }
7351
+ resolveTraceIds(options) {
7352
+ if (options.traceId && options.spanId) return {
7353
+ traceId: options.traceId,
7354
+ spanId: options.spanId
7355
+ };
7356
+ const otel = getOpenTelemetryTraceIds();
7357
+ return {
7358
+ traceId: options.traceId ?? otel.traceId,
7359
+ spanId: options.spanId ?? otel.spanId
7360
+ };
7361
+ }
7362
+ enrichWithTraceIds(entry) {
7363
+ if (entry.traceId && entry.spanId) return entry;
7364
+ const otel = getOpenTelemetryTraceIds();
7365
+ return {
7366
+ ...entry,
7367
+ traceId: entry.traceId ?? otel.traceId,
7368
+ spanId: entry.spanId ?? otel.spanId
7369
+ };
7370
+ }
7323
7371
  };
7324
7372
  LoggerService = __decorate([(0, _nestjs_common.Injectable)(), __decorateMetadata("design:paramtypes", [
7325
7373
  Object,
@@ -7648,8 +7696,15 @@ let TextFormatter = class TextFormatter extends BaseFormatter {
7648
7696
  this.addContext(parts, entry);
7649
7697
  this.addMessage(parts, entry);
7650
7698
  this.addMetadata(parts, entry);
7699
+ this.addTraceIds(parts, entry);
7651
7700
  return parts;
7652
7701
  }
7702
+ addTraceIds(parts, entry) {
7703
+ if (entry.traceId || entry.spanId) {
7704
+ const ids = [entry.traceId, entry.spanId].filter(Boolean).join("/");
7705
+ parts.push(this.colorize(`[${ids}]`, "magenta"));
7706
+ }
7707
+ }
7653
7708
  addTimestamp(parts, entry) {
7654
7709
  if (this.options.timestamp) {
7655
7710
  const timestamp = this.colorize(`[${this.formatTimestamp(entry.timestamp)}]`, "gray");
@@ -7709,6 +7764,8 @@ let JsonFormatter = class JsonFormatter extends BaseFormatter {
7709
7764
  if (entry.context) result.context = entry.context;
7710
7765
  if (entry.metadata) result.metadata = entry.metadata;
7711
7766
  if (entry.trace) result.trace = entry.trace;
7767
+ if (entry.traceId) result.traceId = entry.traceId;
7768
+ if (entry.spanId) result.spanId = entry.spanId;
7712
7769
  return result;
7713
7770
  }
7714
7771
  };
@@ -7718,10 +7775,23 @@ JsonFormatter = __decorate([(0, _nestjs_common.Injectable)()], JsonFormatter);
7718
7775
  //#region src/formatters/pino-formatter.ts
7719
7776
  let PinoFormatter = class PinoFormatter extends BaseFormatter {
7720
7777
  format(entry) {
7721
- return JSON.stringify(entry);
7778
+ const obj = {
7779
+ level: entry.level,
7780
+ message: entry.message,
7781
+ timestamp: entry.timestamp,
7782
+ context: entry.context,
7783
+ metadata: entry.metadata,
7784
+ trace: entry.trace
7785
+ };
7786
+ if (entry.traceId) obj.traceId = entry.traceId;
7787
+ if (entry.spanId) obj.spanId = entry.spanId;
7788
+ return JSON.stringify(obj);
7722
7789
  }
7723
7790
  formatHttpRequest(entry) {
7724
- const jsonString = JSON.stringify(entry);
7791
+ const obj = { ...entry };
7792
+ if (entry.traceId) obj.traceId = entry.traceId;
7793
+ if (entry.spanId) obj.spanId = entry.spanId;
7794
+ const jsonString = JSON.stringify(obj);
7725
7795
  return this.options.colors ? this.colorize(jsonString, this.getColorForLevel(entry.level)) : jsonString;
7726
7796
  }
7727
7797
  };