@yieldkit/logger 0.0.0-stage → 0.1.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.
Files changed (55) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +256 -2
  3. package/dist/callback-logger.d.ts +78 -0
  4. package/dist/callback-logger.js +104 -0
  5. package/dist/config.d.ts +29 -0
  6. package/dist/config.js +264 -0
  7. package/dist/context.d.ts +60 -0
  8. package/dist/context.js +100 -0
  9. package/dist/crash.d.ts +114 -0
  10. package/dist/crash.js +215 -0
  11. package/dist/credentials.d.ts +57 -0
  12. package/dist/credentials.js +267 -0
  13. package/dist/error.d.ts +27 -0
  14. package/dist/error.js +173 -0
  15. package/dist/formatters.d.ts +53 -0
  16. package/dist/formatters.js +137 -0
  17. package/dist/index.d.ts +16 -0
  18. package/dist/index.js +15 -0
  19. package/dist/layer.d.ts +31 -0
  20. package/dist/layer.js +78 -0
  21. package/dist/line-cap.d.ts +6 -0
  22. package/dist/line-cap.js +367 -0
  23. package/dist/log-fan-out.d.ts +24 -0
  24. package/dist/log-fan-out.js +83 -0
  25. package/dist/metrics.d.ts +19 -0
  26. package/dist/metrics.js +45 -0
  27. package/dist/omit-undefined.js +10 -0
  28. package/dist/otlp/flush.d.ts +8 -0
  29. package/dist/otlp/flush.js +11 -0
  30. package/dist/otlp/index.d.ts +4 -0
  31. package/dist/otlp/index.js +4 -0
  32. package/dist/otlp/telemetry.d.ts +86 -0
  33. package/dist/otlp/telemetry.js +230 -0
  34. package/dist/otlp/tracing.d.ts +11 -0
  35. package/dist/otlp/tracing.js +13 -0
  36. package/dist/redact-cause.js +92 -0
  37. package/dist/redact-walk.d.ts +19 -0
  38. package/dist/redact-walk.js +290 -0
  39. package/dist/redact.d.ts +104 -0
  40. package/dist/redact.js +271 -0
  41. package/dist/request-id.d.ts +21 -0
  42. package/dist/request-id.js +28 -0
  43. package/dist/testing/index.d.ts +55 -0
  44. package/dist/testing/index.js +42 -0
  45. package/dist/types.d.ts +212 -0
  46. package/docs/callback-logger.md +38 -0
  47. package/docs/configuration.md +56 -0
  48. package/docs/crash-and-startup.md +57 -0
  49. package/docs/durations-and-request-ids.md +30 -0
  50. package/docs/formats.md +78 -0
  51. package/docs/logging.md +53 -0
  52. package/docs/redaction.md +87 -0
  53. package/docs/telemetry.md +78 -0
  54. package/docs/testing.md +60 -0
  55. package/package.json +74 -3
@@ -0,0 +1,78 @@
1
+ # Telemetry
2
+
3
+ The OTLP exporters live in their own entry point, so programs that only log don't load them or an HTTP client:
4
+
5
+ ```ts
6
+ import { flushOtlp, telemetryLayer, tracingLayer } from '@yieldkit/logger/otlp';
7
+ ```
8
+
9
+ `telemetryLayer(options: TelemetryOptions)` exports traces, metrics and logs over OTLP/HTTP and provides `OtlpExporter.Flusher`. It needs an `HttpClient`. `tracingLayer(serviceName, options?)` is the same layer with `serviceName` as the only required argument.
10
+
11
+ <!-- prettier-ignore -->
12
+ ```ts
13
+ const TelemetryLive = telemetryLayer({
14
+ serviceName: 'api', // service.name; OTEL_SERVICE_NAME is ignored
15
+ serviceVersion: '1.4.0', // optional; wins over OTEL_SERVICE_VERSION
16
+ environment: 'production', // optional; defaults to NODE_ENV
17
+ signals: { traces: true, metrics: true, logs: true }, // TelemetrySignals; each defaults to true
18
+ serialization: 'protobuf', // default; 'json' is readable in tests
19
+ headers: { 'x-api-key': apiKey }, // optional; replaces the OTEL_*_HEADERS variables
20
+ redaction: { sensitiveKeys: ['customerRef'] }, // optional; for span data
21
+ }).pipe(Layer.provide(BunHttpClient.layer));
22
+ ```
23
+
24
+ Build it on top of the logger layer (`Layer.provideMerge(LoggerLive(...))`), so warnings from the exporters go through the redacted logger, and provide it to the whole program.
25
+
26
+ ## Environment variables
27
+
28
+ A signal is exported only when it has an endpoint. Without any, the layer provides a `Flusher` that does nothing.
29
+
30
+ | Variable | Effect |
31
+ | -------------------------------------------------------- | -------------------------------------------------------------------- |
32
+ | `OTEL_EXPORTER_OTLP_ENDPOINT` | Base URL; `/v1/traces`, `/v1/metrics` and `/v1/logs` are appended |
33
+ | `OTEL_EXPORTER_OTLP_{TRACES,METRICS,LOGS}_ENDPOINT` | Per-signal URL, used as it is; wins over the base |
34
+ | `OTEL_EXPORTER_OTLP[_{TRACES,METRICS,LOGS}]_HEADERS` | `key=value, key2=value2` request headers, trimmed, values decoded |
35
+ | `OTEL_EXPORTER_OTLP[_{TRACES,METRICS,LOGS}]_COMPRESSION` | `gzip` or `none` |
36
+ | `OTEL_EXPORTER_OTLP[_{TRACES,METRICS,LOGS}]_TIMEOUT` | Milliseconds to wait for the last export on shutdown |
37
+ | `OTEL_{TRACES,METRICS,LOGS}_EXPORTER` | Comma list; the signal is on when it is blank or includes `otlp` |
38
+ | `OTEL_SDK_DISABLED` | `true` (any case) turns every signal off; other values leave them on |
39
+ | `OTEL_SERVICE_VERSION`, `OTEL_RESOURCE_ATTRIBUTES` | Resource attributes |
40
+ | `OTEL_EXPORTER_OTLP[_{TRACES,METRICS,LOGS}]_PROTOCOL` | Ignored; export is always OTLP over HTTP |
41
+
42
+ A per-signal variable wins over the base one, and a blank one counts as unset. `signals.<signal>: false` turns a signal off whatever the environment says. An endpoint that is not an `http:` or `https:` URL, a compression other than `gzip` or `none`, or a timeout that is not a whole number fails the layer with a `ConfigError`.
43
+
44
+ Resource attributes: `serviceName` is always `service.name`. `serviceVersion` wins over `OTEL_SERVICE_VERSION`, which wins over `service.version` in `OTEL_RESOURCE_ATTRIBUTES`. A `deployment.environment.name` in `OTEL_RESOURCE_ATTRIBUTES` wins over `environment` and `NODE_ENV`.
45
+
46
+ ## Traces
47
+
48
+ Spans are exported through `withTracerRedaction`: span and event names, attribute values, link attributes and the failure cause of a failed span are redacted before the exporter sees them. With `includeTracerLogger` (the default), each log line is also an event on the current span.
49
+
50
+ ## Logs
51
+
52
+ Log records reach the exporter through the fan-out logger that `LoggerLive` and `makeLoggerLayer` install, after the logger's redaction and minimum level. So log export needs one of those layers, and only records logged by code the telemetry layer is provided to are exported: two runtimes in one process never share log exporters.
53
+
54
+ The exporters are added with `logExportSinkLayer`, which registers each sink under a context key of its own; `logExportSinkLayer(make)` adds your own sink logger next to them. Sink layers compose any way: merged as siblings (`Layer.mergeAll(TelemetryLive, CollectLive)`), chained with `Layer.provide`, or provided to one part of the program, where the sink only receives that part's records. Two runtimes never reach each other's sinks, even when they are built with one shared memo map. A list provided directly as `LogExportSinks` (a context reference) is used too:
55
+
56
+ ```ts
57
+ import { logExportSinkLayer } from '@yieldkit/logger';
58
+ import { Effect, Logger } from 'effect';
59
+
60
+ const records: Array<unknown> = [];
61
+ const CollectLive = logExportSinkLayer(
62
+ Effect.succeed(Logger.make((options) => records.push(options.message))),
63
+ );
64
+ ```
65
+
66
+ A sink stops receiving records when its layer's scope closes, before the finalizers its `make` registered run. A sink that throws is skipped for that record; the other sinks and the logging effect carry on.
67
+
68
+ ## Flushing
69
+
70
+ Closing the layer's scope flushes the last batches. To flush earlier, for example before a forced exit, run `flushOtlp`. It flushes the `OtlpExporter.Flusher` the layer provides and does nothing when there is none:
71
+
72
+ ```ts
73
+ const flushed = flushOtlp.pipe(Effect.timeoutOption('2 seconds'));
74
+ ```
75
+
76
+ Pass it to the crash handlers so a crash is exported before the process exits: `makeCrashHandlersLayer({ flush: flushOtlp })` (see [Crashes and startup failures](crash-and-startup.md)).
77
+
78
+ `flush` cannot wait for an export that was already running when it was called. Effect's exporter starts one on its own when a batch fills up (1,000 records), so a record that fills a batch right before a forced exit can be lost with that request.
@@ -0,0 +1,60 @@
1
+ # Testing
2
+
3
+ `@yieldkit/logger/testing` collects structured records in memory instead of writing them to the console. Records are redacted exactly like console output, so assert on `<redacted>`, not on the secret.
4
+
5
+ ## One effect
6
+
7
+ `captureLogs(effect, options?)` runs `effect` with a fresh logger and returns its value and a copy of the records:
8
+
9
+ ```ts
10
+ import { captureLogs } from '@yieldkit/logger/testing';
11
+ import { Effect } from 'effect';
12
+ import { expect, it } from 'vite-plus/test';
13
+
14
+ it('logs the item id on every line', async () => {
15
+ const { logs } = await Effect.runPromise(captureLogs(syncItem('a1')));
16
+
17
+ expect(logs[0]?.annotations).toEqual({ itemId: 'a1', attempt: 1 });
18
+ expect(logs[1]?.error).toMatchObject({ kind: 'fail', tag: 'FetchError', status: 503 });
19
+ });
20
+ ```
21
+
22
+ ## A logger that outlives one effect
23
+
24
+ `makeTestLogger(options?)` returns a `TestLoggerHarness` (the logger, a layer and its records) for a layer built once per suite:
25
+
26
+ ```ts
27
+ import { makeTestLogger } from '@yieldkit/logger/testing';
28
+ import { Effect } from 'effect';
29
+ import { beforeEach, expect, it } from 'vite-plus/test';
30
+
31
+ const logs = makeTestLogger({ minLevel: 'Debug', service: 'test' });
32
+
33
+ beforeEach(() => logs.clear());
34
+
35
+ it('logs the retry', async () => {
36
+ await Effect.runPromise(program.pipe(Effect.provide(logs.layer)));
37
+ expect(logs.records.map((record) => record.message)).toContain('item.sync.retry');
38
+ });
39
+ ```
40
+
41
+ | Member | Content |
42
+ | --------- | ---------------------------------------------------------------------- |
43
+ | `records` | Records captured since the last `clear`, in order; updated in place |
44
+ | `layer` | Installs the logger and, when set, the minimum level |
45
+ | `context` | The same as a `Context`, for `Effect.provide` without building a layer |
46
+ | `logger` | The `Logger` that appends to `records` |
47
+ | `clear` | Empties `records`; the layer stays installed |
48
+
49
+ ## Options
50
+
51
+ Both functions take `TestLoggerOptions`:
52
+
53
+ | Option | Default | Purpose |
54
+ | --------------------------- | --------------- | ----------------------------------------------------- |
55
+ | `minLevel` | Effect's (Info) | Lowest level captured |
56
+ | `includeTracerLogger` | `true` | Also install the redacted tracer logger (span events) |
57
+ | `service`, `env`, `version` | none | Written on each record |
58
+ | `redaction` | none | Extra key names, as in [Redaction](redaction.md) |
59
+
60
+ The test logger replaces the current loggers, so nothing reaches the console or the OTLP log exporter while it is installed.
package/package.json CHANGED
@@ -1,6 +1,77 @@
1
1
  {
2
2
  "name": "@yieldkit/logger",
3
- "version": "0.0.0-stage",
4
- "stub": true,
5
- "description": "Temporary package placeholder for staged publishing"
3
+ "version": "0.1.0",
4
+ "description": "Structured Effect logging with secret redaction, error summaries and OTLP export of traces, metrics and logs.",
5
+ "keywords": [
6
+ "effect",
7
+ "effect-ts",
8
+ "logger",
9
+ "logging",
10
+ "opentelemetry",
11
+ "otlp",
12
+ "redaction"
13
+ ],
14
+ "homepage": "https://github.com/yieldkit/logger#readme",
15
+ "bugs": "https://github.com/yieldkit/logger/issues",
16
+ "license": "MIT",
17
+ "repository": {
18
+ "type": "git",
19
+ "url": "git+https://github.com/yieldkit/logger.git"
20
+ },
21
+ "files": [
22
+ "dist",
23
+ "docs",
24
+ "LICENSE",
25
+ "README.md"
26
+ ],
27
+ "type": "module",
28
+ "sideEffects": false,
29
+ "exports": {
30
+ ".": {
31
+ "types": "./dist/index.d.ts",
32
+ "default": "./dist/index.js"
33
+ },
34
+ "./otlp": {
35
+ "types": "./dist/otlp/index.d.ts",
36
+ "default": "./dist/otlp/index.js"
37
+ },
38
+ "./testing": {
39
+ "types": "./dist/testing/index.d.ts",
40
+ "default": "./dist/testing/index.js"
41
+ },
42
+ "./package.json": "./package.json"
43
+ },
44
+ "publishConfig": {
45
+ "access": "public"
46
+ },
47
+ "devDependencies": {
48
+ "@effect/tsgo": "0.73.4",
49
+ "@types/node": "26.6.6",
50
+ "@yieldkit/oxlint-config": "0.4.1",
51
+ "effect": "4.1.5",
52
+ "typescript": "^7.0.2",
53
+ "vite-plus": "1.8.0"
54
+ },
55
+ "peerDependencies": {
56
+ "effect": "~4.1.5"
57
+ },
58
+ "devEngines": {
59
+ "packageManager": {
60
+ "name": "pnpm",
61
+ "version": "12.23.0",
62
+ "onFail": "download"
63
+ }
64
+ },
65
+ "engines": {
66
+ "node": "^22.18.0 || ^24.11.0 || >=26.0.0"
67
+ },
68
+ "scripts": {
69
+ "build": "vp pack",
70
+ "check": "effect-tsgo vp check",
71
+ "lint:fix": "effect-tsgo vp check --fix",
72
+ "ready": "pnpm check && vp test",
73
+ "test": "vp test",
74
+ "test:package": "node scripts/package-smoke.mjs",
75
+ "typecheck": "tsc --noEmit"
76
+ }
6
77
  }