diff --git a/server/src/app.ts b/server/src/app.ts index 4c4369d..9b3b2dd 100644 --- a/server/src/app.ts +++ b/server/src/app.ts @@ -1,14 +1,12 @@ import cors from "@elysiajs/cors"; import swagger from "@elysiajs/swagger"; +import { logger } from "@tqman/nice-logger"; import { Elysia } from "elysia"; import { api } from "./api"; -import { isProd } from "./env"; -import { logger } from "./lib/logger"; -import { pluginConditionally } from "./utils/elysia"; export const app = new Elysia() .use(cors()) .use(swagger()) - .use(pluginConditionally(!isProd, logger)) + .use(logger()) .use(api); diff --git a/server/src/lib/logger/formatters.ts b/server/src/lib/logger/formatters.ts deleted file mode 100644 index 6233d28..0000000 --- a/server/src/lib/logger/formatters.ts +++ /dev/null @@ -1,83 +0,0 @@ -import pc from "picocolors"; - -export function duration(hrtime: [number, number]): string { - const [seconds, nanoseconds] = process.hrtime(hrtime); - const durationInMicroseconds = (seconds * 1e9 + nanoseconds) / 1e3; - const durationInMilliseconds = (seconds * 1e9 + nanoseconds) / 1e6; - - if (seconds > 0) { - return `${seconds.toPrecision(2)}s`; - } - if (durationInMilliseconds > 1) { - return `${durationInMilliseconds.toPrecision(2)}ms`; - } - if (durationInMicroseconds > 1) { - return `${durationInMicroseconds.toPrecision(4)}µs`; - } - if (nanoseconds > 0) { - return `${nanoseconds.toPrecision(4)}ns`; - } - - return "-/-"; -} - -export function method(method: string): string { - switch (method) { - case "GET": - return pc.green("GET"); - - case "POST": - return pc.blue("POST"); - - case "PUT": - return pc.yellow("PUT"); - - case "DELETE": - return pc.red("DELETE"); - - case "PATCH": - return pc.magenta("PATCH"); - - case "OPTIONS": - return pc.cyan("OPTIONS"); - - case "HEAD": - return pc.gray("HEAD"); - - default: - return method; - } -} - -export function status( - status: number | string | undefined, -): string | undefined { - switch (status) { - case 200: - return pc.green(status); - - case 201: - return pc.blue(status); - - case 204: - return pc.yellow(status); - - case 400: - return pc.red(status); - - case 401: - return pc.magenta(status); - - case 403: - return pc.cyan(status); - - case 404: - return pc.gray(status); - - case 500: - return pc.gray(status); - - default: - return String(status); - } -} diff --git a/server/src/lib/logger/index.ts b/server/src/lib/logger/index.ts deleted file mode 100644 index 830b70c..0000000 --- a/server/src/lib/logger/index.ts +++ /dev/null @@ -1,41 +0,0 @@ -import Elysia from "elysia"; -import pc from "picocolors"; - -import * as fmt from "./formatters"; - -export const logger = new Elysia({ - name: "@todos/server/logger", -}) - .state("__requestStartTime", [Number.NaN, Number.NaN] as [number, number]) - .onRequest(({ store }) => { - store.__requestStartTime = process.hrtime(); - }) - .onResponse({ as: "global" }, ({ request, set, store }) => { - const url = new URL(request.url); - const duration = store.__requestStartTime; - - const components = [ - pc.green("✓"), - pc.bold(fmt.method(request.method)), - url.pathname, - fmt.status(set.status), - pc.dim(`[${fmt.duration(duration)}]`), - ]; - - console.log(components.join(" ")); - }) - .onError({ as: "global" }, ({ request, error, store }) => { - const url = new URL(request.url); - const duration = store.__requestStartTime; - const status = "status" in error ? error.status : "-/-"; - - const components = [ - pc.red("✗"), - pc.bold(fmt.method(request.method)), - url.pathname, - fmt.status(status), - pc.dim(`[${fmt.duration(duration)}]`), - ]; - - console.log(components.join(" ")); - }); diff --git a/server/src/utils/elysia.ts b/server/src/utils/elysia.ts deleted file mode 100644 index 2885c53..0000000 --- a/server/src/utils/elysia.ts +++ /dev/null @@ -1,8 +0,0 @@ -import type Elysia from "elysia"; - -export function pluginConditionally( - condition: boolean, - plugin: Plugin, -) { - return (app: Elysia) => (condition ? plugin : app); -}