@mks2508/better-logger 0.18.2-alpha.2 → 0.18.3
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/Logger.d.ts +113 -3
- package/dist/Logger.d.ts.map +1 -1
- package/dist/ScopedLogger.d.ts +38 -1
- package/dist/ScopedLogger.d.ts.map +1 -1
- package/dist/chunks/LogContext-DZasm_P5.cjs +141 -0
- package/dist/chunks/LogContext-DZasm_P5.cjs.map +1 -0
- package/dist/chunks/LogContext-Dyzs61XG.js +136 -0
- package/dist/chunks/LogContext-Dyzs61XG.js.map +1 -0
- package/dist/chunks/transports-BZ-Mc2IT.cjs +1450 -0
- package/dist/chunks/transports-BZ-Mc2IT.cjs.map +1 -0
- package/dist/chunks/transports-DvaLAeGJ.js +1403 -0
- package/dist/chunks/transports-DvaLAeGJ.js.map +1 -0
- package/dist/context/LogContext.d.ts +2 -4
- package/dist/context/LogContext.d.ts.map +1 -1
- package/dist/context.cjs +1 -1
- package/dist/context.js +1 -1
- package/dist/index.cjs +325 -4
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.ts +5 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +323 -5
- package/dist/index.js.map +1 -1
- package/dist/transports/OtlpTraceTransport.d.ts +118 -0
- package/dist/transports/OtlpTraceTransport.d.ts.map +1 -0
- package/dist/transports/OtlpTransport.d.ts +27 -1
- package/dist/transports/OtlpTransport.d.ts.map +1 -1
- package/dist/transports/SpanRuntime.d.ts +73 -0
- package/dist/transports/SpanRuntime.d.ts.map +1 -0
- package/dist/transports/TransportManager.d.ts +18 -11
- package/dist/transports/TransportManager.d.ts.map +1 -1
- package/dist/transports/index.d.ts +1 -0
- package/dist/transports/index.d.ts.map +1 -1
- package/dist/transports-module.d.ts +3 -1
- package/dist/transports-module.d.ts.map +1 -1
- package/dist/transports.cjs +2 -1
- package/dist/transports.js +2 -2
- package/dist/types/index.d.ts +1 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/transports.d.ts +121 -2
- package/dist/types/transports.d.ts.map +1 -1
- package/dist/utils/asyncLocalStorage.d.ts +33 -0
- package/dist/utils/asyncLocalStorage.d.ts.map +1 -0
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
* cross-runtime styling/utility helpers. Side-effect free at import.
|
|
6
6
|
*/
|
|
7
7
|
import { Logger } from './Logger.js';
|
|
8
|
-
import type { Verbosity, ThemeVariant, BannerType, StyleOptions, ILogHandler } from './types/index.js';
|
|
8
|
+
import type { Verbosity, ThemeVariant, BannerType, StyleOptions, ILogHandler, Span, SpanAttributes } from './types/index.js';
|
|
9
9
|
/**
|
|
10
10
|
* Main `Logger` class — orchestrator for styling, transports, hooks,
|
|
11
11
|
* serializers, MDC, and CLI primitives. Construct directly or use the
|
|
@@ -32,6 +32,10 @@ export declare const group: (label: string, collapsed?: boolean) => void;
|
|
|
32
32
|
export declare const groupEnd: () => void;
|
|
33
33
|
export declare const time: (label: string) => void;
|
|
34
34
|
export declare const timeEnd: (label: string) => number;
|
|
35
|
+
export declare const event: (name: string, attributes?: SpanAttributes) => void;
|
|
36
|
+
export declare const startSpan: (name: string, attributes?: SpanAttributes) => Span;
|
|
37
|
+
export declare function span<T>(name: string, fn: (s: Span) => T | Promise<T>): Promise<T>;
|
|
38
|
+
export declare function span<T>(name: string, attributes: SpanAttributes, fn: (s: Span) => T | Promise<T>): Promise<T>;
|
|
35
39
|
export declare const setGlobalPrefix: (prefix: string) => void;
|
|
36
40
|
export declare const scope: (name: string) => import("./ScopedLogger.js").ScopedLogger;
|
|
37
41
|
export declare const component: (name: string) => import("./ScopedLogger.js").ComponentLogger;
|
package/dist/index.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA;;;;;GAKG;AAGH,OAAO,EAAE,MAAM,EAAE,MAAM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AAC;AAC9C,eAAO,MAAM,IAAI,GAAI,OAAO,SAAS,EAAE,UAAU,YAAY,EAAE,WAAW,MAAM,eACjC,CAAC;AAChD,eAAO,MAAM,GAAG,GAAI,OAAO,SAAS,EAAE,UAAU,YAAY,YACxB,CAAC;AACrC,eAAO,MAAM,GAAG,GAAI,YAAY,YAAY,EAAE,WAAW,MAAM,eACtB,CAAC;AAC1C,eAAO,MAAM,YAAY,GAAI,QAAQ,eAAe,WAChB,CAAC;AACrC,eAAO,MAAM,eAAe,GAAI,IAAI,MAAM,YACP,CAAC;AACpC,eAAO,MAAM,eAAe,qBACK,CAAC;AAClC,eAAO,MAAM,eAAe,qBACK,CAAC;AAGlC,OAAO,EACH,YAAY,EACZ,SAAS,EACT,eAAe,EACf,aAAa,EAChB,MAAM,mBAAmB,CAAC;AAG3B,OAAO,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AAGhD,OAAO,EACH,eAAe,EACf,eAAe,EACf,gBAAgB,EAChB,cAAc,EACd,gBAAgB,EAChB,aAAa,EAChB,MAAM,kBAAkB,CAAC;AAG1B,OAAO,EACH,SAAS,EACT,SAAS,EACT,cAAc,EACd,QAAQ,EACR,OAAO,EACP,aAAa,EACb,kBAAkB,EAClB,iBAAiB,EACpB,MAAM,wBAAwB,CAAC;AAGhC,OAAO,EACH,cAAc,EACd,mBAAmB,EACnB,YAAY,EACZ,kBAAkB,EAClB,gBAAgB,EAChB,iBAAiB,EACjB,kBAAkB,GACrB,MAAM,iCAAiC,CAAC;AAEzC,YAAY,EAAE,WAAW,EAAE,MAAM,iCAAiC,CAAC"}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
|
+
import { n as resolveAsyncLocalStorage, t as createLogContext } from "./chunks/LogContext-Dyzs61XG.js";
|
|
1
2
|
import { t as LOG_LEVELS } from "./chunks/core-Blfi2klP.js";
|
|
2
|
-
import {
|
|
3
|
+
import { c as LOG_LEVEL_TO_SEVERITY_TEXT, s as LOG_LEVEL_TO_SEVERITY_NUMBER, t as TransportManager } from "./chunks/transports-DvaLAeGJ.js";
|
|
3
4
|
import { _ as DEFAULT_CONFIG, a as safeSerialize, b as parseStackTrace, c as ansiBackground, d as ansiDim, f as ansiStyle, g as CLI_LEVEL_MAP, h as formatSuccessANSI, i as getConsoleMethod, l as ansiBold, m as formatLogLevelANSI, o as createStyledOutput, p as ansiUnderline, r as formatTablePlain, s as setupThemeChangeListener, t as createLogEntry, u as ansiColor, y as formatTimestamp } from "./chunks/utils-tKfBAWUM.js";
|
|
4
5
|
import { a as THEME_BANNERS, c as StyleBuilder, i as BANNER_VARIANTS, l as StylePresets, n as getSmartPreset, o as displayInitBanner, r as hasPreset, s as THEME_PRESETS, t as getAvailablePresets } from "./chunks/styling-CRw3KQW4.js";
|
|
5
6
|
import { a as getTerminalWidth, c as isRunningInTerminal, i as getTerminalHeight, l as supportsANSI, n as getEnvironment, r as getEnvironmentInfo, t as getColorCapability } from "./chunks/environment-detector-7NvnYUfr.js";
|
|
6
7
|
import { a as renderDivider, c as formatBadge, i as renderBox, n as SpinnerManager, o as renderHeader, r as renderTable, s as renderStep, t as NoopSpinner } from "./chunks/spinner-BtkwpzYv.js";
|
|
7
|
-
import { t as createLogContext } from "./chunks/LogContext-DjlITOzZ.js";
|
|
8
8
|
import { t as createHookBridge } from "./chunks/HookBridge-CI2PH79S.js";
|
|
9
9
|
import { t as createSerializerBridge } from "./chunks/SerializerBridge-Ba43Mk7j.js";
|
|
10
10
|
import { t as ServerFallback } from "./chunks/server-fallback-jj0T6XaK.js";
|
|
@@ -289,6 +289,34 @@ var ScopedLogger = class {
|
|
|
289
289
|
this.parent.logWithBindings(this.getBindings(), "trace", ...args);
|
|
290
290
|
}
|
|
291
291
|
/**
|
|
292
|
+
* Emite un point-span con el scope de este logger como
|
|
293
|
+
* `SpanRecord.scope`. Ver {@link Logger.event}.
|
|
294
|
+
*
|
|
295
|
+
* @param {string} name - Nombre del evento.
|
|
296
|
+
* @param {SpanAttributes} [attributes] - Attributes del span.
|
|
297
|
+
*
|
|
298
|
+
* @example
|
|
299
|
+
* logger.scope('BUILD').event('step.done', { step: 'compile' });
|
|
300
|
+
*/
|
|
301
|
+
event(name, attributes) {
|
|
302
|
+
this.parent._emitSpanEvent(name, attributes, this.getScopePrefix());
|
|
303
|
+
}
|
|
304
|
+
span(name, fnOrAttributes, maybeFn) {
|
|
305
|
+
const { attributes, fn } = resolveSpanArgs(fnOrAttributes, maybeFn);
|
|
306
|
+
return this.parent._runSpan(name, attributes, this.getScopePrefix(), fn);
|
|
307
|
+
}
|
|
308
|
+
/**
|
|
309
|
+
* Inicia un span externally-ended etiquetado con el scope de este
|
|
310
|
+
* logger. Ver {@link Logger.startSpan}.
|
|
311
|
+
*
|
|
312
|
+
* @param {string} name - Nombre de la operación.
|
|
313
|
+
* @param {SpanAttributes} [attributes] - Attributes iniciales.
|
|
314
|
+
* @returns {Span} Handle con `set`/`end`/`fail`.
|
|
315
|
+
*/
|
|
316
|
+
startSpan(name, attributes) {
|
|
317
|
+
return this.parent._startSpan(name, attributes, this.getScopePrefix());
|
|
318
|
+
}
|
|
319
|
+
/**
|
|
292
320
|
* Delegación a {@link Logger.step}: dibuja una barra de progreso discreta
|
|
293
321
|
* `current/total` para este scope.
|
|
294
322
|
*
|
|
@@ -1533,6 +1561,164 @@ function createTransportBridge() {
|
|
|
1533
1561
|
};
|
|
1534
1562
|
}
|
|
1535
1563
|
//#endregion
|
|
1564
|
+
//#region src/transports/SpanRuntime.ts
|
|
1565
|
+
/** ALS singleton del módulo — compartido por todas las instancias de Logger.
|
|
1566
|
+
*
|
|
1567
|
+
* Resolución por capas (global → `process.getBuiltinModule` → `require`,
|
|
1568
|
+
* undefined en browser) delegada al helper compartido
|
|
1569
|
+
* {@link resolveAsyncLocalStorage} — ver su doc para el detalle de capas.
|
|
1570
|
+
* `context/LogContext.ts` usa el mismo helper para su ALS de MDC. */
|
|
1571
|
+
const activeSpanStore = resolveAsyncLocalStorage();
|
|
1572
|
+
/**
|
|
1573
|
+
* Span activo en el call stack corriente (dentro de `span(fn)`), si lo hay.
|
|
1574
|
+
* Lo consumen la correlación log→span (`TransportRecord.traceId/spanId`) y la
|
|
1575
|
+
* resolución de `parentSpanId` en spans hijos.
|
|
1576
|
+
*/
|
|
1577
|
+
function getActiveSpan() {
|
|
1578
|
+
return activeSpanStore?.getStore();
|
|
1579
|
+
}
|
|
1580
|
+
/**
|
|
1581
|
+
* Ejecuta `fn` con `record` como span activo. Sin ALS disponible (browser),
|
|
1582
|
+
* ejecuta `fn` directamente — sin correlación pero funcional.
|
|
1583
|
+
*/
|
|
1584
|
+
function runWithActiveSpan(record, fn) {
|
|
1585
|
+
return activeSpanStore ? activeSpanStore.run(record, fn) : fn();
|
|
1586
|
+
}
|
|
1587
|
+
/** Nanosegundos Unix actuales como string decimal (formato OTLP). */
|
|
1588
|
+
function nowUnixNano() {
|
|
1589
|
+
return String(BigInt(Date.now()) * 1000000n);
|
|
1590
|
+
}
|
|
1591
|
+
/**
|
|
1592
|
+
* Hex aleatorio de `bytes` bytes. Usa `crypto.getRandomValues`; sin
|
|
1593
|
+
* `crypto` (runtimes exóticos) degrada a `Math.random` — suficiente para ids
|
|
1594
|
+
* de correlación, no para secrets.
|
|
1595
|
+
*/
|
|
1596
|
+
function randomHex(bytes) {
|
|
1597
|
+
const cryptoObj = globalThis.crypto;
|
|
1598
|
+
if (cryptoObj?.getRandomValues) {
|
|
1599
|
+
const buf = new Uint8Array(bytes);
|
|
1600
|
+
cryptoObj.getRandomValues(buf);
|
|
1601
|
+
let out = "";
|
|
1602
|
+
for (const b of buf) out += b.toString(16).padStart(2, "0");
|
|
1603
|
+
return out;
|
|
1604
|
+
}
|
|
1605
|
+
let out = "";
|
|
1606
|
+
for (let i = 0; i < bytes * 2; i++) out += Math.floor(Math.random() * 16).toString(16);
|
|
1607
|
+
return out;
|
|
1608
|
+
}
|
|
1609
|
+
/** Trace id W3C: 16 bytes → 32 chars hex. */
|
|
1610
|
+
function generateTraceId() {
|
|
1611
|
+
return randomHex(16);
|
|
1612
|
+
}
|
|
1613
|
+
/** Span id W3C: 8 bytes → 16 chars hex. */
|
|
1614
|
+
function generateSpanId() {
|
|
1615
|
+
return randomHex(8);
|
|
1616
|
+
}
|
|
1617
|
+
/**
|
|
1618
|
+
* Registry de spans abiertos (leak-safety). Cubre tanto los de `startSpan()`
|
|
1619
|
+
* (externally-ended, el caso del handoff) como los de `span(fn)` en curso:
|
|
1620
|
+
* si un flush llega mid-flight, es mejor exportar el span incompleto que
|
|
1621
|
+
* tirarlo. El doble-`end()` es no-op, así que el cierre natural posterior no
|
|
1622
|
+
* duplica el export.
|
|
1623
|
+
*/
|
|
1624
|
+
const openSpans = /* @__PURE__ */ new Map();
|
|
1625
|
+
/**
|
|
1626
|
+
* Crea el record de span y su {@link Span} handle. El handle registra el
|
|
1627
|
+
* span en el registry de abiertos; `onEnd` se invoca una única vez, con el
|
|
1628
|
+
* record final (duración real o `incomplete: true`), para su export al
|
|
1629
|
+
* pipeline de transports.
|
|
1630
|
+
*
|
|
1631
|
+
* `end()` solo fija `endTimeUnixNano` si no se fijó antes — así los
|
|
1632
|
+
* point-spans (`event()`) pueden fijar `end = start` exacto.
|
|
1633
|
+
*
|
|
1634
|
+
* @param name - Nombre de la operación.
|
|
1635
|
+
* @param attributes - Attributes iniciales (copia propia del record).
|
|
1636
|
+
* @param scope - Scope del logger emisor.
|
|
1637
|
+
* @param onEnd - Callback de export; recibe el record cerrado.
|
|
1638
|
+
* @returns El record (para el ALS store) y el handle del span.
|
|
1639
|
+
*/
|
|
1640
|
+
function createSpan(name, attributes, scope, onEnd) {
|
|
1641
|
+
const parent = getActiveSpan();
|
|
1642
|
+
const record = {
|
|
1643
|
+
kind: "span",
|
|
1644
|
+
traceId: parent?.traceId ?? generateTraceId(),
|
|
1645
|
+
spanId: generateSpanId(),
|
|
1646
|
+
parentSpanId: parent?.spanId,
|
|
1647
|
+
name,
|
|
1648
|
+
spanKind: 1,
|
|
1649
|
+
startTimeUnixNano: nowUnixNano(),
|
|
1650
|
+
endTimeUnixNano: "0",
|
|
1651
|
+
attributes: { ...attributes ?? {} },
|
|
1652
|
+
scope
|
|
1653
|
+
};
|
|
1654
|
+
let ended = false;
|
|
1655
|
+
const finish = () => {
|
|
1656
|
+
if (ended) return;
|
|
1657
|
+
ended = true;
|
|
1658
|
+
openSpans.delete(record);
|
|
1659
|
+
onEnd(record);
|
|
1660
|
+
};
|
|
1661
|
+
openSpans.set(record, {
|
|
1662
|
+
record,
|
|
1663
|
+
end: finish,
|
|
1664
|
+
forceEnd: () => {
|
|
1665
|
+
if (ended) return;
|
|
1666
|
+
record.incomplete = true;
|
|
1667
|
+
if (record.endTimeUnixNano === "0") record.endTimeUnixNano = nowUnixNano();
|
|
1668
|
+
finish();
|
|
1669
|
+
}
|
|
1670
|
+
});
|
|
1671
|
+
const handle = {
|
|
1672
|
+
traceId: record.traceId,
|
|
1673
|
+
spanId: record.spanId,
|
|
1674
|
+
set(key, value) {
|
|
1675
|
+
record.attributes[key] = value;
|
|
1676
|
+
return handle;
|
|
1677
|
+
},
|
|
1678
|
+
end(attributes) {
|
|
1679
|
+
if (attributes) Object.assign(record.attributes, attributes);
|
|
1680
|
+
if (ended) return;
|
|
1681
|
+
if (record.endTimeUnixNano === "0") record.endTimeUnixNano = nowUnixNano();
|
|
1682
|
+
finish();
|
|
1683
|
+
},
|
|
1684
|
+
fail(err) {
|
|
1685
|
+
if (ended) return;
|
|
1686
|
+
record.status = {
|
|
1687
|
+
code: 2,
|
|
1688
|
+
message: err instanceof Error ? err.message : String(err)
|
|
1689
|
+
};
|
|
1690
|
+
this.end();
|
|
1691
|
+
}
|
|
1692
|
+
};
|
|
1693
|
+
return {
|
|
1694
|
+
record,
|
|
1695
|
+
span: handle
|
|
1696
|
+
};
|
|
1697
|
+
}
|
|
1698
|
+
/**
|
|
1699
|
+
* Emite un point-span (`event()`): start = end exactamente, sin registro en
|
|
1700
|
+
* abiertos tras el cierre — no hay leak posible. El `parentSpanId` se resuelve
|
|
1701
|
+
* contra el span activo si existe, pero NO fija contexto ALS.
|
|
1702
|
+
*/
|
|
1703
|
+
function emitEventSpan(name, attributes, scope, onEnd) {
|
|
1704
|
+
const { record, span } = createSpan(name, attributes, scope, onEnd);
|
|
1705
|
+
record.endTimeUnixNano = record.startTimeUnixNano;
|
|
1706
|
+
span.end();
|
|
1707
|
+
}
|
|
1708
|
+
/**
|
|
1709
|
+
* Fuerza el cierre de todos los spans abiertos (`incomplete: true`) y los
|
|
1710
|
+
* exporta vía el `onEnd` de cada uno. Lo invocan `flushTransports()` /
|
|
1711
|
+
* `closeTransports()` / `cleanup()` ANTES del flush real — un span abierto
|
|
1712
|
+
* nunca se tira en shutdown.
|
|
1713
|
+
*
|
|
1714
|
+
* @returns Los records forzados a cerrar (para diagnóstico/tests).
|
|
1715
|
+
*/
|
|
1716
|
+
function forceCloseOpenSpans() {
|
|
1717
|
+
const snapshot = [...openSpans.values()];
|
|
1718
|
+
for (const entry of snapshot) entry.forceEnd();
|
|
1719
|
+
return snapshot.map((entry) => entry.record);
|
|
1720
|
+
}
|
|
1721
|
+
//#endregion
|
|
1536
1722
|
//#region src/playground/TerminalBridge.ts
|
|
1537
1723
|
/**
|
|
1538
1724
|
* Crea un {@link TerminalBridge} usando un getter para evitar referencias
|
|
@@ -2008,6 +2194,7 @@ var Logger = class Logger {
|
|
|
2008
2194
|
} catch {}
|
|
2009
2195
|
this.themeChangeListener = null;
|
|
2010
2196
|
}
|
|
2197
|
+
forceCloseOpenSpans();
|
|
2011
2198
|
await this.transportBridge.closeTransports();
|
|
2012
2199
|
this.handlers.length = 0;
|
|
2013
2200
|
this.timers.clear();
|
|
@@ -2430,21 +2617,27 @@ var Logger = class Logger {
|
|
|
2430
2617
|
return this.transportBridge.removeTransport(id);
|
|
2431
2618
|
}
|
|
2432
2619
|
/**
|
|
2433
|
-
* Fuerza el flush de todos los transports
|
|
2620
|
+
* Fuerza el flush de todos los transports. Antes de flushear, cierra los
|
|
2621
|
+
* spans aún abiertos (`incomplete: true`) y los encola para export — un
|
|
2622
|
+
* span abierto nunca se tira en un flush.
|
|
2434
2623
|
*
|
|
2435
2624
|
* @returns Promise que resuelve cuando todos los buffers están vaciados
|
|
2436
2625
|
*
|
|
2437
2626
|
*/
|
|
2438
2627
|
async flushTransports() {
|
|
2628
|
+
forceCloseOpenSpans();
|
|
2439
2629
|
await this.transportBridge.flushTransports();
|
|
2440
2630
|
}
|
|
2441
2631
|
/**
|
|
2442
|
-
* Cierra todos los transports
|
|
2632
|
+
* Cierra todos los transports. Como en {@link flushTransports}, primero
|
|
2633
|
+
* fuerza el cierre de los spans abiertos para que viajen en el flush
|
|
2634
|
+
* final del close.
|
|
2443
2635
|
*
|
|
2444
2636
|
* @returns Promise que resuelve cuando todos están cerrados
|
|
2445
2637
|
*
|
|
2446
2638
|
*/
|
|
2447
2639
|
async closeTransports() {
|
|
2640
|
+
forceCloseOpenSpans();
|
|
2448
2641
|
await this.transportBridge.closeTransports();
|
|
2449
2642
|
}
|
|
2450
2643
|
/**
|
|
@@ -2546,6 +2739,11 @@ var Logger = class Logger {
|
|
|
2546
2739
|
resource: this.logContext._getResource() ? { ...this.logContext._getResource() } : void 0,
|
|
2547
2740
|
...extra
|
|
2548
2741
|
};
|
|
2742
|
+
const activeSpan = getActiveSpan();
|
|
2743
|
+
if (activeSpan && !record.traceId) {
|
|
2744
|
+
record.traceId = activeSpan.traceId;
|
|
2745
|
+
record.spanId = activeSpan.spanId;
|
|
2746
|
+
}
|
|
2549
2747
|
this.transportBridge.writeRecord(record);
|
|
2550
2748
|
}
|
|
2551
2749
|
/**
|
|
@@ -2802,6 +3000,104 @@ var Logger = class Logger {
|
|
|
2802
3000
|
return this.log("critical", ...args);
|
|
2803
3001
|
}
|
|
2804
3002
|
/**
|
|
3003
|
+
* Emite un **point-span**: un span con `start = end` para completion
|
|
3004
|
+
* signals y eventos puntuales (una operación instantánea, un hito).
|
|
3005
|
+
* No registra contexto activo ni puede leakear — se exporta de inmediato.
|
|
3006
|
+
*
|
|
3007
|
+
* El nombre `event` (y no `trace`) es deliberado: `trace` es un log level
|
|
3008
|
+
* (el -1) y está reservado.
|
|
3009
|
+
*
|
|
3010
|
+
* @param {string} name - Nombre del evento (`job.completed`, `cache.flushed`, ...).
|
|
3011
|
+
* @param {SpanAttributes} [attributes] - Attributes del span.
|
|
3012
|
+
*
|
|
3013
|
+
* @example
|
|
3014
|
+
* logger.event('build.finished', { status: 'ok', durationMs: 4200 });
|
|
3015
|
+
*
|
|
3016
|
+
* @see {@link span} para intervalos con work-block
|
|
3017
|
+
* @see {@link startSpan} para intervalos externally-ended
|
|
3018
|
+
*/
|
|
3019
|
+
event(name, attributes) {
|
|
3020
|
+
emitEventSpan(name, attributes, this._spanScopeName(), (record) => {
|
|
3021
|
+
this.transportBridge.writeRecord(record);
|
|
3022
|
+
});
|
|
3023
|
+
}
|
|
3024
|
+
span(name, fnOrAttributes, maybeFn) {
|
|
3025
|
+
const { attributes, fn } = resolveSpanArgs(fnOrAttributes, maybeFn);
|
|
3026
|
+
return this._runSpan(name, attributes, this._spanScopeName(), fn);
|
|
3027
|
+
}
|
|
3028
|
+
/**
|
|
3029
|
+
* Inicia un span de intervalo **externally-ended**: devuelve el handle y
|
|
3030
|
+
* NO fija contexto ALS (el cierre ocurre fuera del bloque léxico — p.ej.
|
|
3031
|
+
* un `cli.spawn` que se cierra desde un poll o callback externo).
|
|
3032
|
+
*
|
|
3033
|
+
* Si el span no se cierra antes de `flushTransports()` / shutdown, el
|
|
3034
|
+
* flush lo fuerza a cerrar con `incomplete: true` y lo exporta — nunca
|
|
3035
|
+
* se tira.
|
|
3036
|
+
*
|
|
3037
|
+
* @param {string} name - Nombre de la operación.
|
|
3038
|
+
* @param {SpanAttributes} [attributes] - Attributes iniciales.
|
|
3039
|
+
* @returns {Span} Handle con `set`/`end`/`fail` y los ids del span.
|
|
3040
|
+
*
|
|
3041
|
+
* @example
|
|
3042
|
+
* const s = logger.startSpan('spawn.build', { cmd: 'make' });
|
|
3043
|
+
* proc.on('exit', code => {
|
|
3044
|
+
* if (code === 0) s.end({ exitCode: code });
|
|
3045
|
+
* else s.fail(new Error(`exit ${code}`));
|
|
3046
|
+
* });
|
|
3047
|
+
*
|
|
3048
|
+
* @see {@link span} para el caso con work-block
|
|
3049
|
+
*/
|
|
3050
|
+
startSpan(name, attributes) {
|
|
3051
|
+
return this._startSpan(name, attributes, this._spanScopeName());
|
|
3052
|
+
}
|
|
3053
|
+
/**
|
|
3054
|
+
* Scope por defecto de los spans emitidos por este logger (el
|
|
3055
|
+
* `globalPrefix`, o `'root'`).
|
|
3056
|
+
* @internal
|
|
3057
|
+
*/
|
|
3058
|
+
_spanScopeName() {
|
|
3059
|
+
return this.config.globalPrefix || "root";
|
|
3060
|
+
}
|
|
3061
|
+
/**
|
|
3062
|
+
* Emite un point-span con scope explícito. Lo consume
|
|
3063
|
+
* {@link ScopedLogger.event}, que pasa su scope compuesto.
|
|
3064
|
+
* @internal
|
|
3065
|
+
*/
|
|
3066
|
+
_emitSpanEvent(name, attributes, scope) {
|
|
3067
|
+
emitEventSpan(name, attributes, scope, (record) => {
|
|
3068
|
+
this.transportBridge.writeRecord(record);
|
|
3069
|
+
});
|
|
3070
|
+
}
|
|
3071
|
+
/**
|
|
3072
|
+
* Crea un span externally-ended con scope explícito. Lo consume
|
|
3073
|
+
* {@link ScopedLogger.startSpan}.
|
|
3074
|
+
* @internal
|
|
3075
|
+
*/
|
|
3076
|
+
_startSpan(name, attributes, scope) {
|
|
3077
|
+
const { span } = createSpan(name, attributes, scope, (record) => {
|
|
3078
|
+
this.transportBridge.writeRecord(record);
|
|
3079
|
+
});
|
|
3080
|
+
return span;
|
|
3081
|
+
}
|
|
3082
|
+
/**
|
|
3083
|
+
* Corre `fn` dentro de un span activo en el ALS con scope explícito.
|
|
3084
|
+
* Lo consume {@link ScopedLogger.span}.
|
|
3085
|
+
* @internal
|
|
3086
|
+
*/
|
|
3087
|
+
async _runSpan(name, attributes, scope, fn) {
|
|
3088
|
+
const { record, span } = createSpan(name, attributes, scope, (r) => {
|
|
3089
|
+
this.transportBridge.writeRecord(r);
|
|
3090
|
+
});
|
|
3091
|
+
try {
|
|
3092
|
+
const result = await runWithActiveSpan(record, () => fn(span));
|
|
3093
|
+
span.end();
|
|
3094
|
+
return result;
|
|
3095
|
+
} catch (error) {
|
|
3096
|
+
span.fail(error);
|
|
3097
|
+
throw error;
|
|
3098
|
+
}
|
|
3099
|
+
}
|
|
3100
|
+
/**
|
|
2805
3101
|
* Muestra datos en formato de tabla. Pasa por la pipeline completa
|
|
2806
3102
|
* (outputMode-respecting writeOutput + transports + hooks).
|
|
2807
3103
|
*
|
|
@@ -3266,6 +3562,23 @@ function toAttributeValue(value) {
|
|
|
3266
3562
|
return;
|
|
3267
3563
|
}
|
|
3268
3564
|
}
|
|
3565
|
+
/**
|
|
3566
|
+
* Resuelve los argumentos del overload `span(name, fn?)` /
|
|
3567
|
+
* `span(name, attributes, fn)` a `{ attributes, fn }`.
|
|
3568
|
+
*
|
|
3569
|
+
* @internal Compartido con `ScopedLogger.span`; no es API pública.
|
|
3570
|
+
*/
|
|
3571
|
+
function resolveSpanArgs(fnOrAttributes, maybeFn) {
|
|
3572
|
+
if (typeof fnOrAttributes === "function") return {
|
|
3573
|
+
attributes: void 0,
|
|
3574
|
+
fn: fnOrAttributes
|
|
3575
|
+
};
|
|
3576
|
+
if (typeof maybeFn === "function") return {
|
|
3577
|
+
attributes: fnOrAttributes,
|
|
3578
|
+
fn: maybeFn
|
|
3579
|
+
};
|
|
3580
|
+
throw new TypeError("span(name, …): se requiere una función fn");
|
|
3581
|
+
}
|
|
3269
3582
|
//#endregion
|
|
3270
3583
|
//#region src/index.ts
|
|
3271
3584
|
/**
|
|
@@ -3298,6 +3611,11 @@ const group = (label, collapsed) => getLogger().group(label, collapsed);
|
|
|
3298
3611
|
const groupEnd = () => getLogger().groupEnd();
|
|
3299
3612
|
const time = (label) => getLogger().time(label);
|
|
3300
3613
|
const timeEnd = (label) => getLogger().timeEnd(label);
|
|
3614
|
+
const event = (name, attributes) => getLogger().event(name, attributes);
|
|
3615
|
+
const startSpan = (name, attributes) => getLogger().startSpan(name, attributes);
|
|
3616
|
+
function span(name, fnOrAttributes, maybeFn) {
|
|
3617
|
+
return getLogger().span(name, fnOrAttributes, maybeFn);
|
|
3618
|
+
}
|
|
3301
3619
|
const setGlobalPrefix = (prefix) => getLogger().setGlobalPrefix(prefix);
|
|
3302
3620
|
const scope = (name) => getLogger().scope(name);
|
|
3303
3621
|
const component = (name) => getLogger().component(name);
|
|
@@ -3324,6 +3642,6 @@ const removeTransport = (id) => getLogger().removeTransport(id);
|
|
|
3324
3642
|
const flushTransports = () => getLogger().flushTransports();
|
|
3325
3643
|
const closeTransports = () => getLogger().closeTransports();
|
|
3326
3644
|
//#endregion
|
|
3327
|
-
export { APILogger, BANNER_VARIANTS, ComponentLogger, ContextLogger, DEFAULT_CONFIG, Logger, ScopedLogger, StyleBuilder, StylePresets, THEME_BANNERS, THEME_PRESETS, addHandler, addSerializer, addTransport, ansiBackground, ansiBold, ansiColor, ansiDim, ansiStyle, ansiUnderline, api, badge, badges, clearBadges, cli, closeTransports, component, createLogEntry, critical, debug, src_default as default, error, flushTransports, formatLogLevelANSI, formatSuccessANSI, formatTablePlain, formatTimestamp, getColorCapability, getConsoleMethod, getEnvironment, getEnvironmentInfo, getTerminalHeight, getTerminalWidth, group, groupEnd, info, isRunningInTerminal, logAnimated, logWithSVG, off, on, once, parseStackTrace, removeSerializer, removeTransport, safeSerialize, scope, setBannerType, setGlobalPrefix, setTheme, setVerbosity, showBanner, success, supportsANSI, table, time, timeEnd, trace, use, warn };
|
|
3645
|
+
export { APILogger, BANNER_VARIANTS, ComponentLogger, ContextLogger, DEFAULT_CONFIG, Logger, ScopedLogger, StyleBuilder, StylePresets, THEME_BANNERS, THEME_PRESETS, addHandler, addSerializer, addTransport, ansiBackground, ansiBold, ansiColor, ansiDim, ansiStyle, ansiUnderline, api, badge, badges, clearBadges, cli, closeTransports, component, createLogEntry, critical, debug, src_default as default, error, event, flushTransports, formatLogLevelANSI, formatSuccessANSI, formatTablePlain, formatTimestamp, getColorCapability, getConsoleMethod, getEnvironment, getEnvironmentInfo, getTerminalHeight, getTerminalWidth, group, groupEnd, info, isRunningInTerminal, logAnimated, logWithSVG, off, on, once, parseStackTrace, removeSerializer, removeTransport, safeSerialize, scope, setBannerType, setGlobalPrefix, setTheme, setVerbosity, showBanner, span, startSpan, success, supportsANSI, table, time, timeEnd, trace, use, warn };
|
|
3328
3646
|
|
|
3329
3647
|
//# sourceMappingURL=index.js.map
|