@visulima/pail 4.0.0-alpha.11 → 4.0.0-alpha.13

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.
Files changed (127) hide show
  1. package/CHANGELOG.md +33 -0
  2. package/LICENSE.md +131 -900
  3. package/dist/error.d.ts +95 -96
  4. package/dist/error.js +5 -76
  5. package/dist/index.browser.d.ts +557 -47
  6. package/dist/index.browser.js +19 -1499
  7. package/dist/index.server.d.ts +722 -61
  8. package/dist/index.server.js +31 -2803
  9. package/dist/middleware/elysia.d.ts +65 -63
  10. package/dist/middleware/elysia.js +1 -70
  11. package/dist/middleware/express.d.ts +73 -71
  12. package/dist/middleware/express.js +1 -29
  13. package/dist/middleware/fastify.d.ts +69 -67
  14. package/dist/middleware/fastify.js +1 -46
  15. package/dist/middleware/hono.d.ts +74 -72
  16. package/dist/middleware/hono.js +1 -33
  17. package/dist/middleware/next/handler.d.ts +101 -34
  18. package/dist/middleware/next/handler.js +1 -53
  19. package/dist/middleware/sveltekit.d.ts +100 -98
  20. package/dist/middleware/sveltekit.js +1 -43
  21. package/dist/object-tree.d.ts +67 -70
  22. package/dist/object-tree.js +2 -89
  23. package/dist/packem_shared/AbstractJsonReporter-BawNFYxI.js +1 -0
  24. package/dist/packem_shared/AbstractJsonReporter-Dt5-5x2R.js +1 -0
  25. package/dist/packem_shared/JsonReporter-15ln04rX.js +2 -0
  26. package/dist/packem_shared/JsonReporter-CerNvhg-.js +1 -0
  27. package/dist/packem_shared/PrettyReporter-Br5njEEy.js +5 -0
  28. package/dist/packem_shared/PrettyReporter-CgXnwIip.js +44 -0
  29. package/dist/packem_shared/abstract-json-reporter.d-Bz3lZEDi.d.ts +61 -0
  30. package/dist/packem_shared/abstract-pretty-reporter-BiHgKxG_.js +33 -0
  31. package/dist/packem_shared/constants-DKfCaSUR.js +1 -0
  32. package/dist/packem_shared/constants-ep2nsfdy.js +1 -0
  33. package/dist/packem_shared/create-middleware-logger.d-DeNLii5l.d.ts +47 -0
  34. package/dist/packem_shared/createPailError-53jAfQ-T.js +5 -0
  35. package/dist/packem_shared/headers-S8WyX-2m.js +1 -0
  36. package/dist/packem_shared/index-Bzp4LnJ5.js +1 -0
  37. package/dist/packem_shared/pailMiddleware-CWOJRmJq.js +1 -0
  38. package/dist/packem_shared/storage-DHM1EP2P.js +1 -0
  39. package/dist/packem_shared/types.d-ByY2ZuYG.d.ts +246 -0
  40. package/dist/packem_shared/types.d-C51XNfQz.d.ts +194 -0
  41. package/dist/packem_shared/types.d-CM5ie2qm.d.ts +236 -0
  42. package/dist/packem_shared/useLogger-qyZrjy4D.js +1 -0
  43. package/dist/packem_shared/wide-event.d-Ke44zirU.d.ts +704 -0
  44. package/dist/packem_shared/write-console-log-based-on-level-DfD64owL.js +1 -0
  45. package/dist/packem_shared/write-stream-Dd4_PTcm.js +1 -0
  46. package/dist/processor/caller/caller-processor.d.ts +49 -47
  47. package/dist/processor/caller/caller-processor.js +1 -59
  48. package/dist/processor/environment-processor.d.ts +113 -113
  49. package/dist/processor/environment-processor.js +1 -89
  50. package/dist/processor/message-formatter-processor.d.ts +51 -49
  51. package/dist/processor/message-formatter-processor.js +1 -715
  52. package/dist/processor/opentelemetry-processor.d.ts +69 -66
  53. package/dist/processor/opentelemetry-processor.js +1 -52
  54. package/dist/processor/redact-processor.d.ts +60 -41
  55. package/dist/processor/redact-processor.js +1 -31
  56. package/dist/processor/sampling-processor.d.ts +102 -101
  57. package/dist/processor/sampling-processor.js +1 -59
  58. package/dist/reporter/file/json-file-reporter.d.ts +91 -42
  59. package/dist/reporter/file/json-file-reporter.js +2 -136
  60. package/dist/reporter/http/abstract-http-reporter.d.ts +213 -211
  61. package/dist/reporter/http/abstract-http-reporter.js +2 -432
  62. package/dist/reporter/http/http-reporter.d.ts +40 -36
  63. package/dist/reporter/http/http-reporter.edge-light.d.ts +430 -38
  64. package/dist/reporter/http/http-reporter.edge-light.js +2 -728
  65. package/dist/reporter/http/http-reporter.js +1 -13
  66. package/dist/reporter/json/index.browser.d.ts +99 -3
  67. package/dist/reporter/json/index.browser.js +1 -2
  68. package/dist/reporter/json/index.d.ts +51 -3
  69. package/dist/reporter/json/index.js +1 -2
  70. package/dist/reporter/pretty/index.browser.d.ts +106 -2
  71. package/dist/reporter/pretty/index.browser.js +1 -1
  72. package/dist/reporter/pretty/index.d.ts +186 -2
  73. package/dist/reporter/pretty/index.js +1 -1
  74. package/dist/reporter/simple/simple-reporter.server.d.ts +168 -18
  75. package/dist/reporter/simple/simple-reporter.server.js +8 -182
  76. package/dist/wide-event.d.ts +5 -300
  77. package/dist/wide-event.js +1 -284
  78. package/package.json +29 -30
  79. package/dist/constants.d.ts +0 -37
  80. package/dist/middleware/next/middleware.d.ts +0 -59
  81. package/dist/middleware/next/storage.d.ts +0 -14
  82. package/dist/middleware/shared/create-middleware-logger.d.ts +0 -82
  83. package/dist/middleware/shared/headers.d.ts +0 -14
  84. package/dist/middleware/shared/routes.d.ts +0 -30
  85. package/dist/middleware/shared/storage.d.ts +0 -29
  86. package/dist/packem_shared/AbstractJsonReporter-BO8Calb4.js +0 -284
  87. package/dist/packem_shared/AbstractJsonReporter-nOj0Ft1F.js +0 -284
  88. package/dist/packem_shared/JsonReporter-CCmj7oYL.js +0 -28
  89. package/dist/packem_shared/JsonReporter-Ck2PIAEw.js +0 -58
  90. package/dist/packem_shared/PrettyReporter-BCvyNzXO.js +0 -2720
  91. package/dist/packem_shared/PrettyReporter-BtTr13Ha.js +0 -213
  92. package/dist/packem_shared/abstract-pretty-reporter-CXAKYCb8.js +0 -2635
  93. package/dist/packem_shared/constants-B1RjD_ps.js +0 -99
  94. package/dist/packem_shared/constants-omsTHUWB.js +0 -119
  95. package/dist/packem_shared/createPailError-B_sgL0nF.js +0 -76
  96. package/dist/packem_shared/headers-BxHWM6KI.js +0 -127
  97. package/dist/packem_shared/index-Bx3-C0j9.js +0 -658
  98. package/dist/packem_shared/pailMiddleware-Ci88geIF.js +0 -24
  99. package/dist/packem_shared/storage-D0vqz8OX.js +0 -36
  100. package/dist/packem_shared/useLogger-D0rU3lcX.js +0 -33
  101. package/dist/packem_shared/write-console-log-based-on-level-ree2lDPw.js +0 -15
  102. package/dist/packem_shared/write-stream-MDqyXmc_.js +0 -6
  103. package/dist/pail.browser.d.ts +0 -412
  104. package/dist/pail.server.d.ts +0 -158
  105. package/dist/processor/caller/get-caller-filename.d.ts +0 -23
  106. package/dist/reporter/file/utils/rotating-file-stream.d.ts +0 -48
  107. package/dist/reporter/http/utils/compression.d.ts +0 -7
  108. package/dist/reporter/http/utils/log-size-error.d.ts +0 -30
  109. package/dist/reporter/http/utils/retry.d.ts +0 -27
  110. package/dist/reporter/json/abstract-json-reporter.d.ts +0 -61
  111. package/dist/reporter/json/json-reporter.browser.d.ts +0 -40
  112. package/dist/reporter/json/json-reporter.server.d.ts +0 -50
  113. package/dist/reporter/pretty/abstract-pretty-reporter.d.ts +0 -83
  114. package/dist/reporter/pretty/pretty-reporter.browser.d.ts +0 -36
  115. package/dist/reporter/pretty/pretty-reporter.server.d.ts +0 -70
  116. package/dist/reporter/raw/raw-reporter.browser.d.ts +0 -5
  117. package/dist/reporter/raw/raw-reporter.server.d.ts +0 -13
  118. package/dist/reporter/utils/default-inspector-config.d.ts +0 -3
  119. package/dist/reporter/utils/format-label.d.ts +0 -3
  120. package/dist/types.d.ts +0 -241
  121. package/dist/utils/arrayify.d.ts +0 -2
  122. package/dist/utils/get-longest-badge.d.ts +0 -4
  123. package/dist/utils/get-longest-label.d.ts +0 -4
  124. package/dist/utils/merge-types.d.ts +0 -4
  125. package/dist/utils/stream/safe-stream-handler.d.ts +0 -21
  126. package/dist/utils/write-console-log-based-on-level.d.ts +0 -4
  127. package/dist/utils/write-stream.d.ts +0 -2
@@ -1,70 +1,73 @@
1
- import type { Meta, Processor } from "../types.d.ts";
1
+ import { M as Meta, P as Processor } from "../packem_shared/types.d-ByY2ZuYG.js";
2
+ import '@visulima/colorize';
3
+ import '@visulima/interactive-manager';
2
4
  /**
3
- * Configuration options for the OpenTelemetry processor.
4
- */
5
- export interface OpenTelemetryProcessorOptions {
6
- /**
7
- * Field name for the span ID. Defaults to 'span_id'
8
- */
9
- spanIdFieldName?: string;
10
- /**
11
- * If specified, all trace fields will be nested under this key
12
- */
13
- traceFieldName?: string;
14
- /**
15
- * Field name for the trace flags. Defaults to 'trace_flags'
16
- */
17
- traceFlagsFieldName?: string;
18
- /**
19
- * Field name for the trace ID. Defaults to 'trace_id'
20
- */
21
- traceIdFieldName?: string;
5
+ * Configuration options for the OpenTelemetry processor.
6
+ */
7
+ interface OpenTelemetryProcessorOptions {
8
+ /**
9
+ * Field name for the span ID. Defaults to 'span_id'
10
+ */
11
+ spanIdFieldName?: string;
12
+ /**
13
+ * If specified, all trace fields will be nested under this key
14
+ */
15
+ traceFieldName?: string;
16
+ /**
17
+ * Field name for the trace flags. Defaults to 'trace_flags'
18
+ */
19
+ traceFlagsFieldName?: string;
20
+ /**
21
+ * Field name for the trace ID. Defaults to 'trace_id'
22
+ */
23
+ traceIdFieldName?: string;
22
24
  }
23
25
  /**
24
- * OpenTelemetry Processor.
25
- *
26
- * A processor that adds OpenTelemetry trace context to log metadata.
27
- * Extracts trace ID, span ID, and trace flags from the active OpenTelemetry span
28
- * and adds them to the log context for distributed tracing correlation.
29
- * @template L - The log level type
30
- * @example
31
- * ```typescript
32
- * import { createPail } from "@visulima/pail";
33
- * import { OpenTelemetryProcessor } from "@visulima/pail/processor/opentelemetry";
34
- *
35
- * const logger = createPail({
36
- * processors: [new OpenTelemetryProcessor()]
37
- * });
38
- *
39
- * logger.info("Processing request");
40
- * // Context includes: { trace_id: "...", span_id: "...", trace_flags: "01" }
41
- * ```
42
- * @example
43
- * ```typescript
44
- * // With custom field names
45
- * const logger = createPail({
46
- * processors: [new OpenTelemetryProcessor({
47
- * traceFieldName: "trace",
48
- * traceIdFieldName: "traceId",
49
- * spanIdFieldName: "spanId"
50
- * })]
51
- * });
52
- * ```
53
- */
54
- export declare class OpenTelemetryProcessor<L extends string = string> implements Processor<L> {
55
- #private;
56
- /**
57
- * Creates a new OpenTelemetryProcessor instance.
58
- * @param options Configuration options for field names and nesting
59
- */
60
- constructor(options?: OpenTelemetryProcessorOptions);
61
- /**
62
- * Processes log metadata to add OpenTelemetry trace context.
63
- *
64
- * Extracts trace information from the active OpenTelemetry span and adds it
65
- * to the log context. If no valid span is found, the metadata is returned unchanged.
66
- * @param meta The log metadata to process
67
- * @returns The processed metadata with trace context added
68
- */
69
- process(meta: Meta<L>): Meta<L>;
26
+ * OpenTelemetry Processor.
27
+ *
28
+ * A processor that adds OpenTelemetry trace context to log metadata.
29
+ * Extracts trace ID, span ID, and trace flags from the active OpenTelemetry span
30
+ * and adds them to the log context for distributed tracing correlation.
31
+ * @template L - The log level type
32
+ * @example
33
+ * ```typescript
34
+ * import { createPail } from "@visulima/pail";
35
+ * import { OpenTelemetryProcessor } from "@visulima/pail/processor/opentelemetry";
36
+ *
37
+ * const logger = createPail({
38
+ * processors: [new OpenTelemetryProcessor()]
39
+ * });
40
+ *
41
+ * logger.info("Processing request");
42
+ * // Context includes: { trace_id: "...", span_id: "...", trace_flags: "01" }
43
+ * ```
44
+ * @example
45
+ * ```typescript
46
+ * // With custom field names
47
+ * const logger = createPail({
48
+ * processors: [new OpenTelemetryProcessor({
49
+ * traceFieldName: "trace",
50
+ * traceIdFieldName: "traceId",
51
+ * spanIdFieldName: "spanId"
52
+ * })]
53
+ * });
54
+ * ```
55
+ */
56
+ declare class OpenTelemetryProcessor<L extends string = string> implements Processor<L> {
57
+ #private;
58
+ /**
59
+ * Creates a new OpenTelemetryProcessor instance.
60
+ * @param options Configuration options for field names and nesting
61
+ */
62
+ constructor(options?: OpenTelemetryProcessorOptions);
63
+ /**
64
+ * Processes log metadata to add OpenTelemetry trace context.
65
+ *
66
+ * Extracts trace information from the active OpenTelemetry span and adds it
67
+ * to the log context. If no valid span is found, the metadata is returned unchanged.
68
+ * @param meta The log metadata to process
69
+ * @returns The processed metadata with trace context added
70
+ */
71
+ process(meta: Meta<L>): Meta<L>;
70
72
  }
73
+ export { OpenTelemetryProcessor, OpenTelemetryProcessorOptions };
@@ -1,52 +1 @@
1
- import { trace, context, isSpanContextValid } from '@opentelemetry/api';
2
-
3
- class OpenTelemetryProcessor {
4
- /** Field name for trace ID */
5
- #traceIdField;
6
- /** Field name for span ID */
7
- #spanIdField;
8
- /** Field name for trace flags */
9
- #traceFlagsField;
10
- /** Optional field name to nest all trace fields under */
11
- #traceFieldName;
12
- /**
13
- * Creates a new OpenTelemetryProcessor instance.
14
- * @param options Configuration options for field names and nesting
15
- */
16
- constructor(options = {}) {
17
- this.#traceIdField = options.traceIdFieldName ?? "trace_id";
18
- this.#spanIdField = options.spanIdFieldName ?? "span_id";
19
- this.#traceFlagsField = options.traceFlagsFieldName ?? "trace_flags";
20
- this.#traceFieldName = options.traceFieldName;
21
- }
22
- /**
23
- * Processes log metadata to add OpenTelemetry trace context.
24
- *
25
- * Extracts trace information from the active OpenTelemetry span and adds it
26
- * to the log context. If no valid span is found, the metadata is returned unchanged.
27
- * @param meta The log metadata to process
28
- * @returns The processed metadata with trace context added
29
- */
30
- process(meta) {
31
- const span = trace.getSpan(context.active());
32
- if (!span) {
33
- return meta;
34
- }
35
- const spanContext = span.spanContext();
36
- if (!isSpanContextValid(spanContext)) {
37
- return meta;
38
- }
39
- const fields = {
40
- [this.#spanIdField]: spanContext.spanId,
41
- [this.#traceFlagsField]: `0${spanContext.traceFlags.toString(16)}`,
42
- [this.#traceIdField]: spanContext.traceId
43
- };
44
- const traceData = this.#traceFieldName ? {
45
- [this.#traceFieldName]: fields
46
- } : fields;
47
- meta.context = [...meta.context ?? [], traceData];
48
- return meta;
49
- }
50
- }
51
-
52
- export { OpenTelemetryProcessor };
1
+ var n=Object.defineProperty;var s=(e,t)=>n(e,"name",{value:t,configurable:!0});import{trace as o,context as l,isSpanContextValid as d}from"@opentelemetry/api";var p=Object.defineProperty,h=s((e,t)=>p(e,"name",{value:t,configurable:!0}),"i");class f{static{s(this,"OpenTelemetryProcessor")}static{h(this,"OpenTelemetryProcessor")}#e;#r;#s;#t;constructor(t={}){this.#e=t.traceIdFieldName??"trace_id",this.#r=t.spanIdFieldName??"span_id",this.#s=t.traceFlagsFieldName??"trace_flags",this.#t=t.traceFieldName}process(t){const a=o.getSpan(l.active());if(!a)return t;const r=a.spanContext();if(!d(r))return t;const i={[this.#r]:r.spanId,[this.#s]:`0${r.traceFlags.toString(16)}`,[this.#e]:r.traceId},c=this.#t?{[this.#t]:i}:i;return t.context=[...t.context??[],c],t}}export{f as OpenTelemetryProcessor};
@@ -1,44 +1,63 @@
1
- import type { RedactOptions, Rules } from "@visulima/redact";
2
- import type { Meta, Processor } from "../types.d.ts";
1
+ import { M as Meta, P as Processor } from "../packem_shared/types.d-ByY2ZuYG.js";
2
+ import '@visulima/colorize';
3
+ import '@visulima/interactive-manager';
4
+ type StringAnonymize = {
5
+ key: string;
6
+ pattern: RegExp | string;
7
+ replacement?: string;
8
+ };
9
+ type Anonymize = {
10
+ deep?: boolean;
11
+ key: string;
12
+ pattern?: RegExp | string;
13
+ replacement?: unknown;
14
+ };
15
+ type Rules = (Anonymize | StringAnonymize | number | string)[];
16
+ type RedactOptions = {
17
+ exclude?: (number | string)[];
18
+ logger?: {
19
+ debug: (message?: unknown, ...optionalParameters: unknown[]) => void;
20
+ };
21
+ };
3
22
  /**
4
- * Redact Processor.
5
- *
6
- * A processor that redacts sensitive information from log messages and metadata.
7
- * Uses the {@link https://visulima.com/packages/redact/|@visulima/redact} library to identify and mask sensitive data like
8
- * passwords, API keys, credit card numbers, and other PII.
9
- * @template L - The log level type
10
- * @example
11
- * ```typescript
12
- * import { createPail } from "@visulima/pail";
13
- * import RedactProcessor from "@visulima/pail/processor/redact";
14
- *
15
- * const logger = createPail({
16
- * processors: [new RedactProcessor()]
17
- * });
18
- *
19
- * logger.info("User login", {
20
- * username: "john",
21
- * password: "secret123", // Will be redacted
22
- * apiKey: "sk-123456" // Will be redacted
23
- * });
24
- * ```
25
- */
23
+ * Redact Processor.
24
+ *
25
+ * A processor that redacts sensitive information from log messages and metadata.
26
+ * Uses the {@link https://visulima.com/packages/redact/|@visulima/redact} library to identify and mask sensitive data like
27
+ * passwords, API keys, credit card numbers, and other PII.
28
+ * @template L - The log level type
29
+ * @example
30
+ * ```typescript
31
+ * import { createPail } from "@visulima/pail";
32
+ * import RedactProcessor from "@visulima/pail/processor/redact";
33
+ *
34
+ * const logger = createPail({
35
+ * processors: [new RedactProcessor()]
36
+ * });
37
+ *
38
+ * logger.info("User login", {
39
+ * username: "john",
40
+ * password: "secret123", // Will be redacted
41
+ * apiKey: "sk-123456" // Will be redacted
42
+ * });
43
+ * ```
44
+ */
26
45
  declare class RedactProcessor<L extends string = string> implements Processor<L> {
27
- #private;
28
- /**
29
- * Creates a new RedactProcessor instance.
30
- * @param rules Custom redaction rules (uses standardRules if not provided)
31
- * @param options Additional redaction options
32
- */
33
- constructor(rules?: Rules, options?: RedactOptions);
34
- /**
35
- * Processes log metadata to redact sensitive information.
36
- *
37
- * Applies redaction rules to the message, context, and error properties
38
- * in the log metadata to prevent sensitive data from being logged.
39
- * @param meta The log metadata to process
40
- * @returns The processed metadata with sensitive data redacted
41
- */
42
- process(meta: Meta<L>): Meta<L>;
46
+ #private;
47
+ /**
48
+ * Creates a new RedactProcessor instance.
49
+ * @param rules Custom redaction rules (uses standardRules if not provided)
50
+ * @param options Additional redaction options
51
+ */
52
+ constructor(rules?: Rules, options?: RedactOptions);
53
+ /**
54
+ * Processes log metadata to redact sensitive information.
55
+ *
56
+ * Applies redaction rules to the message, context, and error properties
57
+ * in the log metadata to prevent sensitive data from being logged.
58
+ * @param meta The log metadata to process
59
+ * @returns The processed metadata with sensitive data redacted
60
+ */
61
+ process(meta: Meta<L>): Meta<L>;
43
62
  }
44
- export default RedactProcessor;
63
+ export { RedactProcessor as default };
@@ -1,31 +1 @@
1
- import { redact, standardRules } from '@visulima/redact';
2
-
3
- class RedactProcessor {
4
- /** The redact function configured with custom rules and options */
5
- #redact;
6
- /**
7
- * Creates a new RedactProcessor instance.
8
- * @param rules Custom redaction rules (uses standardRules if not provided)
9
- * @param options Additional redaction options
10
- */
11
- constructor(rules, options) {
12
- const effectiveRules = rules ?? standardRules;
13
- this.#redact = (input) => redact(input, effectiveRules, options);
14
- }
15
- /**
16
- * Processes log metadata to redact sensitive information.
17
- *
18
- * Applies redaction rules to the message, context, and error properties
19
- * in the log metadata to prevent sensitive data from being logged.
20
- * @param meta The log metadata to process
21
- * @returns The processed metadata with sensitive data redacted
22
- */
23
- process(meta) {
24
- meta.message = this.#redact(meta.message);
25
- meta.context = this.#redact(meta.context);
26
- meta.error = this.#redact(meta.error);
27
- return meta;
28
- }
29
- }
30
-
31
- export { RedactProcessor as default };
1
+ var c=Object.defineProperty;var s=(r,t)=>c(r,"name",{value:t,configurable:!0});import{redact as i,standardRules as n}from"@visulima/redact";var u=Object.defineProperty,d=s((r,t)=>u(r,"name",{value:t,configurable:!0}),"t");class m{static{s(this,"l")}static{d(this,"RedactProcessor")}#t;constructor(t,e){const o=t??n;this.#t=a=>i(a,o,e)}process(t){return t.message=this.#t(t.message),t.context=this.#t(t.context),t.error=this.#t(t.error),t}}export{m as default};
@@ -1,111 +1,112 @@
1
- import type { Meta, Processor } from "../types.d.ts";
1
+ import { M as Meta, P as Processor } from "../packem_shared/types.d-ByY2ZuYG.js";
2
+ import '@visulima/colorize';
3
+ import '@visulima/interactive-manager';
2
4
  /**
3
- * Head sampling configuration.
4
- *
5
- * Controls random sampling rates per log level. Each key is a log level name
6
- * and the value is the percentage (0-100) of logs at that level to keep.
7
- * Levels not listed default to 100 (keep all).
8
- * @example
9
- * ```typescript
10
- * const headSampling: HeadSamplingConfig = {
11
- * debug: 0, // Drop all debug logs
12
- * informational: 10, // Keep 10% of info logs
13
- * warning: 50, // Keep 50% of warning logs
14
- * error: 100, // Keep all error logs
15
- * };
16
- * ```
17
- */
5
+ * Head sampling configuration.
6
+ *
7
+ * Controls random sampling rates per log level. Each key is a log level name
8
+ * and the value is the percentage (0-100) of logs at that level to keep.
9
+ * Levels not listed default to 100 (keep all).
10
+ * @example
11
+ * ```typescript
12
+ * const headSampling: HeadSamplingConfig = {
13
+ * debug: 0, // Drop all debug logs
14
+ * informational: 10, // Keep 10% of info logs
15
+ * warning: 50, // Keep 50% of warning logs
16
+ * error: 100, // Keep all error logs
17
+ * };
18
+ * ```
19
+ */
18
20
  type HeadSamplingConfig = Partial<Record<string, number>>;
19
21
  /**
20
- * Tail sampling condition function.
21
- *
22
- * A function that receives the log metadata and returns true if the log
23
- * should be force-kept regardless of head sampling. This allows keeping
24
- * important logs based on their content (e.g., errors, slow operations).
25
- * @template L - The log level type
26
- */
22
+ * Tail sampling condition function.
23
+ *
24
+ * A function that receives the log metadata and returns true if the log
25
+ * should be force-kept regardless of head sampling. This allows keeping
26
+ * important logs based on their content (e.g., errors, slow operations).
27
+ * @template L - The log level type
28
+ */
27
29
  type TailSamplingCondition<L extends string = string> = (meta: Readonly<Meta<L>>) => boolean;
28
30
  /**
29
- * Sampling processor configuration options.
30
- */
31
+ * Sampling processor configuration options.
32
+ */
31
33
  interface SamplingProcessorOptions<L extends string = string> {
32
- /**
33
- * Head sampling rates per log level.
34
- *
35
- * A map of log level to sampling percentage (0-100).
36
- * Levels not specified default to 100 (keep all).
37
- * Set to 0 to drop all logs at that level.
38
- */
39
- head?: HeadSamplingConfig;
40
- /**
41
- * Tail sampling conditions.
42
- *
43
- * An array of condition functions that can force-keep a log entry
44
- * even if it was dropped by head sampling. If any condition returns true,
45
- * the log is kept.
46
- */
47
- tail?: TailSamplingCondition<L>[];
34
+ /**
35
+ * Head sampling rates per log level.
36
+ *
37
+ * A map of log level to sampling percentage (0-100).
38
+ * Levels not specified default to 100 (keep all).
39
+ * Set to 0 to drop all logs at that level.
40
+ */
41
+ head?: HeadSamplingConfig;
42
+ /**
43
+ * Tail sampling conditions.
44
+ *
45
+ * An array of condition functions that can force-keep a log entry
46
+ * even if it was dropped by head sampling. If any condition returns true,
47
+ * the log is kept.
48
+ */
49
+ tail?: TailSamplingCondition<L>[];
48
50
  }
49
51
  /**
50
- * Sampling Processor.
51
- *
52
- * Inspired by evlog's production sampling strategy, this processor implements
53
- * both head sampling (random per-level) and tail sampling (force-keep based
54
- * on conditions) to control log volume in production environments.
55
- *
56
- * **Head sampling** randomly drops a percentage of logs per level. This is
57
- * evaluated first and provides broad volume control.
58
- *
59
- * **Tail sampling** can override head sampling to force-keep important logs
60
- * based on their content. For example, you might drop 90% of info logs but
61
- * force-keep any that contain error information or relate to slow operations.
62
- *
63
- * When a log is dropped by sampling, the processor sets a `__dropped: true`
64
- * boolean flag on the meta object. Reporters should check for this flag and
65
- * skip entries where `__dropped` is `true`.
66
- * @template L - The log level type
67
- * @example
68
- * ```typescript
69
- * import { createPail } from "@visulima/pail";
70
- * import SamplingProcessor from "@visulima/pail/processor/sampling";
71
- *
72
- * const logger = createPail({
73
- * processors: [
74
- * new SamplingProcessor({
75
- * head: {
76
- * debug: 0, // Drop all debug logs
77
- * informational: 10, // Keep 10% of info logs
78
- * warning: 50, // Keep 50% of warnings
79
- * error: 100, // Keep all errors
80
- * },
81
- * tail: [
82
- * // Force-keep logs with errors regardless of head sampling
83
- * (meta) => meta.error !== undefined,
84
- * // Force-keep logs from critical scopes
85
- * (meta) => meta.scope?.includes("payment") ?? false,
86
- * ],
87
- * }),
88
- * ],
89
- * });
90
- * ```
91
- */
52
+ * Sampling Processor.
53
+ *
54
+ * Inspired by evlog's production sampling strategy, this processor implements
55
+ * both head sampling (random per-level) and tail sampling (force-keep based
56
+ * on conditions) to control log volume in production environments.
57
+ *
58
+ * **Head sampling** randomly drops a percentage of logs per level. This is
59
+ * evaluated first and provides broad volume control.
60
+ *
61
+ * **Tail sampling** can override head sampling to force-keep important logs
62
+ * based on their content. For example, you might drop 90% of info logs but
63
+ * force-keep any that contain error information or relate to slow operations.
64
+ *
65
+ * When a log is dropped by sampling, the processor sets a `__dropped: true`
66
+ * boolean flag on the meta object. Reporters should check for this flag and
67
+ * skip entries where `__dropped` is `true`.
68
+ * @template L - The log level type
69
+ * @example
70
+ * ```typescript
71
+ * import { createPail } from "@visulima/pail";
72
+ * import SamplingProcessor from "@visulima/pail/processor/sampling";
73
+ *
74
+ * const logger = createPail({
75
+ * processors: [
76
+ * new SamplingProcessor({
77
+ * head: {
78
+ * debug: 0, // Drop all debug logs
79
+ * informational: 10, // Keep 10% of info logs
80
+ * warning: 50, // Keep 50% of warnings
81
+ * error: 100, // Keep all errors
82
+ * },
83
+ * tail: [
84
+ * // Force-keep logs with errors regardless of head sampling
85
+ * (meta) => meta.error !== undefined,
86
+ * // Force-keep logs from critical scopes
87
+ * (meta) => meta.scope?.includes("payment") ?? false,
88
+ * ],
89
+ * }),
90
+ * ],
91
+ * });
92
+ * ```
93
+ */
92
94
  declare class SamplingProcessor<L extends string = string> implements Processor<L> {
93
- #private;
94
- /**
95
- * Creates a new SamplingProcessor instance.
96
- * @param options Sampling configuration options
97
- */
98
- constructor(options?: SamplingProcessorOptions<L>);
99
- /**
100
- * Processes log metadata to apply sampling rules.
101
- *
102
- * First evaluates head sampling (random per-level), then checks tail
103
- * sampling conditions. If a log is dropped, the meta is marked with
104
- * `__dropped: true` so reporters can skip it.
105
- * @param meta The log metadata to process
106
- * @returns The processed metadata, potentially marked as dropped
107
- */
108
- process(meta: Meta<L>): Meta<L>;
95
+ #private;
96
+ /**
97
+ * Creates a new SamplingProcessor instance.
98
+ * @param options Sampling configuration options
99
+ */
100
+ constructor(options?: SamplingProcessorOptions<L>);
101
+ /**
102
+ * Processes log metadata to apply sampling rules.
103
+ *
104
+ * First evaluates head sampling (random per-level), then checks tail
105
+ * sampling conditions. If a log is dropped, the meta is marked with
106
+ * `__dropped: true` so reporters can skip it.
107
+ * @param meta The log metadata to process
108
+ * @returns The processed metadata, potentially marked as dropped
109
+ */
110
+ process(meta: Meta<L>): Meta<L>;
109
111
  }
110
- export default SamplingProcessor;
111
- export type { HeadSamplingConfig, SamplingProcessorOptions, TailSamplingCondition };
112
+ export { type HeadSamplingConfig, type SamplingProcessorOptions, type TailSamplingCondition, SamplingProcessor as default };
@@ -1,59 +1 @@
1
- class SamplingProcessor {
2
- #headRates;
3
- #tailConditions;
4
- /**
5
- * Creates a new SamplingProcessor instance.
6
- * @param options Sampling configuration options
7
- */
8
- constructor(options = {}) {
9
- this.#headRates = options.head ?? {};
10
- this.#tailConditions = options.tail ?? [];
11
- }
12
- /**
13
- * Processes log metadata to apply sampling rules.
14
- *
15
- * First evaluates head sampling (random per-level), then checks tail
16
- * sampling conditions. If a log is dropped, the meta is marked with
17
- * `__dropped: true` so reporters can skip it.
18
- * @param meta The log metadata to process
19
- * @returns The processed metadata, potentially marked as dropped
20
- */
21
- process(meta) {
22
- const level = meta.type.level;
23
- if (this.#shouldDrop(level) && !this.#shouldForceKeep(meta)) {
24
- return { ...meta, dropped: true };
25
- }
26
- return meta;
27
- }
28
- /**
29
- * Evaluates head sampling for the given log level.
30
- * @returns true if the log should be dropped
31
- */
32
- #shouldDrop(level) {
33
- const rate = this.#headRates[level];
34
- if (rate === void 0) {
35
- return false;
36
- }
37
- if (rate <= 0) {
38
- return true;
39
- }
40
- if (rate >= 100) {
41
- return false;
42
- }
43
- return Math.random() * 100 >= rate;
44
- }
45
- /**
46
- * Evaluates tail sampling conditions.
47
- * @returns true if any condition forces keeping the log
48
- */
49
- #shouldForceKeep(meta) {
50
- for (let i = 0; i < this.#tailConditions.length; i += 1) {
51
- if (this.#tailConditions[i](meta)) {
52
- return true;
53
- }
54
- }
55
- return false;
56
- }
57
- }
58
-
59
- export { SamplingProcessor as default };
1
+ var a=Object.defineProperty;var s=(e,t)=>a(e,"name",{value:t,configurable:!0});var i=Object.defineProperty,n=s((e,t)=>i(e,"name",{value:t,configurable:!0}),"i");class l{static{s(this,"o")}static{n(this,"SamplingProcessor")}#r;#t;constructor(t={}){this.#r=t.head??{},this.#t=t.tail??[]}process(t){const r=t.type.level;return this.#e(r)&&!this.#s(t)?{...t,dropped:!0}:t}#e(t){const r=this.#r[t];return r===void 0?!1:r<=0?!0:r>=100?!1:Math.random()*100>=r}#s(t){for(let r=0;r<this.#t.length;r+=1)if(this.#t[r](t))return!0;return!1}}export{l as default};