@devries-cameron20 / t3code Mirror · read-only
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 21, 03:37 UTC

main
ts 82 lines 3.42 KB
Raw
1 import * as HostProcess from "@t3tools/shared/HostProcess";
2 import * as SharedObservability from "@t3tools/shared/observability";
3 import * as Cause from "effect/Cause";
4 import * as Effect from "effect/Effect";
5 import * as Layer from "effect/Layer";
6 import * as Logger from "effect/Logger";
7 import * as Redactable from "effect/Redactable";
8 import * as References from "effect/References";
9 import * as OtlpExporter from "effect/observability/OtlpExporter";
10 import * as OtlpLogger from "effect/observability/OtlpLogger";
11
12 import * as ServerConfig from "./config.ts";
13 import * as OtlpExportFailures from "./observability/OtlpExportFailures.ts";
14
15 // HTTP errors logged with --log-ws-events carry the request URL and its connection ticket.
16 const redactingCredentials = <Output>(logger: Logger.Logger<unknown, Output>) =>
17 Logger.make<unknown, Output>((options) =>
18 logger.log({ ...options, cause: SharedObservability.redactCauseCredentials(options.cause) }),
19 );
20
21 // consolePretty collapses Cause fields via util.inspect; render them with Cause.pretty like standalone arguments.
22 const consolePretty = (): Logger.Logger<unknown, void> => {
23 const pretty = redactingCredentials(Logger.consolePretty());
24 return Logger.make((options) =>
25 pretty.log({
26 ...options,
27 message: Array.isArray(options.message)
28 ? options.message.map(prettyCauseFields)
29 : prettyCauseFields(options.message),
30 }),
31 );
32 };
33
34 function prettyCauseFields(value: unknown): unknown {
35 if (typeof value !== "object" || value === null) return value;
36 const prototype: unknown = Object.getPrototypeOf(value);
37 if (prototype !== Object.prototype && prototype !== null) return value;
38 // A message with its own redaction hook already decides how it prints.
39 if (Redactable.isRedactable(value)) return value;
40 // Copy descriptors to preserve symbols without invoking getters.
41 const descriptors = Object.getOwnPropertyDescriptors(value);
42 let changed = false;
43 for (const descriptor of Object.values(descriptors)) {
44 if ("value" in descriptor && Cause.isCause(descriptor.value)) {
45 descriptor.value = Cause.pretty(descriptor.value);
46 changed = true;
47 }
48 }
49 return changed ? Object.create(prototype, descriptors) : value;
50 }
51
52 export const layer = Effect.gen(function* () {
53 const config = yield* ServerConfig.ServerConfig;
54 const hostName = yield* HostProcess.Hostname;
55 const layerMinimumLogLevel = Layer.succeed(References.MinimumLogLevel, config.logLevel);
56
57 const logs = config.otlpLogsExport;
58 const otlpLogger =
59 config.otlpLogsUrl === undefined
60 ? undefined
61 : OtlpLogger.make({
62 url: config.otlpLogsUrl,
63 exportInterval: `${logs.exportIntervalMs} millis`,
64 headers: logs.headers,
65 resource: ServerConfig.otlpResource(config, hostName),
66 }).pipe(Effect.map(redactingCredentials));
67
68 // Logger.layer replaces the whole set; name every logger here.
69 // OTLP includes traced messages as log records, so also using tracerLogger would duplicate in-span exports.
70 const layerLogger = Logger.layer(
71 otlpLogger === undefined
72 ? [consolePretty(), Logger.tracerLogger]
73 : [consolePretty(), otlpLogger],
74 { mergeWithExisting: false },
75 ).pipe(
76 Layer.provide(OtlpExporter.layerFlusher),
77 Layer.provide(OtlpExportFailures.layer("logs")),
78 Layer.provide(SharedObservability.layerOtlpSerialization(logs.protocol)),
79 );
80
81 return Layer.mergeAll(layerLogger, layerMinimumLogLevel);
82 }).pipe(Layer.unwrap);