Skip to content

armgit / alook

Public Imported from GitHub
0

Multi-Agent Collaboration for Team and Friends

refactor(cache): replace console.warn with structured JSON logging

Gener9d835c1db6e1
diff --git a/src/web/src/lib/cache.test.ts b/src/web/src/lib/cache.test.ts
index fec8c1558..3f5f39e42 100644
--- a/src/web/src/lib/cache.test.ts
+++ b/src/web/src/lib/cache.test.ts
@@ -49,34 +49,31 @@ describe("cache", () => {
it("falls back to fn when KV read throws", async () => {
(mockKV.get as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error("KV down"));
const fn = vi.fn(async () => ({ data: "fallback" }));
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const result = await cached("error-key", 300, fn);
expect(result).toEqual({ data: "fallback" });
expect(fn).toHaveBeenCalledOnce();
- expect(warnSpy).toHaveBeenCalledWith(
- expect.stringContaining("[cache] KV read failed for error-key"),
- expect.any(Error),
+ expect(logSpy).toHaveBeenCalledWith(
+ expect.stringContaining('"msg":"KV read failed"'),
);
- warnSpy.mockRestore();
+ logSpy.mockRestore();
});
it("still returns value when KV write throws", async () => {
(mockKV.put as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error("KV write error"));
const fn = vi.fn(async () => ({ data: "ok" }));
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
const result = await cached("write-fail-key", 300, fn);
expect(result).toEqual({ data: "ok" });
- // Wait for async catch handler
await new Promise((r) => setTimeout(r, 10));
- expect(warnSpy).toHaveBeenCalledWith(
- expect.stringContaining("[cache] KV write failed for write-fail-key"),
- expect.any(Error),
+ expect(logSpy).toHaveBeenCalledWith(
+ expect.stringContaining('"msg":"KV write failed"'),
);
- warnSpy.mockRestore();
+ logSpy.mockRestore();
});
it("clamps TTL to minimum 60s for KV write", async () => {
@@ -175,16 +172,15 @@ describe("cache", () => {
it("logs warning on KV delete failure", async () => {
(mockKV.delete as ReturnType<typeof vi.fn>).mockRejectedValueOnce(new Error("fail"));
- const warnSpy = vi.spyOn(console, "warn").mockImplementation(() => {});
+ const logSpy = vi.spyOn(console, "log").mockImplementation(() => {});
await invalidate("fail-key");
await new Promise((r) => setTimeout(r, 10));
- expect(warnSpy).toHaveBeenCalledWith(
- expect.stringContaining("[cache] KV delete failed for fail-key"),
- expect.any(Error),
+ expect(logSpy).toHaveBeenCalledWith(
+ expect.stringContaining('"msg":"KV invalidate failed"'),
);
- warnSpy.mockRestore();
+ logSpy.mockRestore();
});
it("does nothing when KV is not bound", async () => {
diff --git a/src/web/src/lib/cache.ts b/src/web/src/lib/cache.ts
index 6006d5048..1efc23f80 100644
--- a/src/web/src/lib/cache.ts
+++ b/src/web/src/lib/cache.ts
@@ -1,3 +1,9 @@
+const log = {
+ warn(msg: string, ctx: Record<string, unknown>) {
+ console.log(JSON.stringify({ level: "warn", service: "cache", msg, ...ctx, ts: new Date().toISOString() }));
+ },
+};
+
let _kv: KVNamespace | null | undefined;
export function bindCacheKV(kv: KVNamespace | null) {
@@ -21,7 +27,7 @@ export async function cached<T>(
const raw = await kv.get(key);
if (raw) return JSON.parse(raw) as T;
} catch (err) {
- console.warn(`[cache] KV read failed for ${key}:`, err);
+ log.warn("KV read failed", { key, err });
}
}
@@ -29,7 +35,7 @@ export async function cached<T>(
if (value != null && kv) {
kv.put(key, JSON.stringify(value), { expirationTtl: Math.max(ttlSeconds, MIN_KV_TTL) }).catch((err) => {
- console.warn(`[cache] KV write failed for ${key}:`, err);
+ log.warn("KV write failed", { key, err });
});
}
@@ -63,7 +69,7 @@ export async function cachedBatch<T>(
hits.push(key);
}
} catch (err) {
- console.warn(`[cache] KV batch read failed for ${key}:`, err);
+ log.warn("KV batch read failed", { key, err });
}
}),
);
@@ -76,7 +82,7 @@ export async function cachedBatch<T>(
result.set(key, value);
if (value != null && kv) {
kv.put(key, JSON.stringify(value), { expirationTtl: Math.max(ttlSeconds, MIN_KV_TTL) }).catch((err) => {
- console.warn(`[cache] KV batch write failed for ${key}:`, err);
+ log.warn("KV batch write failed", { key, err });
});
}
}
@@ -120,7 +126,7 @@ export async function throttled(
export async function invalidate(key: string): Promise<void> {
const kv = getKV();
if (kv) await kv.delete(key).catch((err) => {
- console.warn(`[cache] KV delete failed for ${key}:`, err);
+ log.warn("KV invalidate failed", { key, err });
});
}
@@ -128,7 +134,7 @@ export async function invalidateMany(keys: string[]): Promise<void> {
const kv = getKV();
if (!kv || keys.length === 0) return;
await Promise.all(keys.map((key) => kv.delete(key).catch((err) => {
- console.warn(`[cache] KV delete failed for ${key}:`, err);
+ log.warn("KV invalidate failed", { key, err });
})));
}