feat: logger improvement

This commit is contained in:
2026-03-21 01:09:22 +07:00 Unverified
parent e035746a0f
commit 6d96f6b0cc
4 changed files with 45 additions and 34 deletions
+4 -3
View File
@@ -2,8 +2,11 @@ import openapi from "@elysiajs/openapi";
import { Elysia, t } from "elysia";
import { version } from "../package.json";
import { pm } from "./lib/auth";
import { createLogger } from "./lib/logger";
import { v1routes } from "./v1";
const log = createLogger("APP");
const app = new Elysia()
.use(
openapi({
@@ -39,6 +42,4 @@ v1routes(app);
export type App = typeof app;
console.log(
`🦊 Elysia is running at ${app.server?.hostname}:${app.server?.port}`,
);
log`Serving ${app.server?.hostname}:${app.server?.port}`;
+7 -9
View File
@@ -5,8 +5,10 @@ import { file, hash, write } from "bun";
import { TTLCache } from "enka.ts";
import { AvatarStore, EnkaLocs, RelicStore, WeaponStore } from "@/type/enka";
import { Deduplicator } from "./dedupe";
import { createLogger } from "./logger";
export const cache = new TTLCache();
const log = createLogger("ASSET");
export class AssetManager {
private cache = new Map<string, ArrayBuffer>();
@@ -40,9 +42,7 @@ export class AssetManager {
(n) => existing.has(hash(n).toString(36)) || missing.add(n),
);
console.log(
`${missing.size} assets missing, ${existing.size} assets in store`,
);
log`${missing.size} assets missing, ${existing.size} assets in store`;
for (const name of missing)
await fetch(new URL(name, "https://enka.network"))
@@ -57,18 +57,16 @@ export class AssetManager {
})
.then((d) => write(join("store", hash(name).toString(36)), d));
console.log("All missing assets fetched and stored");
log`All missing assets fetched and stored`;
}
async purgeExtraAssets() {
const extra = new Set(await readdir("store"));
(await this.getAssetList()).map((n) => extra.delete(hash(n).toString(36)));
if (!extra.size) return;
console.log(
`Clearing extra assets\n${[...extra.values()]
.map((e) => `- ${e}`)
.join("\n")}`,
);
log`Clearing extra assets\n${[...extra.values()]
.map((e) => `- ${e}`)
.join("\n")}`;
// biome-ignore lint/suspicious/useIterableCallbackReturn: there's no map
extra.forEach((f) => rm(join("store", f)));
}
+31 -22
View File
@@ -2,42 +2,51 @@ import { feature } from "bun:bundle";
import { color, inspect } from "bun";
const colors = [
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"lime",
"orange",
"violet",
"skyblue",
"pink",
"gold",
"deepskyblue",
"mediumspringgreen",
"pink",
"lightgreen",
"lightyellow",
"lightblue",
"lightpink",
"lightcyan",
"peachpuff",
"violet",
"lightskyblue",
"lightpink",
"yellow",
"springgreen",
];
var i = 0;
if (!feature("API"))
console.log(
colors.map((c) => `${color(c, "ansi")}${c.slice(0, 1)}`).join("") +
color("lightgray", "ansi"),
colors
.map((c, i) => `${color(c, "ansi")}${(i + 10).toString(36)}`)
.join("") + color("lightgray", "ansi"),
);
export function createLogger() {
export function createLogger(label?: string) {
const start = Date.now();
const col = color(colors[i], "ansi");
const reset = color("lightgray", "ansi");
i = (i + 1) % colors.length;
return function log(strings: TemplateStringsArray, ...values: unknown[]) {
const normalized = `${col}${Date.now() - start}ms - ${strings.reduce(
(out, str, i) =>
out +
str +
(typeof values[i] === "string" ? values[i] : inspect(values[i] ?? "")),
"",
)}${reset}\n`;
const normalized = `${col}${(label ?? `${Date.now() - start}ms`).padStart(7)} | ${strings
.reduce(
(out, str, i) =>
out +
str +
(typeof values[i] === "string"
? values[i]
: typeof values[i] !== "undefined"
? inspect(values[i])
: ""),
"",
)
.split("\n")
.map((l, i) => (i ? `${" ".repeat(7)} | ${l}` : l))
.join("\n")}${reset}\n`;
console.write(normalized);
};
}