feat: add structured backend logging
Collector failures, service-API errors, WS connection lifecycle, and config reload/discovery events were previously invisible outside the browser (or, for a few config/discovery cases, logged with an inconsistent ad-hoc console.error). Add a small scoped logger (lib/logger.ts, level via LOG_LEVEL) and wire it through server.ts, the scheduler, service collector, config loader/effective store, and the WS server so operators can see failures via `docker logs`. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
+15
-2
@@ -5,11 +5,18 @@ import chokidar, { type FSWatcher } from "chokidar";
|
||||
import { parse as parseYaml } from "yaml";
|
||||
import { configSchema, type Config } from "./schema";
|
||||
import { interpolateEnv } from "./interpolate";
|
||||
import { createLogger } from "@/lib/logger";
|
||||
|
||||
const log = createLogger("config");
|
||||
|
||||
const CONFIG_DIR = path.join(process.cwd(), "config");
|
||||
const CONFIG_PATH = path.join(CONFIG_DIR, "config.yml");
|
||||
const ENV_PATH = path.join(CONFIG_DIR, ".env");
|
||||
|
||||
function widgetCount(config: Config): number {
|
||||
return config.groups.reduce((sum, group) => sum + group.widgets.length, 0);
|
||||
}
|
||||
|
||||
export class ConfigError extends Error {}
|
||||
|
||||
function loadEnv(): Record<string, string | undefined> {
|
||||
@@ -62,6 +69,7 @@ class ConfigStore {
|
||||
|
||||
load(): Config {
|
||||
this.current = readConfig();
|
||||
log.info("config loaded", { groups: this.current.groups.length, widgets: widgetCount(this.current) });
|
||||
return this.current;
|
||||
}
|
||||
|
||||
@@ -85,7 +93,11 @@ class ConfigStore {
|
||||
awaitWriteFinish: { stabilityThreshold: 200, pollInterval: 50 },
|
||||
ignoreInitial: true,
|
||||
});
|
||||
this.watcher.on("all", () => this.reload());
|
||||
this.watcher.on("all", (event) => {
|
||||
log.debug("watched file changed", { event });
|
||||
this.reload();
|
||||
});
|
||||
log.info("watching config files", { paths: [CONFIG_PATH, ENV_PATH] });
|
||||
}
|
||||
|
||||
stop(): void {
|
||||
@@ -97,10 +109,11 @@ class ConfigStore {
|
||||
try {
|
||||
const next = readConfig();
|
||||
this.current = next;
|
||||
log.info("config reloaded", { groups: next.groups.length, widgets: widgetCount(next) });
|
||||
for (const listener of this.listeners) listener(next);
|
||||
} catch (err) {
|
||||
const configError = err instanceof ConfigError ? err : new ConfigError((err as Error).message);
|
||||
console.error(`[config] ${configError.message}`);
|
||||
log.error("config reload failed", { error: configError.message });
|
||||
for (const listener of this.errorListeners) listener(configError);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user