The best way to control Claude Code, Codex and more from any computer or phone
| 1 | import { stripVTControlCharacters } from "node:util"; |
| 2 | import * as Console from "effect/Console"; |
| 3 | import * as DateTime from "effect/DateTime"; |
| 4 | import * as Logger from "effect/Logger"; |
| 5 | import type * as LogLevel from "effect/LogLevel"; |
| 6 | import { LogAnnotation } from "./LogAnnotation.ts"; |
| 7 | import { recordOf, safeStringify, severities } from "./LogRecord.ts"; |
| 8 | import { formatResourceTag } from "./ResourceOutput.ts"; |
| 9 | import { ansiFg, ANSI_RESET, colorsEnabled } from "./Terminal.ts"; |
| 10 | import { theme } from "./Theme.ts"; |
| 11 | |
| 12 | export interface PrettyLoggerOptions { |
| 13 | /** |
| 14 | * Color the prefix and resource tag, and keep the styling child processes |
| 15 | * wrote (vite, workerd); without colors their text is stripped. |
| 16 | * @default colorsEnabled() |
| 17 | */ |
| 18 | readonly colors?: boolean; |
| 19 | } |
| 20 | |
| 21 | const levelColor: Partial<Record<LogLevel.LogLevel, string>> = { |
| 22 | Fatal: theme.color.danger, |
| 23 | Error: theme.color.danger, |
| 24 | Warn: theme.color.warning, |
| 25 | Info: theme.color.success, |
| 26 | Debug: theme.color.info, |
| 27 | }; |
| 28 | |
| 29 | const clock = (date: Date) => |
| 30 | DateTime.formatLocal(DateTime.fromDateUnsafe(date), { |
| 31 | locale: "en-GB", |
| 32 | hour: "2-digit", |
| 33 | minute: "2-digit", |
| 34 | second: "2-digit", |
| 35 | fractionalSecondDigits: 3, |
| 36 | }); |
| 37 | |
| 38 | /** One terminal line: `[12:00:00.000] INFO: [Site/Worker] text`. */ |
| 39 | export const prettyLine = (options: { |
| 40 | readonly date: Date; |
| 41 | readonly level: LogLevel.Severity; |
| 42 | readonly text: string; |
| 43 | readonly resource?: string; |
| 44 | readonly colors?: boolean; |
| 45 | }): string => { |
| 46 | const colors = options.colors ?? colorsEnabled(); |
| 47 | const paint = (color: string | undefined, text: string) => |
| 48 | colors && color !== undefined ? `${ansiFg(color)}${text}${ANSI_RESET}` : text; |
| 49 | const tag = |
| 50 | options.resource === undefined ? "" : `${formatResourceTag(options.resource, colors)} `; |
| 51 | return `${paint(theme.color.muted, `[${clock(options.date)}]`)} ${paint(levelColor[options.level], options.level.toUpperCase())}: ${tag}${options.text}`; |
| 52 | }; |
| 53 | |
| 54 | /** |
| 55 | * The terminal rendering of a log record: `log` and `output` records (typed |
| 56 | * events are drawn by the renderers subscribed to them). The resource tag |
| 57 | * comes from the record, so producers log plain text and never format it. |
| 58 | */ |
| 59 | export const makePlainConsoleSink = (options: PrettyLoggerOptions = {}) => { |
| 60 | const colors = options.colors ?? colorsEnabled(); |
| 61 | return Logger.make<unknown, void>((options) => { |
| 62 | const record = recordOf(options); |
| 63 | if (record.kind !== "log" && record.kind !== "output") return; |
| 64 | const console = options.fiber.getRef(Console.Console); |
| 65 | const text = record.kind === "log" ? record.data.message : record.data.text; |
| 66 | const { [LogAnnotation.fqn]: fqn, ...attributes } = record.attributes; |
| 67 | console.log( |
| 68 | prettyLine({ |
| 69 | date: new Date(record.time), |
| 70 | level: severities[record.level], |
| 71 | text: colors ? text : stripVTControlCharacters(text), |
| 72 | resource: typeof fqn === "string" ? fqn : undefined, |
| 73 | colors, |
| 74 | }), |
| 75 | ); |
| 76 | for (const [key, value] of Object.entries(attributes)) { |
| 77 | console.log(` ${key}: ${typeof value === "string" ? value : safeStringify(value)}`); |
| 78 | } |
| 79 | if (record.kind === "log" && record.data.cause !== null) console.log(record.data.cause); |
| 80 | }); |
| 81 | }; |