|
@@ -3,7 +3,6 @@ import fs from "fs/promises"
|
|
|
import { createWriteStream } from "fs"
|
|
import { createWriteStream } from "fs"
|
|
|
import { Global } from "../global"
|
|
import { Global } from "../global"
|
|
|
import z from "zod"
|
|
import z from "zod"
|
|
|
-import { Glob } from "./glob"
|
|
|
|
|
|
|
|
|
|
export namespace Log {
|
|
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" })
|
|
@@ -15,6 +14,8 @@ export namespace Log {
|
|
|
WARN: 2,
|
|
WARN: 2,
|
|
|
ERROR: 3,
|
|
ERROR: 3,
|
|
|
}
|
|
}
|
|
|
|
|
+ const keep = 10
|
|
|
|
|
+ const rx = /^\d{4}-\d{2}-\d{2}T\d{6}\.log$/
|
|
|
|
|
|
|
|
let level: Level = "INFO"
|
|
let level: Level = "INFO"
|
|
|
|
|
|
|
@@ -78,15 +79,11 @@ export namespace Log {
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
async function cleanup(dir: string) {
|
|
async function cleanup(dir: string) {
|
|
|
- const files = await Glob.scan("????-??-??T??????.log", {
|
|
|
|
|
- cwd: dir,
|
|
|
|
|
- absolute: true,
|
|
|
|
|
- include: "file",
|
|
|
|
|
- })
|
|
|
|
|
- if (files.length <= 5) return
|
|
|
|
|
-
|
|
|
|
|
- const filesToDelete = files.slice(0, -10)
|
|
|
|
|
- await Promise.all(filesToDelete.map((file) => fs.unlink(file).catch(() => {})))
|
|
|
|
|
|
|
+ const files = (await fs.readdir(dir).catch(() => [])).filter((file) => rx.test(file)).sort()
|
|
|
|
|
+ if (files.length <= keep) return
|
|
|
|
|
+
|
|
|
|
|
+ const doomed = files.slice(0, -keep)
|
|
|
|
|
+ await Promise.all(doomed.map((file) => fs.unlink(path.join(dir, file)).catch(() => {})))
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
function formatError(error: Error, depth = 0): string {
|
|
function formatError(error: Error, depth = 0): string {
|