logquill 0.3.0 → 0.4.0

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.d.cts CHANGED
@@ -1,83 +1,113 @@
1
- /** Log levels, shared by name and numeric weight with the logquill-python contract. */
2
- declare enum Level {
3
- TRACE = 5,
4
- DEBUG = 10,
5
- INFO = 20,
6
- WARN = 30,
7
- ERROR = 40,
8
- FATAL = 50
9
- }
10
- /** A level given as a `Level`, a level name (any case), or its numeric weight. */
11
- type LevelInput = Level | number | string;
12
- /** The level's name, e.g. `levelName(Level.INFO) === "INFO"`. */
13
- declare function levelName(level: Level): string;
14
- /** Normalize a level given as a `Level`, level name, or numeric weight. Throws if unknown. */
15
- declare function parseLevel(level: LevelInput): Level;
1
+ import { P as Plugin, L as LogRecord, a as Level, T as Transport, b as LevelInput, F as Formatter } from './logger-D1_THnBJ.cjs';
2
+ export { C as CollectingTransport, c as FunctionPlugin, J as JSONFormatter, d as Logger, e as LoggerOptions, M as MiddlewareFunc, S as SpanOptions, f as createRecord, l as levelName, p as parseLevel, u as utcTimestamp } from './logger-D1_THnBJ.cjs';
16
3
 
17
- /** The cross-language record shape shared with logquill-python. */
18
- interface LogRecord {
19
- timestamp: string;
20
- level: string;
21
- logger: string;
22
- message: string;
23
- meta: Record<string, unknown>;
4
+ /**
5
+ * Injects fixed key/value pairs into every record's `meta`.
6
+ * A value already present in a record's own `meta` wins over the fixed context.
7
+ */
8
+ declare class ContextPlugin implements Plugin {
9
+ readonly context: Record<string, unknown>;
10
+ constructor(context: Record<string, unknown>);
11
+ beforeLog(record: LogRecord): LogRecord;
24
12
  }
25
- /** ISO8601 UTC timestamp with millisecond precision, matching Python's `utc_timestamp()`. */
26
- declare function utcTimestamp(): string;
27
- declare function createRecord(params: {
28
- level: Level;
29
- logger: string;
30
- message: string;
31
- meta: Record<string, unknown>;
32
- }): LogRecord;
33
13
 
34
- /** `format(record) -> string`, per the transport contract shared with logquill-python. */
35
- interface Formatter {
36
- format(record: LogRecord): string;
14
+ interface RunPluginOptions {
15
+ runId?: string;
37
16
  }
38
- /** Serializes a record to the canonical JSON line shape. */
39
- declare class JSONFormatter implements Formatter {
40
- format(record: LogRecord): string;
17
+ /**
18
+ * Stamps `meta.runId` a stable id grouping every record from one agent
19
+ * run — plus an incrementing `meta.step` counter, one per record processed
20
+ * through this plugin instance.
21
+ *
22
+ * Distinct from `TraceContextPlugin`'s `traceId`: `runId` scopes one agent
23
+ * run, `traceId` follows one request across services. A run can span
24
+ * multiple traces (e.g. an agent that calls several downstream services);
25
+ * the two ids are independent.
26
+ *
27
+ * One instance is one run: attach a fresh `RunPlugin()` per run (typically
28
+ * via `logger.child("agent").use(new RunPlugin())`), never a process-wide
29
+ * singleton shared across runs — otherwise concurrent runs would share both
30
+ * the run id and the step counter.
31
+ *
32
+ * A record that already carries `meta.runId` (e.g. propagated from an
33
+ * upstream call) keeps its existing value; `meta.step` is always set from
34
+ * this instance's own counter.
35
+ */
36
+ declare class RunPlugin implements Plugin {
37
+ readonly runId: string;
38
+ private step;
39
+ constructor(options?: RunPluginOptions);
40
+ beforeLog(record: LogRecord): LogRecord;
41
41
  }
42
42
 
43
43
  /**
44
- * The plugin pipeline hooks, matching the Python hook names: `beforeLog`,
45
- * `afterLog`, `onError`. All hooks are optionalimplement only what you need.
46
- * A hook that throws cannot crash logging: the pipeline catches it, routes it
47
- * to `onError`, and moves on.
44
+ * Sets the inbound trace header for the current execution context (e.g.
45
+ * request-scoped HTTP middleware, before the handler runs) the
46
+ * propagation mechanism framework middleware uses to hand `TraceContextPlugin`
47
+ * an inbound header without threading it through every log call. Backed by
48
+ * `AsyncLocalStorage`, so it's isolated per concurrent request. Returns a
49
+ * function that restores the previous value; call it (typically in a
50
+ * `finally` block) once the request is done.
48
51
  */
49
- interface Plugin {
50
- /** Return a (possibly modified) record, or `null` to drop it. */
51
- beforeLog?(record: LogRecord): LogRecord | null;
52
- /** Called after the record has been dispatched to every transport. */
53
- afterLog?(record: LogRecord): void;
54
- /** Called when one of this plugin's own hooks throws. */
55
- onError?(error: unknown, record: LogRecord): void;
56
- }
57
- /** A plain `beforeLog`-style function, as accepted by `Logger.use()` in place of a `Plugin`. */
58
- type MiddlewareFunc = (record: LogRecord) => LogRecord | null;
52
+ declare function setTraceparent(value: string | undefined): () => void;
53
+ /** The header most recently set via `setTraceparent()` for this execution context. */
54
+ declare function getTraceparent(): string | undefined;
55
+ /** A fresh 32-hex-char id, matching the shape of an OTel/W3C trace id. */
56
+ declare function generateTraceId(): string;
59
57
  /**
60
- * Wraps a plain `beforeLog`-style function as a `Plugin`. `Logger.use()`
61
- * builds one of these automatically when given a function instead of a
62
- * `Plugin` Express/Koa-style middleware ergonomics, without needing to
63
- * read the `Plugin` interface first. There's no `next()` chaining: the
64
- * pipeline already calls hooks in sequence, so this is sugar for a
65
- * single-method `Plugin`, not a new execution model.
58
+ * Extracts a 32-hex-char trace id from a W3C `traceparent`, AWS X-Ray
59
+ * `X-Amzn-Trace-Id`, or GCP `X-Cloud-Trace-Context` header value. Returns
60
+ * `undefined` if `header` doesn't match any of the three shapes.
66
61
  */
67
- declare class FunctionPlugin implements Plugin {
68
- private readonly func;
69
- constructor(func: MiddlewareFunc);
70
- beforeLog(record: LogRecord): LogRecord | null;
62
+ declare function parseTraceHeader(header: string): string | undefined;
63
+ /**
64
+ * Best-effort, synchronous lookup of the active OpenTelemetry span's trace
65
+ * id via a plain `require("@opentelemetry/api")` `@opentelemetry/api` is
66
+ * never a declared dependency of this package (matching `logquill-python`'s
67
+ * lazy `import opentelemetry`); this returns `undefined` whenever it isn't
68
+ * installed, or no span is currently active, rather than throwing.
69
+ *
70
+ * A `require()` (via `createRequire`) rather than a dynamic `import()` is
71
+ * deliberate: `Plugin.beforeLog` is synchronous, so this has to be too.
72
+ */
73
+ declare function defaultResolveActiveOtelTraceId(): string | undefined;
74
+ interface TraceContextPluginOptions {
75
+ /** `meta` key the trace id is written to. Default `"traceId"`. */
76
+ traceKey?: string;
77
+ /** An inbound trace header to resolve on every record, bypassing `setTraceparent()`. */
78
+ traceparent?: string;
79
+ /** Override for testing, or to plug in a non-Node OTel API surface. Defaults to `defaultResolveActiveOtelTraceId`. */
80
+ resolveActiveOtelTraceId?: () => string | undefined;
71
81
  }
72
-
73
82
  /**
74
- * Injects fixed key/value pairs into every record's `meta`.
75
- * A value already present in a record's own `meta` wins over the fixed context.
83
+ * Stamps `meta.traceId` for cross-service correlation distinct from
84
+ * `RunPlugin`'s `runId`: `traceId` follows one request across services,
85
+ * `runId` scopes one agent run.
86
+ *
87
+ * A record that already carries `meta[traceKey]` (e.g. because
88
+ * `SamplingPlugin`'s tail-based elevation, or an upstream plugin, already
89
+ * set one) is left alone. Otherwise resolves a trace id in priority order:
90
+ *
91
+ * 1. An active OpenTelemetry span's trace id, if `@opentelemetry/api` is
92
+ * installed and a span is current — read directly, not just inbound
93
+ * headers.
94
+ * 2. The `traceparent` constructor option, if given.
95
+ * 3. Whatever `setTraceparent()` most recently set for the current
96
+ * execution context.
97
+ * 4. A freshly generated trace id, if none of the above produced one.
98
+ *
99
+ * Header parsing understands W3C `traceparent`, AWS X-Ray
100
+ * `X-Amzn-Trace-Id`, and GCP `X-Cloud-Trace-Context` — see
101
+ * `parseTraceHeader`. A header that doesn't parse is treated the same as no
102
+ * header: falls through to generating a new trace id.
76
103
  */
77
- declare class ContextPlugin implements Plugin {
78
- readonly context: Record<string, unknown>;
79
- constructor(context: Record<string, unknown>);
104
+ declare class TraceContextPlugin implements Plugin {
105
+ readonly traceKey: string;
106
+ private readonly explicitTraceparent;
107
+ private readonly resolveActiveOtelTraceId;
108
+ constructor(options?: TraceContextPluginOptions);
80
109
  beforeLog(record: LogRecord): LogRecord;
110
+ private resolveTraceId;
81
111
  }
82
112
 
83
113
  declare const DEFAULT_REDACTED_KEYS: readonly string[];
@@ -133,27 +163,6 @@ declare class PIIRedactPlugin implements Plugin {
133
163
  private redactText;
134
164
  }
135
165
 
136
- /**
137
- * Sink for log records, per the cross-language transport contract:
138
- * `format(record) -> string`, `write(formatted, record)`, `close()` on shutdown.
139
- */
140
- declare abstract class Transport {
141
- formatter: Formatter;
142
- constructor(formatter?: Formatter);
143
- format(record: LogRecord): string;
144
- abstract write(formatted: string, record: LogRecord): void;
145
- /** Flush/release resources on shutdown. No-op unless a transport overrides it. */
146
- close(): void;
147
- }
148
- /** In-memory transport for tests: collects every (formatted, record) pair written to it. */
149
- declare class CollectingTransport extends Transport {
150
- readonly formatted: string[];
151
- readonly records: LogRecord[];
152
- closed: boolean;
153
- write(formatted: string, record: LogRecord): void;
154
- close(): void;
155
- }
156
-
157
166
  interface SamplingPluginOptions {
158
167
  rng?: () => number;
159
168
  /** `meta` key holding the trace/run id used to group buffered records. Default `"traceId"`. */
@@ -1233,42 +1242,6 @@ declare class HTTPTransport extends Transport {
1233
1242
  close(): void;
1234
1243
  }
1235
1244
 
1236
- interface LoggerOptions {
1237
- level?: LevelInput;
1238
- transports?: Transport[];
1239
- plugins?: (Plugin | MiddlewareFunc)[];
1240
- meta?: Record<string, unknown>;
1241
- }
1242
- declare class Logger {
1243
- readonly name: string;
1244
- readonly transports: Transport[];
1245
- readonly plugins: Plugin[];
1246
- private currentLevel;
1247
- private readonly baseMeta;
1248
- constructor(name: string, options?: LoggerOptions);
1249
- get level(): Level;
1250
- setLevel(level: LevelInput): void;
1251
- /**
1252
- * Register a plugin, or a plain `beforeLog`-style function. A function is
1253
- * wrapped internally as an anonymous `Plugin` (`FunctionPlugin`) — the
1254
- * same middleware ergonomics as Express/Koa, without needing to read the
1255
- * `Plugin` interface first. Returns `this` so calls can be chained.
1256
- */
1257
- use(plugin: Plugin | MiddlewareFunc): this;
1258
- /** Close every attached transport. Call on shutdown to flush buffered writes. */
1259
- close(): void;
1260
- /** A logger scoped under this one, inheriting its level, transports, and plugins. */
1261
- child(name: string, meta?: Record<string, unknown>): Logger;
1262
- private notifyError;
1263
- private dispatch;
1264
- trace(message: string, meta?: Record<string, unknown>): LogRecord | null;
1265
- debug(message: string, meta?: Record<string, unknown>): LogRecord | null;
1266
- info(message: string, meta?: Record<string, unknown>): LogRecord | null;
1267
- warn(message: string, meta?: Record<string, unknown>): LogRecord | null;
1268
- error(message: string, meta?: Record<string, unknown>): LogRecord | null;
1269
- fatal(message: string, meta?: Record<string, unknown>): LogRecord | null;
1270
- }
1271
-
1272
1245
  declare const VERSION = "0.3.0";
1273
1246
 
1274
- export { AlertingPlugin, type AlertingPluginOptions, type AmqpChannelLike, type AppInsightsClientLike, type AppInsightsTrace, AppInsightsTransport, type AppInsightsTransportOptions, BaseQueueTransport, type BaseQueueTransportOptions, BaseSQLTransport, type BaseSQLTransportOptions, BatchingTransport, type BatchingTransportOptions, type CloudLoggingClientLike, type CloudLoggingEntry, CloudLoggingTransport, type CloudLoggingTransportOptions, type CloudWatchClientLike, type CloudWatchLogEvent, CloudWatchTransport, type CloudWatchTransportOptions, CollectingTransport, type ConsoleLike, ConsoleTransport, type ConsoleTransportOptions, ContextPlugin, DEFAULT_PII_PATTERNS, DEFAULT_REDACTED_KEYS, type DatadogSender, DatadogTransport, type DatadogTransportOptions, type DynamoClientLike, DynamoDBTransport, type DynamoDBTransportOptions, type DynamoLogItem, type ElasticsearchSender, ElasticsearchTransport, type ElasticsearchTransportOptions, EmailAlertPlugin, type EmailAlertPluginOptions, type EmailMessage, type EmailSender, FileTransport, type FileTransportOptions, type Formatter, FunctionPlugin, GENESIS_HASH, HTTPTransport, type HTTPTransportOptions, JSONFormatter, type KafkaProducerLike, KafkaTransport, type KafkaTransportOptions, Level, type LevelInput, type LogRecord, Logger, type LoggerOptions, type MiddlewareFunc, type MongoClientLike, type MongoCollectionLike, MongoDBTransport, type MongoDBTransportOptions, type MySQLClientLike, MySQLTransport, type MySQLTransportOptions, type NewRelicRegion, type NewRelicSender, type NewRelicSenderResult, NewRelicTransport, type NewRelicTransportOptions, type NodemailerTransporterLike, PIIRedactPlugin, type PIIRedactPluginOptions, PagerDutyAlertPlugin, type PagerDutyAlertPluginOptions, type PagerDutySender, type PgClientLike, type Plugin, PostgresTransport, type PostgresTransportOptions, type PubSubTopicLike, PubSubTransport, type PubSubTransportOptions, RabbitMQTransport, type RabbitMQTransportOptions, RedactPlugin, type RedactPluginOptions, type RedisClientLike, RedisTransport, type RedisTransportOptions, type SQLLogRow, type SQLiteClientLike, type SQLiteStatementLike, SQLiteTransport, type SQLiteTransportOptions, type SQSClientLike, SQSTransport, type SQSTransportOptions, SamplingPlugin, type SamplingPluginOptions, type Sender, SlackAlertPlugin, type SlackAlertPluginOptions, type SlackSender, TamperEvidentPlugin, type TamperEvidentPluginOptions, Transport, VERSION, createRecord, levelName, parseLevel, utcTimestamp };
1247
+ export { AlertingPlugin, type AlertingPluginOptions, type AmqpChannelLike, type AppInsightsClientLike, type AppInsightsTrace, AppInsightsTransport, type AppInsightsTransportOptions, BaseQueueTransport, type BaseQueueTransportOptions, BaseSQLTransport, type BaseSQLTransportOptions, BatchingTransport, type BatchingTransportOptions, type CloudLoggingClientLike, type CloudLoggingEntry, CloudLoggingTransport, type CloudLoggingTransportOptions, type CloudWatchClientLike, type CloudWatchLogEvent, CloudWatchTransport, type CloudWatchTransportOptions, type ConsoleLike, ConsoleTransport, type ConsoleTransportOptions, ContextPlugin, DEFAULT_PII_PATTERNS, DEFAULT_REDACTED_KEYS, type DatadogSender, DatadogTransport, type DatadogTransportOptions, type DynamoClientLike, DynamoDBTransport, type DynamoDBTransportOptions, type DynamoLogItem, type ElasticsearchSender, ElasticsearchTransport, type ElasticsearchTransportOptions, EmailAlertPlugin, type EmailAlertPluginOptions, type EmailMessage, type EmailSender, FileTransport, type FileTransportOptions, Formatter, GENESIS_HASH, HTTPTransport, type HTTPTransportOptions, type KafkaProducerLike, KafkaTransport, type KafkaTransportOptions, Level, LevelInput, LogRecord, type MongoClientLike, type MongoCollectionLike, MongoDBTransport, type MongoDBTransportOptions, type MySQLClientLike, MySQLTransport, type MySQLTransportOptions, type NewRelicRegion, type NewRelicSender, type NewRelicSenderResult, NewRelicTransport, type NewRelicTransportOptions, type NodemailerTransporterLike, PIIRedactPlugin, type PIIRedactPluginOptions, PagerDutyAlertPlugin, type PagerDutyAlertPluginOptions, type PagerDutySender, type PgClientLike, Plugin, PostgresTransport, type PostgresTransportOptions, type PubSubTopicLike, PubSubTransport, type PubSubTransportOptions, RabbitMQTransport, type RabbitMQTransportOptions, RedactPlugin, type RedactPluginOptions, type RedisClientLike, RedisTransport, type RedisTransportOptions, RunPlugin, type RunPluginOptions, type SQLLogRow, type SQLiteClientLike, type SQLiteStatementLike, SQLiteTransport, type SQLiteTransportOptions, type SQSClientLike, SQSTransport, type SQSTransportOptions, SamplingPlugin, type SamplingPluginOptions, type Sender, SlackAlertPlugin, type SlackAlertPluginOptions, type SlackSender, TamperEvidentPlugin, type TamperEvidentPluginOptions, TraceContextPlugin, type TraceContextPluginOptions, Transport, VERSION, defaultResolveActiveOtelTraceId, generateTraceId, getTraceparent, parseTraceHeader, setTraceparent };
package/dist/index.d.ts CHANGED
@@ -1,83 +1,113 @@
1
- /** Log levels, shared by name and numeric weight with the logquill-python contract. */
2
- declare enum Level {
3
- TRACE = 5,
4
- DEBUG = 10,
5
- INFO = 20,
6
- WARN = 30,
7
- ERROR = 40,
8
- FATAL = 50
9
- }
10
- /** A level given as a `Level`, a level name (any case), or its numeric weight. */
11
- type LevelInput = Level | number | string;
12
- /** The level's name, e.g. `levelName(Level.INFO) === "INFO"`. */
13
- declare function levelName(level: Level): string;
14
- /** Normalize a level given as a `Level`, level name, or numeric weight. Throws if unknown. */
15
- declare function parseLevel(level: LevelInput): Level;
1
+ import { P as Plugin, L as LogRecord, a as Level, T as Transport, b as LevelInput, F as Formatter } from './logger-D1_THnBJ.js';
2
+ export { C as CollectingTransport, c as FunctionPlugin, J as JSONFormatter, d as Logger, e as LoggerOptions, M as MiddlewareFunc, S as SpanOptions, f as createRecord, l as levelName, p as parseLevel, u as utcTimestamp } from './logger-D1_THnBJ.js';
16
3
 
17
- /** The cross-language record shape shared with logquill-python. */
18
- interface LogRecord {
19
- timestamp: string;
20
- level: string;
21
- logger: string;
22
- message: string;
23
- meta: Record<string, unknown>;
4
+ /**
5
+ * Injects fixed key/value pairs into every record's `meta`.
6
+ * A value already present in a record's own `meta` wins over the fixed context.
7
+ */
8
+ declare class ContextPlugin implements Plugin {
9
+ readonly context: Record<string, unknown>;
10
+ constructor(context: Record<string, unknown>);
11
+ beforeLog(record: LogRecord): LogRecord;
24
12
  }
25
- /** ISO8601 UTC timestamp with millisecond precision, matching Python's `utc_timestamp()`. */
26
- declare function utcTimestamp(): string;
27
- declare function createRecord(params: {
28
- level: Level;
29
- logger: string;
30
- message: string;
31
- meta: Record<string, unknown>;
32
- }): LogRecord;
33
13
 
34
- /** `format(record) -> string`, per the transport contract shared with logquill-python. */
35
- interface Formatter {
36
- format(record: LogRecord): string;
14
+ interface RunPluginOptions {
15
+ runId?: string;
37
16
  }
38
- /** Serializes a record to the canonical JSON line shape. */
39
- declare class JSONFormatter implements Formatter {
40
- format(record: LogRecord): string;
17
+ /**
18
+ * Stamps `meta.runId` a stable id grouping every record from one agent
19
+ * run — plus an incrementing `meta.step` counter, one per record processed
20
+ * through this plugin instance.
21
+ *
22
+ * Distinct from `TraceContextPlugin`'s `traceId`: `runId` scopes one agent
23
+ * run, `traceId` follows one request across services. A run can span
24
+ * multiple traces (e.g. an agent that calls several downstream services);
25
+ * the two ids are independent.
26
+ *
27
+ * One instance is one run: attach a fresh `RunPlugin()` per run (typically
28
+ * via `logger.child("agent").use(new RunPlugin())`), never a process-wide
29
+ * singleton shared across runs — otherwise concurrent runs would share both
30
+ * the run id and the step counter.
31
+ *
32
+ * A record that already carries `meta.runId` (e.g. propagated from an
33
+ * upstream call) keeps its existing value; `meta.step` is always set from
34
+ * this instance's own counter.
35
+ */
36
+ declare class RunPlugin implements Plugin {
37
+ readonly runId: string;
38
+ private step;
39
+ constructor(options?: RunPluginOptions);
40
+ beforeLog(record: LogRecord): LogRecord;
41
41
  }
42
42
 
43
43
  /**
44
- * The plugin pipeline hooks, matching the Python hook names: `beforeLog`,
45
- * `afterLog`, `onError`. All hooks are optionalimplement only what you need.
46
- * A hook that throws cannot crash logging: the pipeline catches it, routes it
47
- * to `onError`, and moves on.
44
+ * Sets the inbound trace header for the current execution context (e.g.
45
+ * request-scoped HTTP middleware, before the handler runs) the
46
+ * propagation mechanism framework middleware uses to hand `TraceContextPlugin`
47
+ * an inbound header without threading it through every log call. Backed by
48
+ * `AsyncLocalStorage`, so it's isolated per concurrent request. Returns a
49
+ * function that restores the previous value; call it (typically in a
50
+ * `finally` block) once the request is done.
48
51
  */
49
- interface Plugin {
50
- /** Return a (possibly modified) record, or `null` to drop it. */
51
- beforeLog?(record: LogRecord): LogRecord | null;
52
- /** Called after the record has been dispatched to every transport. */
53
- afterLog?(record: LogRecord): void;
54
- /** Called when one of this plugin's own hooks throws. */
55
- onError?(error: unknown, record: LogRecord): void;
56
- }
57
- /** A plain `beforeLog`-style function, as accepted by `Logger.use()` in place of a `Plugin`. */
58
- type MiddlewareFunc = (record: LogRecord) => LogRecord | null;
52
+ declare function setTraceparent(value: string | undefined): () => void;
53
+ /** The header most recently set via `setTraceparent()` for this execution context. */
54
+ declare function getTraceparent(): string | undefined;
55
+ /** A fresh 32-hex-char id, matching the shape of an OTel/W3C trace id. */
56
+ declare function generateTraceId(): string;
59
57
  /**
60
- * Wraps a plain `beforeLog`-style function as a `Plugin`. `Logger.use()`
61
- * builds one of these automatically when given a function instead of a
62
- * `Plugin` Express/Koa-style middleware ergonomics, without needing to
63
- * read the `Plugin` interface first. There's no `next()` chaining: the
64
- * pipeline already calls hooks in sequence, so this is sugar for a
65
- * single-method `Plugin`, not a new execution model.
58
+ * Extracts a 32-hex-char trace id from a W3C `traceparent`, AWS X-Ray
59
+ * `X-Amzn-Trace-Id`, or GCP `X-Cloud-Trace-Context` header value. Returns
60
+ * `undefined` if `header` doesn't match any of the three shapes.
66
61
  */
67
- declare class FunctionPlugin implements Plugin {
68
- private readonly func;
69
- constructor(func: MiddlewareFunc);
70
- beforeLog(record: LogRecord): LogRecord | null;
62
+ declare function parseTraceHeader(header: string): string | undefined;
63
+ /**
64
+ * Best-effort, synchronous lookup of the active OpenTelemetry span's trace
65
+ * id via a plain `require("@opentelemetry/api")` `@opentelemetry/api` is
66
+ * never a declared dependency of this package (matching `logquill-python`'s
67
+ * lazy `import opentelemetry`); this returns `undefined` whenever it isn't
68
+ * installed, or no span is currently active, rather than throwing.
69
+ *
70
+ * A `require()` (via `createRequire`) rather than a dynamic `import()` is
71
+ * deliberate: `Plugin.beforeLog` is synchronous, so this has to be too.
72
+ */
73
+ declare function defaultResolveActiveOtelTraceId(): string | undefined;
74
+ interface TraceContextPluginOptions {
75
+ /** `meta` key the trace id is written to. Default `"traceId"`. */
76
+ traceKey?: string;
77
+ /** An inbound trace header to resolve on every record, bypassing `setTraceparent()`. */
78
+ traceparent?: string;
79
+ /** Override for testing, or to plug in a non-Node OTel API surface. Defaults to `defaultResolveActiveOtelTraceId`. */
80
+ resolveActiveOtelTraceId?: () => string | undefined;
71
81
  }
72
-
73
82
  /**
74
- * Injects fixed key/value pairs into every record's `meta`.
75
- * A value already present in a record's own `meta` wins over the fixed context.
83
+ * Stamps `meta.traceId` for cross-service correlation distinct from
84
+ * `RunPlugin`'s `runId`: `traceId` follows one request across services,
85
+ * `runId` scopes one agent run.
86
+ *
87
+ * A record that already carries `meta[traceKey]` (e.g. because
88
+ * `SamplingPlugin`'s tail-based elevation, or an upstream plugin, already
89
+ * set one) is left alone. Otherwise resolves a trace id in priority order:
90
+ *
91
+ * 1. An active OpenTelemetry span's trace id, if `@opentelemetry/api` is
92
+ * installed and a span is current — read directly, not just inbound
93
+ * headers.
94
+ * 2. The `traceparent` constructor option, if given.
95
+ * 3. Whatever `setTraceparent()` most recently set for the current
96
+ * execution context.
97
+ * 4. A freshly generated trace id, if none of the above produced one.
98
+ *
99
+ * Header parsing understands W3C `traceparent`, AWS X-Ray
100
+ * `X-Amzn-Trace-Id`, and GCP `X-Cloud-Trace-Context` — see
101
+ * `parseTraceHeader`. A header that doesn't parse is treated the same as no
102
+ * header: falls through to generating a new trace id.
76
103
  */
77
- declare class ContextPlugin implements Plugin {
78
- readonly context: Record<string, unknown>;
79
- constructor(context: Record<string, unknown>);
104
+ declare class TraceContextPlugin implements Plugin {
105
+ readonly traceKey: string;
106
+ private readonly explicitTraceparent;
107
+ private readonly resolveActiveOtelTraceId;
108
+ constructor(options?: TraceContextPluginOptions);
80
109
  beforeLog(record: LogRecord): LogRecord;
110
+ private resolveTraceId;
81
111
  }
82
112
 
83
113
  declare const DEFAULT_REDACTED_KEYS: readonly string[];
@@ -133,27 +163,6 @@ declare class PIIRedactPlugin implements Plugin {
133
163
  private redactText;
134
164
  }
135
165
 
136
- /**
137
- * Sink for log records, per the cross-language transport contract:
138
- * `format(record) -> string`, `write(formatted, record)`, `close()` on shutdown.
139
- */
140
- declare abstract class Transport {
141
- formatter: Formatter;
142
- constructor(formatter?: Formatter);
143
- format(record: LogRecord): string;
144
- abstract write(formatted: string, record: LogRecord): void;
145
- /** Flush/release resources on shutdown. No-op unless a transport overrides it. */
146
- close(): void;
147
- }
148
- /** In-memory transport for tests: collects every (formatted, record) pair written to it. */
149
- declare class CollectingTransport extends Transport {
150
- readonly formatted: string[];
151
- readonly records: LogRecord[];
152
- closed: boolean;
153
- write(formatted: string, record: LogRecord): void;
154
- close(): void;
155
- }
156
-
157
166
  interface SamplingPluginOptions {
158
167
  rng?: () => number;
159
168
  /** `meta` key holding the trace/run id used to group buffered records. Default `"traceId"`. */
@@ -1233,42 +1242,6 @@ declare class HTTPTransport extends Transport {
1233
1242
  close(): void;
1234
1243
  }
1235
1244
 
1236
- interface LoggerOptions {
1237
- level?: LevelInput;
1238
- transports?: Transport[];
1239
- plugins?: (Plugin | MiddlewareFunc)[];
1240
- meta?: Record<string, unknown>;
1241
- }
1242
- declare class Logger {
1243
- readonly name: string;
1244
- readonly transports: Transport[];
1245
- readonly plugins: Plugin[];
1246
- private currentLevel;
1247
- private readonly baseMeta;
1248
- constructor(name: string, options?: LoggerOptions);
1249
- get level(): Level;
1250
- setLevel(level: LevelInput): void;
1251
- /**
1252
- * Register a plugin, or a plain `beforeLog`-style function. A function is
1253
- * wrapped internally as an anonymous `Plugin` (`FunctionPlugin`) — the
1254
- * same middleware ergonomics as Express/Koa, without needing to read the
1255
- * `Plugin` interface first. Returns `this` so calls can be chained.
1256
- */
1257
- use(plugin: Plugin | MiddlewareFunc): this;
1258
- /** Close every attached transport. Call on shutdown to flush buffered writes. */
1259
- close(): void;
1260
- /** A logger scoped under this one, inheriting its level, transports, and plugins. */
1261
- child(name: string, meta?: Record<string, unknown>): Logger;
1262
- private notifyError;
1263
- private dispatch;
1264
- trace(message: string, meta?: Record<string, unknown>): LogRecord | null;
1265
- debug(message: string, meta?: Record<string, unknown>): LogRecord | null;
1266
- info(message: string, meta?: Record<string, unknown>): LogRecord | null;
1267
- warn(message: string, meta?: Record<string, unknown>): LogRecord | null;
1268
- error(message: string, meta?: Record<string, unknown>): LogRecord | null;
1269
- fatal(message: string, meta?: Record<string, unknown>): LogRecord | null;
1270
- }
1271
-
1272
1245
  declare const VERSION = "0.3.0";
1273
1246
 
1274
- export { AlertingPlugin, type AlertingPluginOptions, type AmqpChannelLike, type AppInsightsClientLike, type AppInsightsTrace, AppInsightsTransport, type AppInsightsTransportOptions, BaseQueueTransport, type BaseQueueTransportOptions, BaseSQLTransport, type BaseSQLTransportOptions, BatchingTransport, type BatchingTransportOptions, type CloudLoggingClientLike, type CloudLoggingEntry, CloudLoggingTransport, type CloudLoggingTransportOptions, type CloudWatchClientLike, type CloudWatchLogEvent, CloudWatchTransport, type CloudWatchTransportOptions, CollectingTransport, type ConsoleLike, ConsoleTransport, type ConsoleTransportOptions, ContextPlugin, DEFAULT_PII_PATTERNS, DEFAULT_REDACTED_KEYS, type DatadogSender, DatadogTransport, type DatadogTransportOptions, type DynamoClientLike, DynamoDBTransport, type DynamoDBTransportOptions, type DynamoLogItem, type ElasticsearchSender, ElasticsearchTransport, type ElasticsearchTransportOptions, EmailAlertPlugin, type EmailAlertPluginOptions, type EmailMessage, type EmailSender, FileTransport, type FileTransportOptions, type Formatter, FunctionPlugin, GENESIS_HASH, HTTPTransport, type HTTPTransportOptions, JSONFormatter, type KafkaProducerLike, KafkaTransport, type KafkaTransportOptions, Level, type LevelInput, type LogRecord, Logger, type LoggerOptions, type MiddlewareFunc, type MongoClientLike, type MongoCollectionLike, MongoDBTransport, type MongoDBTransportOptions, type MySQLClientLike, MySQLTransport, type MySQLTransportOptions, type NewRelicRegion, type NewRelicSender, type NewRelicSenderResult, NewRelicTransport, type NewRelicTransportOptions, type NodemailerTransporterLike, PIIRedactPlugin, type PIIRedactPluginOptions, PagerDutyAlertPlugin, type PagerDutyAlertPluginOptions, type PagerDutySender, type PgClientLike, type Plugin, PostgresTransport, type PostgresTransportOptions, type PubSubTopicLike, PubSubTransport, type PubSubTransportOptions, RabbitMQTransport, type RabbitMQTransportOptions, RedactPlugin, type RedactPluginOptions, type RedisClientLike, RedisTransport, type RedisTransportOptions, type SQLLogRow, type SQLiteClientLike, type SQLiteStatementLike, SQLiteTransport, type SQLiteTransportOptions, type SQSClientLike, SQSTransport, type SQSTransportOptions, SamplingPlugin, type SamplingPluginOptions, type Sender, SlackAlertPlugin, type SlackAlertPluginOptions, type SlackSender, TamperEvidentPlugin, type TamperEvidentPluginOptions, Transport, VERSION, createRecord, levelName, parseLevel, utcTimestamp };
1247
+ export { AlertingPlugin, type AlertingPluginOptions, type AmqpChannelLike, type AppInsightsClientLike, type AppInsightsTrace, AppInsightsTransport, type AppInsightsTransportOptions, BaseQueueTransport, type BaseQueueTransportOptions, BaseSQLTransport, type BaseSQLTransportOptions, BatchingTransport, type BatchingTransportOptions, type CloudLoggingClientLike, type CloudLoggingEntry, CloudLoggingTransport, type CloudLoggingTransportOptions, type CloudWatchClientLike, type CloudWatchLogEvent, CloudWatchTransport, type CloudWatchTransportOptions, type ConsoleLike, ConsoleTransport, type ConsoleTransportOptions, ContextPlugin, DEFAULT_PII_PATTERNS, DEFAULT_REDACTED_KEYS, type DatadogSender, DatadogTransport, type DatadogTransportOptions, type DynamoClientLike, DynamoDBTransport, type DynamoDBTransportOptions, type DynamoLogItem, type ElasticsearchSender, ElasticsearchTransport, type ElasticsearchTransportOptions, EmailAlertPlugin, type EmailAlertPluginOptions, type EmailMessage, type EmailSender, FileTransport, type FileTransportOptions, Formatter, GENESIS_HASH, HTTPTransport, type HTTPTransportOptions, type KafkaProducerLike, KafkaTransport, type KafkaTransportOptions, Level, LevelInput, LogRecord, type MongoClientLike, type MongoCollectionLike, MongoDBTransport, type MongoDBTransportOptions, type MySQLClientLike, MySQLTransport, type MySQLTransportOptions, type NewRelicRegion, type NewRelicSender, type NewRelicSenderResult, NewRelicTransport, type NewRelicTransportOptions, type NodemailerTransporterLike, PIIRedactPlugin, type PIIRedactPluginOptions, PagerDutyAlertPlugin, type PagerDutyAlertPluginOptions, type PagerDutySender, type PgClientLike, Plugin, PostgresTransport, type PostgresTransportOptions, type PubSubTopicLike, PubSubTransport, type PubSubTransportOptions, RabbitMQTransport, type RabbitMQTransportOptions, RedactPlugin, type RedactPluginOptions, type RedisClientLike, RedisTransport, type RedisTransportOptions, RunPlugin, type RunPluginOptions, type SQLLogRow, type SQLiteClientLike, type SQLiteStatementLike, SQLiteTransport, type SQLiteTransportOptions, type SQSClientLike, SQSTransport, type SQSTransportOptions, SamplingPlugin, type SamplingPluginOptions, type Sender, SlackAlertPlugin, type SlackAlertPluginOptions, type SlackSender, TamperEvidentPlugin, type TamperEvidentPluginOptions, TraceContextPlugin, type TraceContextPluginOptions, Transport, VERSION, defaultResolveActiveOtelTraceId, generateTraceId, getTraceparent, parseTraceHeader, setTraceparent };