This commit is contained in:
James Long 2026-05-17 21:48:06 -04:00
commit ef96fdfd83

View file

@ -54,6 +54,29 @@ export interface Options {
level?: Level level?: Level
} }
export interface Entry {
readonly time: string
readonly level: Level
readonly tags: Record<string, unknown>
readonly message: string
}
const MAX_ENTRIES = 5_000
const memory: Entry[] = []
function record(entry: Entry) {
memory.push(entry)
if (memory.length > MAX_ENTRIES) memory.splice(0, memory.length - MAX_ENTRIES)
}
export function entries(): Entry[] {
return memory.slice()
}
export function clearEntries() {
memory.length = 0
}
let logpath = "" let logpath = ""
export function file() { export function file() {
return logpath return logpath
@ -139,24 +162,39 @@ export function create(tags?: Record<string, any>) {
last = next.getTime() last = next.getTime()
return [next.toISOString().split(".")[0], "+" + diff + "ms", prefix, message].filter(Boolean).join(" ") + "\n" return [next.toISOString().split(".")[0], "+" + diff + "ms", prefix, message].filter(Boolean).join(" ") + "\n"
} }
function capture(level: Level, message: any, extra?: Record<string, any>) {
const text = message instanceof Error ? formatError(message) : message === undefined ? "" : String(message)
record({
time: new Date().toISOString(),
level,
tags: { ...tags, ...extra },
message: text,
})
}
const result: Logger = { const result: Logger = {
debug(message?: any, extra?: Record<string, any>) { debug(message?: any, extra?: Record<string, any>) {
if (shouldLog("DEBUG")) { if (shouldLog("DEBUG")) {
capture("DEBUG", message, extra)
write("DEBUG " + build(message, extra)) write("DEBUG " + build(message, extra))
} }
}, },
info(message?: any, extra?: Record<string, any>) { info(message?: any, extra?: Record<string, any>) {
if (shouldLog("INFO")) { if (shouldLog("INFO")) {
capture("INFO", message, extra)
write("INFO " + build(message, extra)) write("INFO " + build(message, extra))
} }
}, },
error(message?: any, extra?: Record<string, any>) { error(message?: any, extra?: Record<string, any>) {
if (shouldLog("ERROR")) { if (shouldLog("ERROR")) {
capture("ERROR", message, extra)
write("ERROR " + build(message, extra)) write("ERROR " + build(message, extra))
} }
}, },
warn(message?: any, extra?: Record<string, any>) { warn(message?: any, extra?: Record<string, any>) {
if (shouldLog("WARN")) { if (shouldLog("WARN")) {
capture("WARN", message, extra)
write("WARN " + build(message, extra)) write("WARN " + build(message, extra))
} }
}, },