Open History to see the last commit
1import * as HostProcess from "@t3tools/shared/HostProcess";
2import * as SharedObservability from "@t3tools/shared/observability";
3import * as Cause from "effect/Cause";
4import * as Effect from "effect/Effect";
5import * as Layer from "effect/Layer";
6import * as Logger from "effect/Logger";
7import * as Redactable from "effect/Redactable";
8import * as References from "effect/References";
9import * as OtlpExporter from "effect/observability/OtlpExporter";
10import * as OtlpLogger from "effect/observability/OtlpLogger";
11
12import * as ServerConfig from "./config.ts";
13
14/**
15 * `Logger.consolePretty` prints message objects with `util.inspect`, which
16 * collapses a `Cause` passed as a field (`{ cause }`) to
17 * `{ failures: [ [Object] ] }`. Render those fields with `Cause.pretty`, the
18 * same text a `Cause` passed as its own log argument already gets.
19 */
20const consolePretty = (): Logger.Logger<unknown, void> => {
21 const pretty = Logger.consolePretty();
22 return Logger.make((options) =>
23 pretty.log({
24 ...options,
25 message: Array.isArray(options.message)
26 ? options.message.map(prettyCauseFields)
27 : prettyCauseFields(options.message),
28 }),
29 );
30};
31
32function prettyCauseFields(value: unknown): unknown {
33 if (typeof value !== "object" || value === null) return value;
34 const prototype: unknown = Object.getPrototypeOf(value);
35 if (prototype !== Object.prototype && prototype !== null) return value;
36 // A message with its own redaction hook already decides how it prints.
37 if (Redactable.isRedactable(value)) return value;
38 // Copy descriptors rather than entries so getters are never invoked and
39 // symbol keys survive the copy.
40 const descriptors = Object.getOwnPropertyDescriptors(value);
41 let changed = false;
42 for (const descriptor of Object.values(descriptors)) {
43 if ("value" in descriptor && Cause.isCause(descriptor.value)) {
44 descriptor.value = Cause.pretty(descriptor.value);
45 changed = true;
46 }
47 }
48 return changed ? Object.create(prototype, descriptors) : value;
49}
50
51export const layer = Effect.gen(function* () {
52 const config = yield* ServerConfig.ServerConfig;
53 const hostName = yield* HostProcess.Hostname;
54 const layerMinimumLogLevel = Layer.succeed(References.MinimumLogLevel, config.logLevel);
55
56 const logs = config.otlpLogsExport;
57 const otlpLogger =
58 config.otlpLogsUrl === undefined
59 ? undefined
60 : OtlpLogger.make({
61 url: config.otlpLogsUrl,
62 exportInterval: `${logs.exportIntervalMs} millis`,
63 headers: logs.headers,
64 resource: ServerConfig.otlpResource(config, hostName),
65 });
66
67 // `Logger.layer` writes the whole logger set rather than adding to it, so
68 // every logger the server wants has to be named in this one call.
69 // `Logger.tracerLogger` reaches a collector by attaching each message to the
70 // active span as a span event, which covers only messages logged inside a
71 // recorded span and files them under traces. The OTLP logger carries the same
72 // messages as log records stamped with their trace and span ids, so it is a
73 // superset: keeping both would export every in-span message twice.
74 const layerLogger = Logger.layer(
75 otlpLogger === undefined
76 ? [consolePretty(), Logger.tracerLogger]
77 : [consolePretty(), otlpLogger],
78 { mergeWithExisting: false },
79 ).pipe(
80 Layer.provide(OtlpExporter.layerFlusher),
81 Layer.provide(SharedObservability.layerOtlpSerialization(logs.protocol)),
82 );
83
84 return Layer.mergeAll(layerLogger, layerMinimumLogLevel);
85}).pipe(Layer.unwrap);