// utils/logging.ts // Helper to format date as yyyymmdd function getDateString() { const now = new Date(); const yyyy = now.getFullYear(); const mm = String(now.getMonth() + 1).padStart(2, "0"); const dd = String(now.getDate()).padStart(2, "0"); return `${yyyy}${mm}${dd}`; } // Ensure the log directory exists using Deno.stat async function ensureLogDir() { const logDir = "./log"; try { const stat = await Deno.stat(logDir); if (!stat.isDirectory) { throw new Error(`${logDir} exists but is not a directory`); } } catch (err) { if (err instanceof Deno.errors.NotFound) { await Deno.mkdir(logDir, { recursive: true }); } else { throw err; } } } // Write a log entry export async function writeAudioLog(entry: string) { await ensureLogDir(); const dateStr = getDateString(); const logFile = `./log/audio-${dateStr}.log`; const logEntry = `[${new Date().toISOString()}] ${entry}\n`; await Deno.writeTextFile(logFile, logEntry, { append: true }); } // Write a log entry export async function writeImageLog(entry: string) { await ensureLogDir(); const dateStr = getDateString(); const logFile = `./log/image-${dateStr}.log`; const logEntry = `[${new Date().toISOString()}] ${entry}\n`; await Deno.writeTextFile(logFile, logEntry, { append: true }); } export async function logBlobFetch(entry: string) { await ensureLogDir(); const dateStr = getDateString(); const logFile = `./log/blob-${dateStr}.log`; const logEntry = `[${new Date().toISOString()}] ${entry}\n`; await Deno.writeTextFile(logFile, logEntry, { append: true }); }