@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.
- package/LICENSE +21 -0
- package/README.md +256 -2
- package/dist/callback-logger.d.ts +78 -0
- package/dist/callback-logger.js +104 -0
- package/dist/config.d.ts +29 -0
- package/dist/config.js +264 -0
- package/dist/context.d.ts +60 -0
- package/dist/context.js +100 -0
- package/dist/crash.d.ts +114 -0
- package/dist/crash.js +215 -0
- package/dist/credentials.d.ts +57 -0
- package/dist/credentials.js +267 -0
- package/dist/error.d.ts +27 -0
- package/dist/error.js +173 -0
- package/dist/formatters.d.ts +53 -0
- package/dist/formatters.js +137 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.js +15 -0
- package/dist/layer.d.ts +31 -0
- package/dist/layer.js +78 -0
- package/dist/line-cap.d.ts +6 -0
- package/dist/line-cap.js +367 -0
- package/dist/log-fan-out.d.ts +24 -0
- package/dist/log-fan-out.js +83 -0
- package/dist/metrics.d.ts +19 -0
- package/dist/metrics.js +45 -0
- package/dist/omit-undefined.js +10 -0
- package/dist/otlp/flush.d.ts +8 -0
- package/dist/otlp/flush.js +11 -0
- package/dist/otlp/index.d.ts +4 -0
- package/dist/otlp/index.js +4 -0
- package/dist/otlp/telemetry.d.ts +86 -0
- package/dist/otlp/telemetry.js +230 -0
- package/dist/otlp/tracing.d.ts +11 -0
- package/dist/otlp/tracing.js +13 -0
- package/dist/redact-cause.js +92 -0
- package/dist/redact-walk.d.ts +19 -0
- package/dist/redact-walk.js +290 -0
- package/dist/redact.d.ts +104 -0
- package/dist/redact.js +271 -0
- package/dist/request-id.d.ts +21 -0
- package/dist/request-id.js +28 -0
- package/dist/testing/index.d.ts +55 -0
- package/dist/testing/index.js +42 -0
- package/dist/types.d.ts +212 -0
- package/docs/callback-logger.md +38 -0
- package/docs/configuration.md +56 -0
- package/docs/crash-and-startup.md +57 -0
- package/docs/durations-and-request-ids.md +30 -0
- package/docs/formats.md +78 -0
- package/docs/logging.md +53 -0
- package/docs/redaction.md +87 -0
- package/docs/telemetry.md +78 -0
- package/docs/testing.md +60 -0
- package/package.json +74 -3
package/dist/error.js
ADDED
|
@@ -0,0 +1,173 @@
|
|
|
1
|
+
import { omitUndefined } from "./omit-undefined.js";
|
|
2
|
+
import { FUNCTION_LOG_VALUE, UNSERIALIZABLE_LOG_VALUE, withVisible } from "./redact-walk.js";
|
|
3
|
+
import { defaultLogRedaction } from "./redact.js";
|
|
4
|
+
import { Cause, Formatter, Predicate } from "effect";
|
|
5
|
+
// src/error.ts
|
|
6
|
+
/**
|
|
7
|
+
* Reads a non-empty string property of an object. Missing, non-string, and empty values, and a
|
|
8
|
+
* property that throws when read, are omitted.
|
|
9
|
+
*/
|
|
10
|
+
const readNonEmptyStringProp = (value, key) => {
|
|
11
|
+
try {
|
|
12
|
+
if (!Predicate.hasProperty(value, key)) return;
|
|
13
|
+
const candidate = value[key];
|
|
14
|
+
return typeof candidate === "string" && candidate.length > 0 ? candidate : void 0;
|
|
15
|
+
} catch {
|
|
16
|
+
return;
|
|
17
|
+
}
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* Reads a finite number property of an object. Missing and non-finite values, and a property that
|
|
21
|
+
* throws when read, are omitted.
|
|
22
|
+
*/
|
|
23
|
+
const readFiniteNumberProp = (value, key) => {
|
|
24
|
+
try {
|
|
25
|
+
if (!Predicate.hasProperty(value, key)) return;
|
|
26
|
+
const candidate = value[key];
|
|
27
|
+
return typeof candidate === "number" && Number.isFinite(candidate) ? candidate : void 0;
|
|
28
|
+
} catch {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Tells whether `value` is an `Error`. A revoked `Proxy`, which throws on the check, is not.
|
|
34
|
+
*/
|
|
35
|
+
const isError = (value) => {
|
|
36
|
+
try {
|
|
37
|
+
return value instanceof Error;
|
|
38
|
+
} catch {
|
|
39
|
+
return false;
|
|
40
|
+
}
|
|
41
|
+
};
|
|
42
|
+
/**
|
|
43
|
+
* Reads the class name of an object, or `undefined` for a plain object, a null-prototype object or
|
|
44
|
+
* one whose prototype cannot be read.
|
|
45
|
+
*/
|
|
46
|
+
const className = (value) => {
|
|
47
|
+
try {
|
|
48
|
+
const prototype = typeof value === "object" && value !== null ? Reflect.getPrototypeOf(value) : void 0;
|
|
49
|
+
if (!Predicate.hasProperty(prototype, "constructor")) return;
|
|
50
|
+
const name = Predicate.hasProperty(prototype.constructor, "name") ? prototype.constructor.name : void 0;
|
|
51
|
+
return typeof name === "string" && name !== "" && name !== "Object" ? name : void 0;
|
|
52
|
+
} catch {
|
|
53
|
+
return;
|
|
54
|
+
}
|
|
55
|
+
};
|
|
56
|
+
/**
|
|
57
|
+
* Renders an object that has no message or tag: its redacted JSON form, after the class name when
|
|
58
|
+
* it has one (`Credentials {"user":"u","password":"<redacted>"}`). The object's own `toString` is
|
|
59
|
+
* never called, because it may print the fields it holds.
|
|
60
|
+
*/
|
|
61
|
+
const describeObject = (error, redaction) => {
|
|
62
|
+
const redacted = redaction.redactValue(error);
|
|
63
|
+
const json = typeof redacted === "string" ? redacted : Formatter.formatJson(redacted);
|
|
64
|
+
const name = className(error);
|
|
65
|
+
return name === void 0 ? json : `${name} ${json}`;
|
|
66
|
+
};
|
|
67
|
+
/**
|
|
68
|
+
* {@link describeError} with the given rules for the JSON form of a plain object.
|
|
69
|
+
*/
|
|
70
|
+
const describeErrorWith = (error, redaction) => withVisible(error, (visible) => describeVisibleError(visible, redaction));
|
|
71
|
+
/**
|
|
72
|
+
* Returns the message a value carries itself: an `Error`'s message (or name, or `Error`), another
|
|
73
|
+
* object's `message` or `_tag` string, or `String(value)` for a primitive. Returns `undefined` for
|
|
74
|
+
* an object that has neither, whose only description is its JSON form.
|
|
75
|
+
*/
|
|
76
|
+
const ownDescription = (error) => {
|
|
77
|
+
if (isError(error)) return readNonEmptyStringProp(error, "message") ?? readNonEmptyStringProp(error, "name") ?? "Error";
|
|
78
|
+
if (typeof error === "object" && error !== null) return readNonEmptyStringProp(error, "message") ?? readNonEmptyStringProp(error, "_tag");
|
|
79
|
+
if (typeof error === "function") return FUNCTION_LOG_VALUE;
|
|
80
|
+
try {
|
|
81
|
+
return String(error);
|
|
82
|
+
} catch {
|
|
83
|
+
return UNSERIALIZABLE_LOG_VALUE;
|
|
84
|
+
}
|
|
85
|
+
};
|
|
86
|
+
/**
|
|
87
|
+
* Describes a value that is already its own redacted form. See {@link describeError}.
|
|
88
|
+
*/
|
|
89
|
+
const describeVisibleError = (error, redaction) => ownDescription(error) ?? describeObject(error, redaction);
|
|
90
|
+
/**
|
|
91
|
+
* Returns a message for an unknown error, and never throws. A `Redactable` value is described by
|
|
92
|
+
* its redacted form. An `Error` gives its message, or its name when the message is empty. Another
|
|
93
|
+
* object gives its `message` or `_tag` string, or else its JSON form with sensitive keys masked
|
|
94
|
+
* (see `redactLogValue`), after its class name, such as `Credentials
|
|
95
|
+
* {"user":"u","password":"<redacted>"}`. Its own `toString` is never called. Other values give
|
|
96
|
+
* `String(value)`. The result is not passed through `redactLogString`; callers that log it should
|
|
97
|
+
* do that.
|
|
98
|
+
*/
|
|
99
|
+
const describeError = (error) => describeErrorWith(error, defaultLogRedaction);
|
|
100
|
+
/**
|
|
101
|
+
* Reads `Error.name` or a non-empty `name` string. Other values are omitted.
|
|
102
|
+
*/
|
|
103
|
+
const readErrorName = (error) => {
|
|
104
|
+
if (isError(error)) return readNonEmptyStringProp(error, "name");
|
|
105
|
+
return typeof error === "object" && error !== null ? readNonEmptyStringProp(error, "name") : void 0;
|
|
106
|
+
};
|
|
107
|
+
/**
|
|
108
|
+
* Builds a fail or die summary from `message`, `_tag`, `name`, and `status` when present, read from
|
|
109
|
+
* the redacted form of a `Redactable` value. The message, tag and name go through `redactText` (the
|
|
110
|
+
* convenience shapes and `scrubText`).
|
|
111
|
+
*/
|
|
112
|
+
const buildErrorSummary = (kind, thrown, redaction) => withVisible(thrown, (error) => buildVisibleSummary(kind, error, redaction));
|
|
113
|
+
/**
|
|
114
|
+
* Reads the tag, name and status of a value that is already its own redacted form, with the tag and
|
|
115
|
+
* name masked by `redaction.redactString`.
|
|
116
|
+
*/
|
|
117
|
+
const summaryDetails = (error, redaction) => {
|
|
118
|
+
const tag = readNonEmptyStringProp(error, "_tag");
|
|
119
|
+
const name = readErrorName(error);
|
|
120
|
+
return omitUndefined({
|
|
121
|
+
tag: tag === void 0 ? void 0 : redaction.redactString(tag),
|
|
122
|
+
name: name === void 0 ? void 0 : redaction.redactString(name),
|
|
123
|
+
status: readFiniteNumberProp(error, "status")
|
|
124
|
+
});
|
|
125
|
+
};
|
|
126
|
+
/**
|
|
127
|
+
* {@link buildErrorSummary} for a value that is already its own redacted form.
|
|
128
|
+
*/
|
|
129
|
+
const buildVisibleSummary = (kind, error, redaction) => ({
|
|
130
|
+
kind,
|
|
131
|
+
message: redaction.redactString(describeVisibleError(error, redaction)),
|
|
132
|
+
...summaryDetails(error, redaction)
|
|
133
|
+
});
|
|
134
|
+
/**
|
|
135
|
+
* Returns annotation fields for `error`, read from the redacted form of a `Redactable` value:
|
|
136
|
+
* `errorMessage`, `errorTag`, `errorName` and `errorStatus`, each when present. The message, tag
|
|
137
|
+
* and name are masked with `redactLogString`. An object with no `message` or `_tag` gets no
|
|
138
|
+
* `errorMessage`: its JSON form is redacted with the built-in names only, so it could reveal a
|
|
139
|
+
* value under a name the logger layer adds, and the line's cause already carries that object
|
|
140
|
+
* redacted with the layer's rules. Never throws.
|
|
141
|
+
*/
|
|
142
|
+
const extractErrorFields = (error) => withVisible(error, (visible) => {
|
|
143
|
+
const message = ownDescription(visible);
|
|
144
|
+
const { tag, name, status } = summaryDetails(visible, defaultLogRedaction);
|
|
145
|
+
return omitUndefined({
|
|
146
|
+
errorMessage: message === void 0 ? void 0 : defaultLogRedaction.redactString(message),
|
|
147
|
+
errorTag: tag,
|
|
148
|
+
errorName: name,
|
|
149
|
+
errorStatus: status
|
|
150
|
+
});
|
|
151
|
+
});
|
|
152
|
+
/**
|
|
153
|
+
* {@link extractCauseSummary} with the given rules.
|
|
154
|
+
*/
|
|
155
|
+
const summarizeCause = (cause, redaction) => {
|
|
156
|
+
if (cause.reasons.length === 0) return;
|
|
157
|
+
const failReason = cause.reasons.find(Cause.isFailReason);
|
|
158
|
+
if (failReason !== void 0) return buildErrorSummary("fail", failReason.error, redaction);
|
|
159
|
+
const dieReason = cause.reasons.find(Cause.isDieReason);
|
|
160
|
+
if (dieReason !== void 0) return buildErrorSummary("die", dieReason.defect, redaction);
|
|
161
|
+
const interruptReason = cause.reasons.find(Cause.isInterruptReason);
|
|
162
|
+
if (interruptReason !== void 0) return {
|
|
163
|
+
kind: "interrupt",
|
|
164
|
+
message: "Fiber interrupted",
|
|
165
|
+
...omitUndefined({ fiberId: interruptReason.fiberId })
|
|
166
|
+
};
|
|
167
|
+
};
|
|
168
|
+
/**
|
|
169
|
+
* Returns the first fail, otherwise the first die, otherwise the first interrupt. An empty cause
|
|
170
|
+
* returns `undefined`. The summary's message, tag and name are masked with `redactLogString`.
|
|
171
|
+
*/
|
|
172
|
+
const extractCauseSummary = (cause) => summarizeCause(cause, defaultLogRedaction);
|
|
173
|
+
export { describeError, extractCauseSummary, extractErrorFields, summarizeCause };
|
|
@@ -0,0 +1,53 @@
|
|
|
1
|
+
import "./redact.js";
|
|
2
|
+
import { LogFormat, LogStderrMode, StructuredLogRecord, StructuredLoggerOptions } from "./types.js";
|
|
3
|
+
import { DEFAULT_MAX_LINE_BYTES } from "./line-cap.js";
|
|
4
|
+
import { Logger } from "effect";
|
|
5
|
+
/**
|
|
6
|
+
* Builds a logger that emits a `StructuredLogRecord`. The message, annotations, log span labels and
|
|
7
|
+
* cause are redacted with the built-in sensitive names plus `options.redaction` (see
|
|
8
|
+
* `redactLogValue` and `sanitizeCause`), and `Redacted` values print as `<redacted>`. The `error`
|
|
9
|
+
* summary is read from the original cause, so its status survives, and its message, tag and name
|
|
10
|
+
* are masked. A parent span adds `traceId` and `spanId`.
|
|
11
|
+
*/
|
|
12
|
+
export declare const makeStructuredLogger: (options?: StructuredLoggerOptions) => Logger.Logger<unknown, StructuredLogRecord>;
|
|
13
|
+
/**
|
|
14
|
+
* Builds a logger that emits one JSON line per record, redacted like `makeStructuredLogger`. A line
|
|
15
|
+
* longer than `maxLineBytes` in UTF-8, newline included, is shortened: `truncated: true` is added,
|
|
16
|
+
* then `cause`, `error.message` and `message` are cut to the longest start that fits, and a record
|
|
17
|
+
* that still does not fit is replaced by a minimal record with the identity fields, trace ids, the
|
|
18
|
+
* error's kind, tag, name and status, and the start of the message. A missing, non-finite or
|
|
19
|
+
* non-positive `maxLineBytes` uses `DEFAULT_MAX_LINE_BYTES`.
|
|
20
|
+
*/
|
|
21
|
+
export declare const makeJsonLogger: (options?: StructuredLoggerOptions) => Logger.Logger<unknown, string>;
|
|
22
|
+
/**
|
|
23
|
+
* Sends formatted output to stderr or stdout. With no `mode`, a line goes to stderr only when
|
|
24
|
+
* `Logger.LogToStderr` is set. `true` always uses stderr. `leveled` also honors `LogToStderr`, and
|
|
25
|
+
* otherwise sends Warn, Error and Fatal to `console.error` and everything else to `console.log`. It
|
|
26
|
+
* never calls `console.trace`, `console.debug` or `console.info`.
|
|
27
|
+
*/
|
|
28
|
+
export declare const withConsoleRouting: <Message, Output>(logger: Logger.Logger<Message, Output>, mode?: LogStderrMode) => Logger.Logger<Message, void>;
|
|
29
|
+
/**
|
|
30
|
+
* Options for the console logger. `format` selects the encoder.
|
|
31
|
+
*/
|
|
32
|
+
export interface ConsoleLoggerOptions extends StructuredLoggerOptions {
|
|
33
|
+
/**
|
|
34
|
+
* Encoder. Pretty uses Effect's console logger and does not add service or trace fields.
|
|
35
|
+
*/
|
|
36
|
+
readonly format: LogFormat;
|
|
37
|
+
/**
|
|
38
|
+
* ANSI color mode for the pretty encoder. Ignored by JSON, structured, and logfmt.
|
|
39
|
+
*/
|
|
40
|
+
readonly colors?: 'auto' | boolean | undefined;
|
|
41
|
+
/**
|
|
42
|
+
* Stream routing. `leveled` sends Warn, Error and Fatal to stderr and the rest to stdout, and
|
|
43
|
+
* `true` sends every line to stderr. Omit or use `false` to follow `Logger.LogToStderr` only.
|
|
44
|
+
*/
|
|
45
|
+
readonly stderr?: LogStderrMode | undefined;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Builds the console logger for `format`. JSON and structured include service and span ids. Pretty
|
|
49
|
+
* and logfmt use Effect's own formatters behind `withRedaction`. Every format is redacted with the
|
|
50
|
+
* built-in sensitive names plus `options.redaction`.
|
|
51
|
+
*/
|
|
52
|
+
export declare const makeConsoleLogger: (options: ConsoleLoggerOptions) => Logger.Logger<unknown, void>;
|
|
53
|
+
export { DEFAULT_MAX_LINE_BYTES };
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
import { summarizeCause } from "./error.js";
|
|
2
|
+
import { DEFAULT_MAX_LINE_BYTES, fitRecord, resolveMaxLineBytes } from "./line-cap.js";
|
|
3
|
+
import { omitUndefined } from "./omit-undefined.js";
|
|
4
|
+
import { defaultLogRedaction, makeLogRedaction, overrideFiberRefs } from "./redact.js";
|
|
5
|
+
import { Console, Context, Logger, Option, Tracer } from "effect";
|
|
6
|
+
// src/formatters.ts
|
|
7
|
+
/**
|
|
8
|
+
* Levels written to stderr in `leveled` mode.
|
|
9
|
+
*/
|
|
10
|
+
const STDERR_LEVELS = /* @__PURE__ */ new Set([
|
|
11
|
+
"Warn",
|
|
12
|
+
"Error",
|
|
13
|
+
"Fatal"
|
|
14
|
+
]);
|
|
15
|
+
/**
|
|
16
|
+
* Redactions built by {@link resolveRedaction}, by the options object they were built from.
|
|
17
|
+
*/
|
|
18
|
+
const redactionsByOptions = /* @__PURE__ */ new WeakMap();
|
|
19
|
+
/**
|
|
20
|
+
* Returns the redaction for `options`: the built-in sensitive names when none are given. The same
|
|
21
|
+
* options object always gives the same redaction, so the console, tracer and fan-out loggers of one
|
|
22
|
+
* layer share its memos and redact each line once between them.
|
|
23
|
+
*/
|
|
24
|
+
const resolveRedaction = (options) => {
|
|
25
|
+
if (options === void 0) return defaultLogRedaction;
|
|
26
|
+
const existing = redactionsByOptions.get(options);
|
|
27
|
+
if (existing !== void 0) return existing;
|
|
28
|
+
const created = makeLogRedaction(options);
|
|
29
|
+
redactionsByOptions.set(options, created);
|
|
30
|
+
return created;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Builds a logger that emits a `StructuredLogRecord`. The message, annotations, log span labels and
|
|
34
|
+
* cause are redacted with the built-in sensitive names plus `options.redaction` (see
|
|
35
|
+
* `redactLogValue` and `sanitizeCause`), and `Redacted` values print as `<redacted>`. The `error`
|
|
36
|
+
* summary is read from the original cause, so its status survives, and its message, tag and name
|
|
37
|
+
* are masked. A parent span adds `traceId` and `spanId`.
|
|
38
|
+
*/
|
|
39
|
+
const makeStructuredLogger = (options = {}) => {
|
|
40
|
+
const redaction = resolveRedaction(options.redaction);
|
|
41
|
+
return Logger.make((logOptions) => {
|
|
42
|
+
const base = Logger.formatStructured.log(redaction.redactLogOptions(logOptions));
|
|
43
|
+
const activeSpan = Option.getOrUndefined(Context.getOption(logOptions.fiber.context, Tracer.ParentSpan));
|
|
44
|
+
const error = summarizeCause(logOptions.cause, redaction);
|
|
45
|
+
return {
|
|
46
|
+
timestamp: base.timestamp,
|
|
47
|
+
level: base.level,
|
|
48
|
+
...omitUndefined({
|
|
49
|
+
service: options.service,
|
|
50
|
+
env: options.env,
|
|
51
|
+
version: options.version
|
|
52
|
+
}),
|
|
53
|
+
message: base.message,
|
|
54
|
+
fiberId: base.fiberId,
|
|
55
|
+
...omitUndefined({
|
|
56
|
+
traceId: activeSpan?.traceId,
|
|
57
|
+
spanId: activeSpan?.spanId
|
|
58
|
+
}),
|
|
59
|
+
annotations: base.annotations,
|
|
60
|
+
spans: base.spans,
|
|
61
|
+
...omitUndefined({
|
|
62
|
+
cause: base.cause,
|
|
63
|
+
error
|
|
64
|
+
})
|
|
65
|
+
};
|
|
66
|
+
});
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* Builds a logger that emits one JSON line per record, redacted like `makeStructuredLogger`. A line
|
|
70
|
+
* longer than `maxLineBytes` in UTF-8, newline included, is shortened: `truncated: true` is added,
|
|
71
|
+
* then `cause`, `error.message` and `message` are cut to the longest start that fits, and a record
|
|
72
|
+
* that still does not fit is replaced by a minimal record with the identity fields, trace ids, the
|
|
73
|
+
* error's kind, tag, name and status, and the start of the message. A missing, non-finite or
|
|
74
|
+
* non-positive `maxLineBytes` uses `DEFAULT_MAX_LINE_BYTES`.
|
|
75
|
+
*/
|
|
76
|
+
const makeJsonLogger = (options = {}) => {
|
|
77
|
+
const maxBytes = resolveMaxLineBytes(options.maxLineBytes);
|
|
78
|
+
return Logger.map(makeStructuredLogger(options), (record) => fitRecord(record, maxBytes));
|
|
79
|
+
};
|
|
80
|
+
/**
|
|
81
|
+
* Sends formatted output to stderr or stdout. With no `mode`, a line goes to stderr only when
|
|
82
|
+
* `Logger.LogToStderr` is set. `true` always uses stderr. `leveled` also honors `LogToStderr`, and
|
|
83
|
+
* otherwise sends Warn, Error and Fatal to `console.error` and everything else to `console.log`. It
|
|
84
|
+
* never calls `console.trace`, `console.debug` or `console.info`.
|
|
85
|
+
*/
|
|
86
|
+
const withConsoleRouting = (logger, mode) => Logger.make((logOptions) => {
|
|
87
|
+
const output = logger.log(logOptions);
|
|
88
|
+
const consoleService = logOptions.fiber.getRef(Console.Console);
|
|
89
|
+
const forced = mode === true || logOptions.fiber.getRef(Logger.LogToStderr);
|
|
90
|
+
const leveled = mode === "leveled" && STDERR_LEVELS.has(logOptions.logLevel);
|
|
91
|
+
if (forced || leveled) {
|
|
92
|
+
consoleService.error(output);
|
|
93
|
+
return;
|
|
94
|
+
}
|
|
95
|
+
consoleService.log(output);
|
|
96
|
+
});
|
|
97
|
+
/**
|
|
98
|
+
* Wraps Effect's pretty logger, which picks its stream from `Logger.LogToStderr`, so that the lines
|
|
99
|
+
* `mode` sends to stderr see the ref as true: Warn, Error and Fatal for `leveled`, and every line
|
|
100
|
+
* for `true`. Other modes return `logger` unchanged, so the ref alone decides.
|
|
101
|
+
*/
|
|
102
|
+
const withPrettyStderr = (logger, mode) => {
|
|
103
|
+
if (mode !== true && mode !== "leveled") return logger;
|
|
104
|
+
return Logger.make((logOptions) => {
|
|
105
|
+
if (mode === "leveled" && !STDERR_LEVELS.has(logOptions.logLevel)) {
|
|
106
|
+
logger.log(logOptions);
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
const overrides = Context.make(Logger.LogToStderr, true);
|
|
110
|
+
logger.log({
|
|
111
|
+
...logOptions,
|
|
112
|
+
fiber: overrideFiberRefs(logOptions.fiber, overrides)
|
|
113
|
+
});
|
|
114
|
+
});
|
|
115
|
+
};
|
|
116
|
+
/**
|
|
117
|
+
* Builds the console logger for `format`. JSON and structured include service and span ids. Pretty
|
|
118
|
+
* and logfmt use Effect's own formatters behind `withRedaction`. Every format is redacted with the
|
|
119
|
+
* built-in sensitive names plus `options.redaction`.
|
|
120
|
+
*/
|
|
121
|
+
const makeConsoleLogger = (options) => {
|
|
122
|
+
switch (options.format) {
|
|
123
|
+
case "json": return withConsoleRouting(makeJsonLogger(options), options.stderr);
|
|
124
|
+
case "structured": return withConsoleRouting(makeStructuredLogger(options), options.stderr);
|
|
125
|
+
case "logfmt": {
|
|
126
|
+
const redaction = resolveRedaction(options.redaction);
|
|
127
|
+
return withConsoleRouting(redaction.withRedaction(Logger.formatLogFmt), options.stderr);
|
|
128
|
+
}
|
|
129
|
+
case "pretty": {
|
|
130
|
+
const redaction = resolveRedaction(options.redaction);
|
|
131
|
+
const pretty = Logger.consolePretty({ colors: options.colors ?? "auto" });
|
|
132
|
+
return redaction.withRedaction(withPrettyStderr(pretty, options.stderr));
|
|
133
|
+
}
|
|
134
|
+
default: return options.format;
|
|
135
|
+
}
|
|
136
|
+
};
|
|
137
|
+
export { DEFAULT_MAX_LINE_BYTES, makeConsoleLogger, makeJsonLogger, makeStructuredLogger, resolveRedaction, withConsoleRouting };
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { CALLBACK_LOGGER_CAPACITY, CallbackLogFunction, CallbackLogger, CallbackLoggerOptions, callbackLoggerDropped, makeCallbackLogger } from "./callback-logger.js";
|
|
2
|
+
import { parseLogColors, parseLogFormat, parseLogLevel, parseLogStderr, readLoggerConfig, resolveLogFormat } from "./config.js";
|
|
3
|
+
import { compactLogFields, logDebug, logError, logErrorCause, logFailure, logFatal, logInfo, logTrace, logWarning, logWithFields, withLogContext, withLogContextScoped } from "./context.js";
|
|
4
|
+
import { CrashHandlerOptions, CrashHandlersLive, CrashTarget, StartupFailureOptions, describeStartupFailure, makeCrashHandlersLayer, makeStartupFailureTeardown, startupFailureTeardown } from "./crash.js";
|
|
5
|
+
import { describeError, extractCauseSummary, extractErrorFields } from "./error.js";
|
|
6
|
+
import { ConsoleLoggerOptions, makeConsoleLogger, makeJsonLogger, makeStructuredLogger, withConsoleRouting } from "./formatters.js";
|
|
7
|
+
import { RedactedLogRecord, RedactedLogValue, UNSERIALIZABLE_LOG_VALUE } from "./redact-walk.js";
|
|
8
|
+
import { LoggerLive, SilentLoggerLayer, makeLoggerLayer, redactedTracerLogger } from "./layer.js";
|
|
9
|
+
import { LogExportSinks, logExportSinkLayer } from "./log-fan-out.js";
|
|
10
|
+
import { DEFAULT_MAX_LINE_BYTES } from "./line-cap.js";
|
|
11
|
+
import { durationHistogram, durationMsBoundaries, recordDurationMs } from "./metrics.js";
|
|
12
|
+
import { REDACTED_LOG_VALUE, RedactionOptions, isSensitiveLogKey, isSensitiveQueryParamName, redactLogString } from "./credentials.js";
|
|
13
|
+
import { LogRedaction, defaultLogRedaction, makeLogRedaction, redactLogValue, sanitizeCause, withRedaction, withTracerRedaction } from "./redact.js";
|
|
14
|
+
import { REQUEST_ID_HEADER, generateRequestId, isValidRequestId, requestIdFromHeaders, resolveRequestId } from "./request-id.js";
|
|
15
|
+
import { ConfiguredLogFormat, ErrorSummary, LogFields, LogFormat, LogStderrMode, LoggerLayerOptions, ResolvedLoggerConfig, StructuredLogRecord, StructuredLoggerOptions } from "./types.js";
|
|
16
|
+
export { CALLBACK_LOGGER_CAPACITY, type CallbackLogFunction, type CallbackLogger, type CallbackLoggerOptions, type ConfiguredLogFormat, type ConsoleLoggerOptions, type CrashHandlerOptions, CrashHandlersLive, type CrashTarget, DEFAULT_MAX_LINE_BYTES, type ErrorSummary, LogExportSinks, type LogFields, type LogFormat, type LogRedaction, type LogStderrMode, type LoggerLayerOptions, LoggerLive, REDACTED_LOG_VALUE, REQUEST_ID_HEADER, type RedactedLogRecord, type RedactedLogValue, type RedactionOptions, type ResolvedLoggerConfig, SilentLoggerLayer, type StartupFailureOptions, type StructuredLogRecord, type StructuredLoggerOptions, UNSERIALIZABLE_LOG_VALUE, callbackLoggerDropped, compactLogFields, defaultLogRedaction, describeError, describeStartupFailure, durationHistogram, durationMsBoundaries, extractCauseSummary, extractErrorFields, generateRequestId, isSensitiveLogKey, isSensitiveQueryParamName, isValidRequestId, logDebug, logError, logErrorCause, logExportSinkLayer, logFailure, logFatal, logInfo, logTrace, logWarning, logWithFields, makeCallbackLogger, makeConsoleLogger, makeCrashHandlersLayer, makeJsonLogger, makeLogRedaction, makeLoggerLayer, makeStartupFailureTeardown, makeStructuredLogger, parseLogColors, parseLogFormat, parseLogLevel, parseLogStderr, readLoggerConfig, recordDurationMs, redactLogString, redactLogValue, redactedTracerLogger, requestIdFromHeaders, resolveLogFormat, resolveRequestId, sanitizeCause, startupFailureTeardown, withConsoleRouting, withLogContext, withLogContextScoped, withRedaction, withTracerRedaction };
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { CALLBACK_LOGGER_CAPACITY, callbackLoggerDropped, makeCallbackLogger } from "./callback-logger.js";
|
|
2
|
+
import { UNSERIALIZABLE_LOG_VALUE } from "./redact-walk.js";
|
|
3
|
+
import { parseLogColors, parseLogFormat, parseLogLevel, parseLogStderr, readLoggerConfig, resolveLogFormat } from "./config.js";
|
|
4
|
+
import { compactLogFields, logDebug, logError, logErrorCause, logFailure, logFatal, logInfo, logTrace, logWarning, logWithFields, withLogContext, withLogContextScoped } from "./context.js";
|
|
5
|
+
import { CrashHandlersLive, describeStartupFailure, makeCrashHandlersLayer, makeStartupFailureTeardown, startupFailureTeardown } from "./crash.js";
|
|
6
|
+
import { describeError, extractCauseSummary, extractErrorFields } from "./error.js";
|
|
7
|
+
import { makeConsoleLogger, makeJsonLogger, makeStructuredLogger, withConsoleRouting } from "./formatters.js";
|
|
8
|
+
import { LoggerLive, SilentLoggerLayer, makeLoggerLayer, redactedTracerLogger } from "./layer.js";
|
|
9
|
+
import { DEFAULT_MAX_LINE_BYTES } from "./line-cap.js";
|
|
10
|
+
import { LogExportSinks, logExportSinkLayer } from "./log-fan-out.js";
|
|
11
|
+
import { durationHistogram, durationMsBoundaries, recordDurationMs } from "./metrics.js";
|
|
12
|
+
import { REDACTED_LOG_VALUE, isSensitiveLogKey, isSensitiveQueryParamName, redactLogString } from "./credentials.js";
|
|
13
|
+
import { defaultLogRedaction, makeLogRedaction, redactLogValue, sanitizeCause, withRedaction, withTracerRedaction } from "./redact.js";
|
|
14
|
+
import { REQUEST_ID_HEADER, generateRequestId, isValidRequestId, requestIdFromHeaders, resolveRequestId } from "./request-id.js";
|
|
15
|
+
export { CALLBACK_LOGGER_CAPACITY, CrashHandlersLive, DEFAULT_MAX_LINE_BYTES, LogExportSinks, LoggerLive, REDACTED_LOG_VALUE, REQUEST_ID_HEADER, SilentLoggerLayer, UNSERIALIZABLE_LOG_VALUE, callbackLoggerDropped, compactLogFields, defaultLogRedaction, describeError, describeStartupFailure, durationHistogram, durationMsBoundaries, extractCauseSummary, extractErrorFields, generateRequestId, isSensitiveLogKey, isSensitiveQueryParamName, isValidRequestId, logDebug, logError, logErrorCause, logExportSinkLayer, logFailure, logFatal, logInfo, logTrace, logWarning, logWithFields, makeCallbackLogger, makeConsoleLogger, makeCrashHandlersLayer, makeJsonLogger, makeLogRedaction, makeLoggerLayer, makeStartupFailureTeardown, makeStructuredLogger, parseLogColors, parseLogFormat, parseLogLevel, parseLogStderr, readLoggerConfig, recordDurationMs, redactLogString, redactLogValue, redactedTracerLogger, requestIdFromHeaders, resolveLogFormat, resolveRequestId, sanitizeCause, startupFailureTeardown, withConsoleRouting, withLogContext, withLogContextScoped, withRedaction, withTracerRedaction };
|
package/dist/layer.d.ts
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import "./redact.js";
|
|
2
|
+
import { LoggerLayerOptions } from "./types.js";
|
|
3
|
+
import { Config, Layer, Logger } from "effect";
|
|
4
|
+
/**
|
|
5
|
+
* Effect's `Logger.tracerLogger` redacted with the built-in sensitive names, so span events (and
|
|
6
|
+
* the traces exported from them) carry the same redacted message, annotations and cause as the
|
|
7
|
+
* console line. Lines logged outside a recording span cost nothing. `makeLoggerLayer` builds its
|
|
8
|
+
* own with the layer's `redaction` option.
|
|
9
|
+
*/
|
|
10
|
+
export declare const redactedTracerLogger: Logger.Logger<unknown, void>;
|
|
11
|
+
/**
|
|
12
|
+
* Drops every log line by setting the minimum level to `None`.
|
|
13
|
+
*/
|
|
14
|
+
export declare const SilentLoggerLayer: Layer.Layer<never>;
|
|
15
|
+
/**
|
|
16
|
+
* Installs the console logger from `options`. Does not read the environment. `stderr` of `true` or
|
|
17
|
+
* `false` sets `Logger.LogToStderr`, and `leveled` (the default) routes by level. Includes a
|
|
18
|
+
* redacted `Logger.tracerLogger` unless `includeTracerLogger` is false. Always includes the fan-out
|
|
19
|
+
* logger that forwards each record to the log export sinks `telemetryLayer` provides (the OTLP log
|
|
20
|
+
* exporter). Every one of them redacts with the built-in sensitive names plus `options.redaction`,
|
|
21
|
+
* and none of them can fail the fiber that logs: a line one of them cannot write is retried with
|
|
22
|
+
* only the message's string parts, and dropped when that fails too.
|
|
23
|
+
*/
|
|
24
|
+
export declare const makeLoggerLayer: (options?: LoggerLayerOptions) => Layer.Layer<never>;
|
|
25
|
+
/**
|
|
26
|
+
* Installs the console logger from `LOG_LEVEL`, `LOG_FORMAT`, `LOG_STDERR`, `LOG_COLORS`, and
|
|
27
|
+
* `NODE_ENV`, falling back to `defaults` (which also carry `service`, `version`, `maxLineBytes`,
|
|
28
|
+
* `includeTracerLogger` and `redaction`). Invalid values fail with `ConfigError` before a line is
|
|
29
|
+
* written.
|
|
30
|
+
*/
|
|
31
|
+
export declare const LoggerLive: (defaults?: LoggerLayerOptions) => Layer.Layer<never, Config.ConfigError>;
|
package/dist/layer.js
ADDED
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import { readLoggerConfig, resolveLogFormat } from "./config.js";
|
|
2
|
+
import { makeConsoleLogger, resolveRedaction } from "./formatters.js";
|
|
3
|
+
import { makeFanOutLogger } from "./log-fan-out.js";
|
|
4
|
+
import { defaultLogRedaction, guardLogger } from "./redact.js";
|
|
5
|
+
import { Effect, Layer, Logger, References } from "effect";
|
|
6
|
+
// src/layer.ts
|
|
7
|
+
/**
|
|
8
|
+
* Tells whether `Logger.tracerLogger` would add an event for a line logged on `fiber`: the fiber
|
|
9
|
+
* has a current span that is not external and has not ended. This is the check Effect's tracer
|
|
10
|
+
* logger runs before it reads anything else.
|
|
11
|
+
*/
|
|
12
|
+
const hasRecordingSpan = (fiber) => {
|
|
13
|
+
const span = fiber.cache.span;
|
|
14
|
+
return span !== void 0 && span._tag !== "ExternalSpan" && span.status._tag !== "Ended";
|
|
15
|
+
};
|
|
16
|
+
/**
|
|
17
|
+
* Effect's `Logger.tracerLogger` behind `redaction.withRedaction`. A line logged outside a
|
|
18
|
+
* recording span (see {@link hasRecordingSpan}) is dropped before anything is redacted, since the
|
|
19
|
+
* tracer logger would drop it anyway; inside one, the span event carries the redacted message,
|
|
20
|
+
* annotations and cause.
|
|
21
|
+
*/
|
|
22
|
+
const makeRedactedTracerLogger = (redaction) => {
|
|
23
|
+
const redacted = redaction.withRedaction(Logger.tracerLogger);
|
|
24
|
+
return Logger.make((options) => {
|
|
25
|
+
if (hasRecordingSpan(options.fiber)) redacted.log(options);
|
|
26
|
+
});
|
|
27
|
+
};
|
|
28
|
+
/**
|
|
29
|
+
* Effect's `Logger.tracerLogger` redacted with the built-in sensitive names, so span events (and
|
|
30
|
+
* the traces exported from them) carry the same redacted message, annotations and cause as the
|
|
31
|
+
* console line. Lines logged outside a recording span cost nothing. `makeLoggerLayer` builds its
|
|
32
|
+
* own with the layer's `redaction` option.
|
|
33
|
+
*/
|
|
34
|
+
const redactedTracerLogger = makeRedactedTracerLogger(defaultLogRedaction);
|
|
35
|
+
/**
|
|
36
|
+
* Drops every log line by setting the minimum level to `None`.
|
|
37
|
+
*/
|
|
38
|
+
const SilentLoggerLayer = Layer.succeed(References.MinimumLogLevel, "None");
|
|
39
|
+
/**
|
|
40
|
+
* Installs the console logger from `options`. Does not read the environment. `stderr` of `true` or
|
|
41
|
+
* `false` sets `Logger.LogToStderr`, and `leveled` (the default) routes by level. Includes a
|
|
42
|
+
* redacted `Logger.tracerLogger` unless `includeTracerLogger` is false. Always includes the fan-out
|
|
43
|
+
* logger that forwards each record to the log export sinks `telemetryLayer` provides (the OTLP log
|
|
44
|
+
* exporter). Every one of them redacts with the built-in sensitive names plus `options.redaction`,
|
|
45
|
+
* and none of them can fail the fiber that logs: a line one of them cannot write is retried with
|
|
46
|
+
* only the message's string parts, and dropped when that fails too.
|
|
47
|
+
*/
|
|
48
|
+
const makeLoggerLayer = (options = {}) => {
|
|
49
|
+
const format = resolveLogFormat(options.format ?? "auto", options.env);
|
|
50
|
+
const redaction = resolveRedaction(options.redaction);
|
|
51
|
+
const consoleLogger = makeConsoleLogger({
|
|
52
|
+
format,
|
|
53
|
+
service: options.service,
|
|
54
|
+
env: options.env,
|
|
55
|
+
version: options.version,
|
|
56
|
+
colors: options.colors,
|
|
57
|
+
maxLineBytes: options.maxLineBytes,
|
|
58
|
+
stderr: options.stderr ?? "leveled",
|
|
59
|
+
redaction: options.redaction
|
|
60
|
+
});
|
|
61
|
+
const fanOutLogger = makeFanOutLogger(redaction);
|
|
62
|
+
const loggers = options.includeTracerLogger ?? true ? [
|
|
63
|
+
consoleLogger,
|
|
64
|
+
makeRedactedTracerLogger(redaction),
|
|
65
|
+
fanOutLogger
|
|
66
|
+
] : [consoleLogger, fanOutLogger];
|
|
67
|
+
const baseLayer = Layer.mergeAll(Logger.layer(loggers.map(guardLogger)), Layer.succeed(References.MinimumLogLevel, options.minLevel ?? "Info"));
|
|
68
|
+
if (typeof options.stderr === "boolean") return Layer.mergeAll(baseLayer, Layer.succeed(Logger.LogToStderr, options.stderr));
|
|
69
|
+
return baseLayer;
|
|
70
|
+
};
|
|
71
|
+
/**
|
|
72
|
+
* Installs the console logger from `LOG_LEVEL`, `LOG_FORMAT`, `LOG_STDERR`, `LOG_COLORS`, and
|
|
73
|
+
* `NODE_ENV`, falling back to `defaults` (which also carry `service`, `version`, `maxLineBytes`,
|
|
74
|
+
* `includeTracerLogger` and `redaction`). Invalid values fail with `ConfigError` before a line is
|
|
75
|
+
* written.
|
|
76
|
+
*/
|
|
77
|
+
const LoggerLive = (defaults = {}) => Layer.unwrap(Effect.map(readLoggerConfig(defaults), makeLoggerLayer));
|
|
78
|
+
export { LoggerLive, SilentLoggerLayer, makeLoggerLayer, makeRedactedTracerLogger, redactedTracerLogger };
|