]> git.morado.dev Git - shop/commitdiff
Logger
authorRoberto Morado <roramigator@duck.com>
Sat, 16 May 2026 03:32:03 +0000 (23:32 -0400)
committerRoberto Morado <roramigator@duck.com>
Sat, 16 May 2026 03:32:03 +0000 (23:32 -0400)
.gitignore [new file with mode: 0644]
deno.json
main.ts
src/logger.ts [new file with mode: 0644]

diff --git a/.gitignore b/.gitignore
new file mode 100644 (file)
index 0000000..7a6501a
--- /dev/null
@@ -0,0 +1,2 @@
+.env
+logs/
index f3e6d305ab9f8a5da76ec87615c6fa9bfb6f83d1..6ca594ec4266bb2ccdf547b6712e128babb33388 100644 (file)
--- a/deno.json
+++ b/deno.json
@@ -1,7 +1,7 @@
 {
   "tasks": {
-    "dev": "deno run --allow-net --allow-env --allow-read --env-file --unstable-kv main.ts",
-    "start": "deno run --allow-net --allow-env --allow-read --env-file --unstable-kv main.ts"
+    "dev": "deno run --allow-net --allow-env --allow-read --allow-write --env-file --unstable-kv main.ts",
+    "start": "deno run --allow-net --allow-env --allow-read --allow-write --env-file --unstable-kv main.ts"
   },
   "imports": {
     "hono": "npm:hono@^4",
diff --git a/main.ts b/main.ts
index e1f6984e5dc9d7c7fb1c0dfb44729dc3dd50a7a2..5ca740d6b4a26628f1e8b5b23bd5b70bb17dcd1d 100644 (file)
--- a/main.ts
+++ b/main.ts
@@ -1,3 +1,4 @@
+import "./src/logger.ts";
 import { Hono } from "hono";
 import { logger } from "hono/logger";
 import { session } from "./src/middleware/session.ts";
diff --git a/src/logger.ts b/src/logger.ts
new file mode 100644 (file)
index 0000000..34609f6
--- /dev/null
@@ -0,0 +1,42 @@
+// Patches global console so every log/warn/error is written to logs/YYYY-MM-DD.log
+// Import once at the top of main.ts — no other changes needed anywhere.
+
+const LOG_DIR = "./logs";
+
+try {
+  Deno.mkdirSync(LOG_DIR, { recursive: true });
+} catch { /* already exists */ }
+
+const enc = new TextEncoder();
+let currentDate = "";
+let file: Deno.FsFile | null = null;
+
+function getFile(): Deno.FsFile {
+  const today = new Date().toISOString().slice(0, 10); // YYYY-MM-DD
+  if (today !== currentDate) {
+    file?.close();
+    file = Deno.openSync(`${LOG_DIR}/${today}.log`, { create: true, append: true });
+    currentDate = today;
+  }
+  return file!;
+}
+
+function write(level: string, args: unknown[]) {
+  const ts = new Date().toISOString();
+  const body = args
+    .map((a) => (typeof a === "string" ? a : JSON.stringify(a, null, 2)))
+    .join(" ");
+  try {
+    getFile().writeSync(enc.encode(`[${ts}] [${level}] ${body}\n`));
+  } catch { /* don't let a log failure crash the app */ }
+}
+
+const _log   = console.log.bind(console);
+const _info  = console.info.bind(console);
+const _warn  = console.warn.bind(console);
+const _error = console.error.bind(console);
+
+console.log   = (...a: unknown[]) => { _log(...a);   write("INFO",  a); };
+console.info  = (...a: unknown[]) => { _info(...a);  write("INFO",  a); };
+console.warn  = (...a: unknown[]) => { _warn(...a);  write("WARN",  a); };
+console.error = (...a: unknown[]) => { _error(...a); write("ERROR", a); };