diff --git a/config.ts b/config.ts index 33370f2..fc31995 100644 --- a/config.ts +++ b/config.ts @@ -1,3 +1,5 @@ +import "$std/log/mod.ts"; + export interface Config { postgres: { url: string; diff --git a/db/mod.ts b/db/mod.ts index f260166..9aa62b5 100644 --- a/db/mod.ts +++ b/db/mod.ts @@ -10,6 +10,7 @@ import { } from "https://deno.land/x/postgres@v0.16.1/query/query.ts?s=QueryArguments"; import { config } from "@/config.ts"; import * as base64 from "$std/encoding/base64.ts"; +import { log } from "@/log.ts"; import { type Identifiable, @@ -36,14 +37,14 @@ async function dbOp(op: (connection: PoolClient) => Promise) { try { result = await op(connection); } catch (err) { - console.error("Error querying database:", { ...err }); + log.error("Error querying database:", { ...err }); exception = err; } finally { connection.release(); } } catch (err) { exception = err; - console.error("Error connecting to database:", err); + log.error("Error connecting to database:", err); } if (exception != null) throw exception; return result; @@ -59,7 +60,7 @@ export async function queryObject( text: sql.trim(), args, }); - console.debug(result); + log.debug(result); return result; }); } @@ -88,7 +89,7 @@ export async function getNote( id: string | { id: string }, ): Promise { const idVal = typeof id == "object" ? id.id : id; - console.debug("getNote id =", JSON.stringify(idVal)); + log.debug("getNote id =", JSON.stringify(idVal)); return singleRow( await queryObject( "select * from note where id = $1", @@ -228,7 +229,7 @@ export async function getTeam( } function someRows(result: { rows: T[] } | null): T[] | null { - console.debug(result); + log.debug(result); if (!result || result.rows.length < 1) return null; else return result.rows; } @@ -236,7 +237,7 @@ function someRows(result: { rows: T[] } | null): T[] | null { function singleRow(result: { rows: T[] } | null): T | null { if (!result || result.rows.length < 1) return null; else if (result.rows.length > 1) { - console.error( + log.error( "This singleRow result brought back more than 1 row:", result, ); diff --git a/fresh.gen.ts b/fresh.gen.ts index 069359d..5924fff 100644 --- a/fresh.gen.ts +++ b/fresh.gen.ts @@ -19,9 +19,10 @@ import * as $12 from "./routes/logout.tsx"; import * as $13 from "./routes/note.tsx"; import * as $14 from "./routes/note/[id].tsx"; import * as $15 from "./routes/note/create.tsx"; -import * as $16 from "./routes/register.tsx"; -import * as $17 from "./routes/route-config-example.tsx"; -import * as $18 from "./routes/search.tsx"; +import * as $16 from "./routes/plain.ts"; +import * as $17 from "./routes/register.tsx"; +import * as $18 from "./routes/route-config-example.tsx"; +import * as $19 from "./routes/search.tsx"; import * as $$0 from "./islands/Countdown.tsx"; import * as $$1 from "./islands/Counter.tsx"; @@ -43,9 +44,10 @@ const manifest = { "./routes/note.tsx": $13, "./routes/note/[id].tsx": $14, "./routes/note/create.tsx": $15, - "./routes/register.tsx": $16, - "./routes/route-config-example.tsx": $17, - "./routes/search.tsx": $18, + "./routes/plain.ts": $16, + "./routes/register.tsx": $17, + "./routes/route-config-example.tsx": $18, + "./routes/search.tsx": $19, }, islands: { "./islands/Countdown.tsx": $$0, diff --git a/import_map.json b/import_map.json index 9264962..432abd0 100644 --- a/import_map.json +++ b/import_map.json @@ -1,7 +1,7 @@ { "imports": { "@/": "./", - "$std/": "https://deno.land/std@0.144.0/", + "$std/": "https://deno.land/std@0.158.0/", "$fresh/": "https://deno.land/x/fresh@1.1.1/", "preact": "https://esm.sh/preact@10.11.0", "preact/": "https://esm.sh/preact@10.11.0/", diff --git a/log.ts b/log.ts new file mode 100644 index 0000000..2d34c45 --- /dev/null +++ b/log.ts @@ -0,0 +1,24 @@ +export * as log from "$std/log/mod.ts"; +import * as log from "$std/log/mod.ts"; + +const levelColors = {}; + +export function setupLoggers() { + log.setup({ + handlers: { + console: new log.handlers.ConsoleHandler("DEBUG", { + formatter: `{datetime} {levelName} {msg}`, + }), + }, + loggers: { + default: { + level: "DEBUG", + handlers: ["console", "file"], + }, + tasks: { + level: "ERROR", + handlers: ["console"], + }, + }, + }); +} diff --git a/mail/mod.ts b/mail/mod.ts index aacfa07..6f70b99 100644 --- a/mail/mod.ts +++ b/mail/mod.ts @@ -1,5 +1,6 @@ import Mailgun from "https://deno.land/x/mailgun@v1.1.1/index.ts"; import { type Message } from "https://deno.land/x/mailgun@v1.1.1/types.ts"; +import * as log from "$std/log/mod.ts"; export type Email = Message; @@ -7,7 +8,7 @@ const MAILGUN_API_KEY = Deno.env.get("MAILGUN_API_KEY"); const MAILGUN_DOMAIN = Deno.env.get("MAILGUN_DOMAIN"); if (!MAILGUN_API_KEY) { - console.error( + log.error( "MAILGUN_API_KEY not set. Emails will not be sent, only logged to the console.", ); } diff --git a/main.ts b/main.ts index ea25e6e..e9dc21c 100644 --- a/main.ts +++ b/main.ts @@ -10,4 +10,8 @@ import manifest from "@/fresh.gen.ts"; import twindPlugin from "$fresh/plugins/twind.ts"; import twindConfig from "@/twind.config.ts"; +import { setupLoggers } from "@/log.ts"; + +setupLoggers(); + await start(manifest, { plugins: [twindPlugin(twindConfig)] }); diff --git a/routes/plain.ts b/routes/plain.ts new file mode 100644 index 0000000..794b258 --- /dev/null +++ b/routes/plain.ts @@ -0,0 +1,5 @@ +import { HandlerContext } from "$fresh/server.ts"; + +export function handler(_req: Request, _context: HandlerContext) { + return new Response("yo", { status: 200 }); +}