From 34e6f86b44747d5db4e7314e208d008395706c34 Mon Sep 17 00:00:00 2001 From: crackheadakira Date: Fri, 11 Sep 2026 06:20:15 +0200 Subject: [PATCH] fix: rather than flattening the meta, store within a meta field so Elysia Typebox is happy --- src/api/index.ts | 70 ++++++++++++++++++---------------- src/caching/redis.ts | 2 +- src/checks/run.ts | 2 +- src/utils/logger.ts | 91 ++++++++++++++++++++++++++++---------------- 4 files changed, 98 insertions(+), 67 deletions(-) diff --git a/src/api/index.ts b/src/api/index.ts index 858368f..9e9924d 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -7,42 +7,46 @@ import { env } from "#env"; import { logger } from "#src/utils/logger"; export async function startApi() { - const port = env.API_PORT; + const port = env.API_PORT; - await RSA.loadKeys(); + await RSA.loadKeys(); - new Elysia().onError(({ error }) => { - const err = new Response(error.toString()); - logger.error(error.toString()); + new Elysia().onError(({ error }) => { + const err = new Response(error.toString()); + logger.error(error.toString()); - return err; - }).use(cors({ - origin: /.*\.yuuko\.dev$/, - methods: ["POST", "GET"] - })).use(openapi({ - documentation: { - info: { - title: "Yuuko API", - version: "1.0.0", - description: "API documentation for Yuuko services", - }, - tags: [ - { name: "Public", description: "Unauthenticated public endpoints" }, - { name: "Protected / Info", description: "Internal status and management endpoints" } - ], - components: { - securitySchemes: { - "BearerAuth": { - type: "apiKey", - in: "header", - name: "authorization", - description: "Trusted User Token" - } + return err; + }).use(cors({ + origin: [ + /.*\.yuuko\.dev$/, + /^http:\/\/localhost(:\d+)?$/, + /^http:\/\/127\.0\.0\.1(:\d+)?$/ + ], + methods: ["POST", "GET"] + })).use(openapi({ + documentation: { + info: { + title: "Yuuko API", + version: "1.0.0", + description: "API documentation for Yuuko services", + }, + tags: [ + { name: "Public", description: "Unauthenticated public endpoints" }, + { name: "Protected / Info", description: "Internal status and management endpoints" } + ], + components: { + securitySchemes: { + "BearerAuth": { + type: "apiKey", + in: "header", + name: "authorization", + description: "Trusted User Token" + } + } + } } - } - } - })).use(api).listen(port) + })).use(api).listen(port) - logger.info(`API is open on port ${port}`); - logger.info(`OpenAPI UI available at http://localhost:${port}/openapi`); + logger.info(`API is open on port ${port}`, { type: "startup" }); + logger.info(`OpenAPI UI available at http://localhost:${port}/openapi`, { type: "startup" }); } diff --git a/src/caching/redis.ts b/src/caching/redis.ts index 875df61..9a9ba8e 100644 --- a/src/caching/redis.ts +++ b/src/caching/redis.ts @@ -17,7 +17,7 @@ redis.on("error", (err) => { }); redis.on("connect", handleAsync(async () => { - logger.info(`Connected to ${host}!`); + logger.info(`Connected to ${host}!`, { type: "startup" }); await redis.set("test", "test"); })); diff --git a/src/checks/run.ts b/src/checks/run.ts index d30d8c1..99770c7 100644 --- a/src/checks/run.ts +++ b/src/checks/run.ts @@ -43,7 +43,7 @@ export async function runChecks() { } } - logger.info("Checks passed!"); + logger.info("Checks passed!", { type: "startup" }); } function serializeError(e: unknown): string { diff --git a/src/utils/logger.ts b/src/utils/logger.ts index 03fdb00..a2e3906 100644 --- a/src/utils/logger.ts +++ b/src/utils/logger.ts @@ -89,6 +89,21 @@ export const StartupMetaSchema = t.Object( { title: "Startup Meta" } ); +export const ErrorMetaSchema = t.Intersect( + [ + t.Object( + { + type: t.Literal("error", { title: "Error Type" }), + errorName: t.Optional(t.String()), + stack: t.Optional(t.String()), + errors: t.Optional(t.Array(t.String())), + }, + { title: "Error Meta" } + ), + t.Record(t.String(), t.Unknown()), + ] +); + export const LogMetaSchema = t.Union([ GenericMetaSchema, CommandMetaSchema, @@ -97,38 +112,43 @@ export const LogMetaSchema = t.Union([ GraphQLMetaSchema, CheckMetaSchema, StartupMetaSchema, + ErrorMetaSchema, ]); -export const LogEntrySchema = t.Intersect([ - t.Object({ - timestamp: t.Optional(t.String({ description: "ISO Timestamp generated by Winston" })), - level: t.Union([ - t.Literal("error", { title: "Error" }), - t.Literal("warn", { title: "Warn" }), - t.Literal("info", { title: "Info" }), - t.Literal("http", { title: "HTTP" }), - t.Literal("verbose", { title: "Verbose" }), - t.Literal("debug", { title: "Debug" }), - t.Literal("silly", { title: "Silly" }), - ]), - message: t.String(), - }), - t.Partial(LogMetaSchema), -]); - - -export type CommandMeta = Static; -export type GenericMeta = Static; -export type CommandDebugMeta = Static; -export type EventMeta = Static; -export type GraphQLMeta = Static; -export type CheckMeta = Static; -export type StartupMeta = Static; +export const LogEntrySchema = t.Object({ + timestamp: t.Optional(t.String({ description: "ISO Timestamp generated by Winston" })), + level: t.Union([ + t.Literal("error", { title: "Error" }), + t.Literal("warn", { title: "Warn" }), + t.Literal("info", { title: "Info" }), + t.Literal("http", { title: "HTTP" }), + t.Literal("verbose", { title: "Verbose" }), + t.Literal("debug", { title: "Debug" }), + t.Literal("silly", { title: "Silly" }), + ]), + message: t.String(), + meta: t.Optional(LogMetaSchema), +}); export type LogMeta = Static; - export type LogEntry = Static; +const nestMetaFormat = winston.format((info) => { + const { timestamp, level, message, meta, ...rest } = info; + const mergedMeta = meta ? { ...meta, ...rest } : rest; + + if (Object.keys(mergedMeta).length > 0) { + return { + timestamp, + level, + message, + meta: mergedMeta, + }; + } + + return { timestamp, level, message }; +}); + class Logger { public logger: winston.Logger; @@ -138,7 +158,11 @@ class Logger { transports: [ new winston.transports.File({ filename, - format: winston.format.combine(winston.format.timestamp(), winston.format.json()), + format: winston.format.combine( + winston.format.timestamp(), + nestMetaFormat(), + winston.format.json() + ), }), ], }); @@ -149,14 +173,15 @@ class Logger { format: winston.format.combine( winston.format.colorize(), winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }), - winston.format.printf(({ timestamp, level, message, ...meta }) => { - const metaStr = Object.keys(meta).length ? JSON.stringify(meta) : ""; + winston.format.printf(({ timestamp, level, message, meta, ...rest }) => { + const combinedMeta = meta ?? rest; + const metaStr = Object.keys(combinedMeta).length ? JSON.stringify(combinedMeta) : ""; const timeStr = String(timestamp); const levelStr = String(level); const msgStr = String(message); - return `${timeStr} | [${levelStr}]: ${msgStr} ${metaStr}`; + return `${timeStr} | [${levelStr}]: ${msgStr} ${metaStr}`.trim(); }), ), }), @@ -174,9 +199,10 @@ class Logger { error(message: string | Error | unknown, meta?: LogMeta) { if (message instanceof Error) { const errMeta: Record = { - ...meta, + type: "error", errorName: message.name, stack: message.stack, + ...meta, }; if (message instanceof AggregateError) { @@ -188,7 +214,8 @@ class Logger { } const logMessage = typeof message === "string" ? message : String(message); - this.logger.error(logMessage, meta); + const finalMeta = meta ? { ...meta, type: "error" } : { type: "error" }; + this.logger.error(logMessage, finalMeta); } debug(message: string, meta?: LogMeta) {