Logging
This commit is contained in:
parent
ac44a249ed
commit
56378fbc26
|
@ -1,3 +1,5 @@
|
||||||
|
import "$std/log/mod.ts";
|
||||||
|
|
||||||
export interface Config {
|
export interface Config {
|
||||||
postgres: {
|
postgres: {
|
||||||
url: string;
|
url: string;
|
||||||
|
|
13
db/mod.ts
13
db/mod.ts
|
@ -10,6 +10,7 @@ import {
|
||||||
} from "https://deno.land/x/postgres@v0.16.1/query/query.ts?s=QueryArguments";
|
} from "https://deno.land/x/postgres@v0.16.1/query/query.ts?s=QueryArguments";
|
||||||
import { config } from "@/config.ts";
|
import { config } from "@/config.ts";
|
||||||
import * as base64 from "$std/encoding/base64.ts";
|
import * as base64 from "$std/encoding/base64.ts";
|
||||||
|
import { log } from "@/log.ts";
|
||||||
|
|
||||||
import {
|
import {
|
||||||
type Identifiable,
|
type Identifiable,
|
||||||
|
@ -36,14 +37,14 @@ async function dbOp<T>(op: (connection: PoolClient) => Promise<T>) {
|
||||||
try {
|
try {
|
||||||
result = await op(connection);
|
result = await op(connection);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Error querying database:", { ...err });
|
log.error("Error querying database:", { ...err });
|
||||||
exception = err;
|
exception = err;
|
||||||
} finally {
|
} finally {
|
||||||
connection.release();
|
connection.release();
|
||||||
}
|
}
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
exception = err;
|
exception = err;
|
||||||
console.error("Error connecting to database:", err);
|
log.error("Error connecting to database:", err);
|
||||||
}
|
}
|
||||||
if (exception != null) throw exception;
|
if (exception != null) throw exception;
|
||||||
return result;
|
return result;
|
||||||
|
@ -59,7 +60,7 @@ export async function queryObject<T>(
|
||||||
text: sql.trim(),
|
text: sql.trim(),
|
||||||
args,
|
args,
|
||||||
});
|
});
|
||||||
console.debug(result);
|
log.debug(result);
|
||||||
return result;
|
return result;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -88,7 +89,7 @@ export async function getNote(
|
||||||
id: string | { id: string },
|
id: string | { id: string },
|
||||||
): Promise<Note | null> {
|
): Promise<Note | null> {
|
||||||
const idVal = typeof id == "object" ? id.id : id;
|
const idVal = typeof id == "object" ? id.id : id;
|
||||||
console.debug("getNote id =", JSON.stringify(idVal));
|
log.debug("getNote id =", JSON.stringify(idVal));
|
||||||
return singleRow(
|
return singleRow(
|
||||||
await queryObject<Note>(
|
await queryObject<Note>(
|
||||||
"select * from note where id = $1",
|
"select * from note where id = $1",
|
||||||
|
@ -228,7 +229,7 @@ export async function getTeam(
|
||||||
}
|
}
|
||||||
|
|
||||||
function someRows<T>(result: { rows: T[] } | null): T[] | null {
|
function someRows<T>(result: { rows: T[] } | null): T[] | null {
|
||||||
console.debug(result);
|
log.debug(result);
|
||||||
if (!result || result.rows.length < 1) return null;
|
if (!result || result.rows.length < 1) return null;
|
||||||
else return result.rows;
|
else return result.rows;
|
||||||
}
|
}
|
||||||
|
@ -236,7 +237,7 @@ function someRows<T>(result: { rows: T[] } | null): T[] | null {
|
||||||
function singleRow<T>(result: { rows: T[] } | null): T | null {
|
function singleRow<T>(result: { rows: T[] } | null): T | null {
|
||||||
if (!result || result.rows.length < 1) return null;
|
if (!result || result.rows.length < 1) return null;
|
||||||
else if (result.rows.length > 1) {
|
else if (result.rows.length > 1) {
|
||||||
console.error(
|
log.error(
|
||||||
"This singleRow result brought back more than 1 row:",
|
"This singleRow result brought back more than 1 row:",
|
||||||
result,
|
result,
|
||||||
);
|
);
|
||||||
|
|
14
fresh.gen.ts
14
fresh.gen.ts
|
@ -19,9 +19,10 @@ import * as $12 from "./routes/logout.tsx";
|
||||||
import * as $13 from "./routes/note.tsx";
|
import * as $13 from "./routes/note.tsx";
|
||||||
import * as $14 from "./routes/note/[id].tsx";
|
import * as $14 from "./routes/note/[id].tsx";
|
||||||
import * as $15 from "./routes/note/create.tsx";
|
import * as $15 from "./routes/note/create.tsx";
|
||||||
import * as $16 from "./routes/register.tsx";
|
import * as $16 from "./routes/plain.ts";
|
||||||
import * as $17 from "./routes/route-config-example.tsx";
|
import * as $17 from "./routes/register.tsx";
|
||||||
import * as $18 from "./routes/search.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 $$0 from "./islands/Countdown.tsx";
|
||||||
import * as $$1 from "./islands/Counter.tsx";
|
import * as $$1 from "./islands/Counter.tsx";
|
||||||
|
|
||||||
|
@ -43,9 +44,10 @@ const manifest = {
|
||||||
"./routes/note.tsx": $13,
|
"./routes/note.tsx": $13,
|
||||||
"./routes/note/[id].tsx": $14,
|
"./routes/note/[id].tsx": $14,
|
||||||
"./routes/note/create.tsx": $15,
|
"./routes/note/create.tsx": $15,
|
||||||
"./routes/register.tsx": $16,
|
"./routes/plain.ts": $16,
|
||||||
"./routes/route-config-example.tsx": $17,
|
"./routes/register.tsx": $17,
|
||||||
"./routes/search.tsx": $18,
|
"./routes/route-config-example.tsx": $18,
|
||||||
|
"./routes/search.tsx": $19,
|
||||||
},
|
},
|
||||||
islands: {
|
islands: {
|
||||||
"./islands/Countdown.tsx": $$0,
|
"./islands/Countdown.tsx": $$0,
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"imports": {
|
"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/",
|
"$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",
|
||||||
"preact/": "https://esm.sh/preact@10.11.0/",
|
"preact/": "https://esm.sh/preact@10.11.0/",
|
||||||
|
|
24
log.ts
Normal file
24
log.ts
Normal file
|
@ -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"],
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,5 +1,6 @@
|
||||||
import Mailgun from "https://deno.land/x/mailgun@v1.1.1/index.ts";
|
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 { 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;
|
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");
|
const MAILGUN_DOMAIN = Deno.env.get("MAILGUN_DOMAIN");
|
||||||
|
|
||||||
if (!MAILGUN_API_KEY) {
|
if (!MAILGUN_API_KEY) {
|
||||||
console.error(
|
log.error(
|
||||||
"MAILGUN_API_KEY not set. Emails will not be sent, only logged to the console.",
|
"MAILGUN_API_KEY not set. Emails will not be sent, only logged to the console.",
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
4
main.ts
4
main.ts
|
@ -10,4 +10,8 @@ import manifest from "@/fresh.gen.ts";
|
||||||
import twindPlugin from "$fresh/plugins/twind.ts";
|
import twindPlugin from "$fresh/plugins/twind.ts";
|
||||||
import twindConfig from "@/twind.config.ts";
|
import twindConfig from "@/twind.config.ts";
|
||||||
|
|
||||||
|
import { setupLoggers } from "@/log.ts";
|
||||||
|
|
||||||
|
setupLoggers();
|
||||||
|
|
||||||
await start(manifest, { plugins: [twindPlugin(twindConfig)] });
|
await start(manifest, { plugins: [twindPlugin(twindConfig)] });
|
||||||
|
|
5
routes/plain.ts
Normal file
5
routes/plain.ts
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
import { HandlerContext } from "$fresh/server.ts";
|
||||||
|
|
||||||
|
export function handler(_req: Request, _context: HandlerContext) {
|
||||||
|
return new Response("yo", { status: 200 });
|
||||||
|
}
|
Loading…
Reference in a new issue