|
|
@@ -4,7 +4,9 @@ import { Global } from "../global"
|
|
|
import z from "zod"
|
|
|
|
|
|
export namespace Log {
|
|
|
- export const Level = z.enum(["DEBUG", "INFO", "WARN", "ERROR"]).meta({ ref: "LogLevel", description: "Log level" })
|
|
|
+ export const Level = z
|
|
|
+ .enum(["DEBUG", "INFO", "WARN", "ERROR"])
|
|
|
+ .meta({ ref: "LogLevel", description: "Log level" })
|
|
|
export type Level = z.infer<typeof Level>
|
|
|
|
|
|
const levelPriority: Record<Level, number> = {
|
|
|
@@ -50,6 +52,7 @@ export namespace Log {
|
|
|
export function file() {
|
|
|
return logpath
|
|
|
}
|
|
|
+ let write: (msg: string) => void
|
|
|
|
|
|
export async function init(options: Options) {
|
|
|
if (options.level) level = options.level
|
|
|
@@ -62,7 +65,7 @@ export namespace Log {
|
|
|
const logfile = Bun.file(logpath)
|
|
|
await fs.truncate(logpath).catch(() => {})
|
|
|
const writer = logfile.writer()
|
|
|
- process.stderr.write = (msg) => {
|
|
|
+ write = (msg) => {
|
|
|
writer.write(msg)
|
|
|
writer.flush()
|
|
|
return true
|
|
|
@@ -118,27 +121,31 @@ export namespace Log {
|
|
|
const next = new Date()
|
|
|
const diff = next.getTime() - last
|
|
|
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"
|
|
|
+ )
|
|
|
}
|
|
|
const result: Logger = {
|
|
|
debug(message?: any, extra?: Record<string, any>) {
|
|
|
if (shouldLog("DEBUG")) {
|
|
|
- process.stderr.write("DEBUG " + build(message, extra))
|
|
|
+ write("DEBUG " + build(message, extra))
|
|
|
}
|
|
|
},
|
|
|
info(message?: any, extra?: Record<string, any>) {
|
|
|
if (shouldLog("INFO")) {
|
|
|
- process.stderr.write("INFO " + build(message, extra))
|
|
|
+ write("INFO " + build(message, extra))
|
|
|
}
|
|
|
},
|
|
|
error(message?: any, extra?: Record<string, any>) {
|
|
|
if (shouldLog("ERROR")) {
|
|
|
- process.stderr.write("ERROR " + build(message, extra))
|
|
|
+ write("ERROR " + build(message, extra))
|
|
|
}
|
|
|
},
|
|
|
warn(message?: any, extra?: Record<string, any>) {
|
|
|
if (shouldLog("WARN")) {
|
|
|
- process.stderr.write("WARN " + build(message, extra))
|
|
|
+ write("WARN " + build(message, extra))
|
|
|
}
|
|
|
},
|
|
|
tag(key: string, value: string) {
|