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
+3
View File
@@ -6,6 +6,9 @@ services:
ports: ports:
- 8809:3000 - 8809:3000
tty: true
stdin_open: true
# volumes: # volumes:
# - ./data:/app/data # - ./data:/app/data
env_file: .env env_file: .env
+4 -3
View File
@@ -2,8 +2,11 @@ import openapi from "@elysiajs/openapi";
import { Elysia, t } from "elysia"; import { Elysia, t } from "elysia";
import { version } from "../package.json"; import { version } from "../package.json";
import { pm } from "./lib/auth"; import { pm } from "./lib/auth";
import { createLogger } from "./lib/logger";
import { v1routes } from "./v1"; import { v1routes } from "./v1";
const log = createLogger("APP");
const app = new Elysia() const app = new Elysia()
.use( .use(
openapi({ openapi({
@@ -39,6 +42,4 @@ v1routes(app);
export type App = typeof app; export type App = typeof app;
console.log( log`Serving ${app.server?.hostname}:${app.server?.port}`;
`🦊 Elysia is running at ${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 { TTLCache } from "enka.ts";
import { AvatarStore, EnkaLocs, RelicStore, WeaponStore } from "@/type/enka"; import { AvatarStore, EnkaLocs, RelicStore, WeaponStore } from "@/type/enka";
import { Deduplicator } from "./dedupe"; import { Deduplicator } from "./dedupe";
import { createLogger } from "./logger";
export const cache = new TTLCache(); export const cache = new TTLCache();
const log = createLogger("ASSET");
export class AssetManager { export class AssetManager {
private cache = new Map<string, ArrayBuffer>(); private cache = new Map<string, ArrayBuffer>();
@@ -40,9 +42,7 @@ export class AssetManager {
(n) => existing.has(hash(n).toString(36)) || missing.add(n), (n) => existing.has(hash(n).toString(36)) || missing.add(n),
); );
console.log( log`${missing.size} assets missing, ${existing.size} assets in store`;
`${missing.size} assets missing, ${existing.size} assets in store`,
);
for (const name of missing) for (const name of missing)
await fetch(new URL(name, "https://enka.network")) 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)); .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() { async purgeExtraAssets() {
const extra = new Set(await readdir("store")); const extra = new Set(await readdir("store"));
(await this.getAssetList()).map((n) => extra.delete(hash(n).toString(36))); (await this.getAssetList()).map((n) => extra.delete(hash(n).toString(36)));
if (!extra.size) return; if (!extra.size) return;
console.log( log`Clearing extra assets\n${[...extra.values()]
`Clearing extra assets\n${[...extra.values()] .map((e) => `- ${e}`)
.map((e) => `- ${e}`) .join("\n")}`;
.join("\n")}`,
);
// biome-ignore lint/suspicious/useIterableCallbackReturn: there's no map // biome-ignore lint/suspicious/useIterableCallbackReturn: there's no map
extra.forEach((f) => rm(join("store", f))); 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"; import { color, inspect } from "bun";
const colors = [ const colors = [
"red",
"green",
"yellow",
"blue",
"magenta",
"cyan",
"lime",
"orange",
"violet",
"skyblue",
"pink",
"gold",
"deepskyblue", "deepskyblue",
"mediumspringgreen",
"pink",
"lightgreen",
"lightyellow",
"lightblue",
"lightpink",
"lightcyan",
"peachpuff",
"violet",
"lightskyblue",
"lightpink",
"yellow",
"springgreen", "springgreen",
]; ];
var i = 0; var i = 0;
if (!feature("API")) if (!feature("API"))
console.log( console.log(
colors.map((c) => `${color(c, "ansi")}${c.slice(0, 1)}`).join("") + colors
color("lightgray", "ansi"), .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 start = Date.now();
const col = color(colors[i], "ansi"); const col = color(colors[i], "ansi");
const reset = color("lightgray", "ansi"); const reset = color("lightgray", "ansi");
i = (i + 1) % colors.length; i = (i + 1) % colors.length;
return function log(strings: TemplateStringsArray, ...values: unknown[]) { return function log(strings: TemplateStringsArray, ...values: unknown[]) {
const normalized = `${col}${Date.now() - start}ms - ${strings.reduce( const normalized = `${col}${(label ?? `${Date.now() - start}ms`).padStart(7)} | ${strings
(out, str, i) => .reduce(
out + (out, str, i) =>
str + out +
(typeof values[i] === "string" ? values[i] : inspect(values[i] ?? "")), str +
"", (typeof values[i] === "string"
)}${reset}\n`; ? 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); console.write(normalized);
}; };
} }