fix(logging): simplify tty color detection
This commit is contained in:
@@ -49,7 +49,6 @@ If set, CLAWDIS derives defaults (only when you haven’t set them explicitly):
|
||||
- Console output can be tuned separately via:
|
||||
- `logging.consoleLevel` (defaults to `info`, bumps to `debug` when `--verbose`)
|
||||
- `logging.consoleStyle` (`pretty` | `compact` | `json`)
|
||||
- `logging.consoleColor` (`auto` | `always` | `never`)
|
||||
|
||||
```json5
|
||||
{
|
||||
@@ -57,8 +56,7 @@ If set, CLAWDIS derives defaults (only when you haven’t set them explicitly):
|
||||
level: "info",
|
||||
file: "/tmp/clawdis/clawdis.log",
|
||||
consoleLevel: "info",
|
||||
consoleStyle: "pretty",
|
||||
consoleColor: "auto"
|
||||
consoleStyle: "pretty"
|
||||
}
|
||||
}
|
||||
```
|
||||
|
||||
@@ -75,7 +75,7 @@ Behavior:
|
||||
|
||||
- **Subsystem prefixes** on every line (e.g. `[gateway]`, `[canvas]`, `[tailscale]`)
|
||||
- **Subsystem colors** (stable per subsystem) plus level coloring
|
||||
- **Color modes** (`logging.consoleColor`: `auto`/`always`/`never`; `auto` honors `NO_COLOR` and TTY)
|
||||
- **Color only when TTY** (`process.stdout.isTTY`/`process.stderr.isTTY`), respects `NO_COLOR`
|
||||
- **Sub-loggers by subsystem** (auto prefix + structured field `{ subsystem }`)
|
||||
- **`logRaw()`** for QR/UX output (no prefix, no formatting)
|
||||
- **Console styles** (e.g. `pretty | compact | json`)
|
||||
|
||||
@@ -29,7 +29,6 @@ export type LoggingConfig = {
|
||||
| "debug"
|
||||
| "trace";
|
||||
consoleStyle?: "pretty" | "compact" | "json";
|
||||
consoleColor?: "auto" | "always" | "never";
|
||||
};
|
||||
|
||||
export type WebReconnectConfig = {
|
||||
@@ -270,9 +269,6 @@ const ClawdisSchema = z.object({
|
||||
consoleStyle: z
|
||||
.union([z.literal("pretty"), z.literal("compact"), z.literal("json")])
|
||||
.optional(),
|
||||
consoleColor: z
|
||||
.union([z.literal("auto"), z.literal("always"), z.literal("never")])
|
||||
.optional(),
|
||||
})
|
||||
.optional(),
|
||||
browser: z
|
||||
|
||||
@@ -34,7 +34,6 @@ export type LoggerSettings = {
|
||||
file?: string;
|
||||
consoleLevel?: Level;
|
||||
consoleStyle?: ConsoleStyle;
|
||||
consoleColor?: ConsoleColor;
|
||||
};
|
||||
|
||||
type LogObj = { date?: Date } & Record<string, unknown>;
|
||||
@@ -46,11 +45,9 @@ type ResolvedSettings = {
|
||||
export type LoggerResolvedSettings = ResolvedSettings;
|
||||
|
||||
export type ConsoleStyle = "pretty" | "compact" | "json";
|
||||
export type ConsoleColor = "auto" | "always" | "never";
|
||||
type ConsoleSettings = {
|
||||
level: Level;
|
||||
style: ConsoleStyle;
|
||||
color: ConsoleColor;
|
||||
};
|
||||
export type ConsoleLoggerSettings = ConsoleSettings;
|
||||
|
||||
@@ -90,8 +87,7 @@ function resolveConsoleSettings(): ConsoleSettings {
|
||||
overrideSettings ?? loadConfig().logging;
|
||||
const level = normalizeConsoleLevel(cfg?.consoleLevel);
|
||||
const style = normalizeConsoleStyle(cfg?.consoleStyle);
|
||||
const color = normalizeConsoleColor(cfg?.consoleColor);
|
||||
return { level, style, color };
|
||||
return { level, style };
|
||||
}
|
||||
|
||||
function settingsChanged(a: ResolvedSettings | null, b: ResolvedSettings) {
|
||||
@@ -104,7 +100,7 @@ function consoleSettingsChanged(
|
||||
b: ConsoleSettings,
|
||||
) {
|
||||
if (!a) return true;
|
||||
return a.level !== b.level || a.style !== b.style || a.color !== b.color;
|
||||
return a.level !== b.level || a.style !== b.style;
|
||||
}
|
||||
|
||||
function levelToMinLevel(level: Level): number {
|
||||
@@ -137,13 +133,6 @@ function normalizeConsoleStyle(style?: string): ConsoleStyle {
|
||||
return "pretty";
|
||||
}
|
||||
|
||||
function normalizeConsoleColor(color?: string): ConsoleColor {
|
||||
if (color === "auto" || color === "always" || color === "never") {
|
||||
return color;
|
||||
}
|
||||
return "auto";
|
||||
}
|
||||
|
||||
function buildLogger(settings: ResolvedSettings): TsLogger<LogObj> {
|
||||
fs.mkdirSync(path.dirname(settings.file), { recursive: true });
|
||||
// Clean up stale rolling logs when using a dated log filename.
|
||||
@@ -350,14 +339,10 @@ function shouldLogToConsole(level: Level, settings: ConsoleSettings): boolean {
|
||||
return current <= min;
|
||||
}
|
||||
|
||||
function getColorForConsole(mode: ConsoleColor): Chalk {
|
||||
if (mode === "never") return new Chalk({ level: 0 });
|
||||
if (mode === "always") return new Chalk({ level: 1 });
|
||||
const supports =
|
||||
process.stdout.isTTY &&
|
||||
!process.env.NO_COLOR &&
|
||||
Boolean(chalk.supportsColor);
|
||||
return supports ? chalk : new Chalk({ level: 0 });
|
||||
function getColorForConsole(): Chalk {
|
||||
if (process.env.NO_COLOR) return new Chalk({ level: 0 });
|
||||
const hasTty = Boolean(process.stdout.isTTY || process.stderr.isTTY);
|
||||
return hasTty ? new Chalk({ level: 1 }) : new Chalk({ level: 0 });
|
||||
}
|
||||
|
||||
const SUBSYSTEM_COLORS = [
|
||||
@@ -384,7 +369,6 @@ function formatConsoleLine(opts: {
|
||||
subsystem: string;
|
||||
message: string;
|
||||
style: ConsoleStyle;
|
||||
colorMode: ConsoleColor;
|
||||
meta?: Record<string, unknown>;
|
||||
}): string {
|
||||
if (opts.style === "json") {
|
||||
@@ -396,7 +380,7 @@ function formatConsoleLine(opts: {
|
||||
...opts.meta,
|
||||
});
|
||||
}
|
||||
const color = getColorForConsole(opts.colorMode);
|
||||
const color = getColorForConsole();
|
||||
const prefix = `[${opts.subsystem}]`;
|
||||
const prefixColor = pickSubsystemColor(color, opts.subsystem);
|
||||
const levelColor =
|
||||
@@ -455,7 +439,6 @@ export function createSubsystemLogger(subsystem: string): SubsystemLogger {
|
||||
subsystem,
|
||||
message,
|
||||
style: consoleSettings.style,
|
||||
colorMode: consoleSettings.color,
|
||||
meta,
|
||||
});
|
||||
writeConsoleLine(level, line);
|
||||
|
||||
Reference in New Issue
Block a user