Rework logger, improve plaintext caching

This commit is contained in:
Jon Staab
2026-06-20 10:12:39 -07:00
parent ed17dcc412
commit d2b57c559d
10 changed files with 124 additions and 137 deletions
+29
View File
@@ -0,0 +1,29 @@
import {writable} from "svelte/store"
import {randomId} from "@welshman/lib"
import {projection} from "./base.js"
import type {IApp} from "../app.js"
export type LogMessage = {
source: string
id: string
at: number
[key: string]: unknown
}
/**
* A logger which stores messages durably for inspection. Subscribe to `messages`
* (a projection) to read the log; append with `log(source, {...})`.
*/
export class Logger {
protected store = writable<LogMessage[]>([])
messages = projection(this.store)
constructor(protected readonly app: IApp) {}
log(
source: string,
{id = randomId(), at = Date.now(), ...message}: {id?: string; at?: number; [key: string]: unknown},
) {
this.store.update($messages => $messages.concat({source, id, at, ...message}).slice(-1000))
}
}