Files
routstrd/src/utils/logger.ts
T
redshift fd8f433a36 feat: add Docker dev environment and fix logging/cashu-ts
- Add Dockerfile, docker-compose.yml, and .dockerignore for Bun dev container
- Fix sdkLogger to use logger.warn instead of logger.log for warnings
- Add proper warn method to logger (writes to file instead of console)
- Use console.log for daemon startup messages to keep them visible
- Update cashu-ts getDecodedToken call with explicit well-known-pubkeys
- Use proof.amount.toNumber() for updated cashu-ts Amount type
2026-05-16 16:22:16 +08:00

66 lines
1.7 KiB
TypeScript

import { appendFile, mkdir } from "fs/promises";
import { existsSync } from "fs";
import { join } from "path";
const HOME = process.env.HOME || process.env.USERPROFILE || "";
const LOG_DIR = process.env.ROUTSTRD_DIR || `${HOME}/.routstrd`;
const LOGS_DIR = join(LOG_DIR, "logs");
function getLogFileForDate(date: Date = new Date()): string {
const year = date.getFullYear();
const month = String(date.getMonth() + 1).padStart(2, "0");
const day = String(date.getDate()).padStart(2, "0");
return join(LOGS_DIR, `${year}-${month}-${day}.log`);
}
async function ensureLogDir() {
if (!existsSync(LOGS_DIR)) {
await mkdir(LOGS_DIR, { recursive: true });
}
}
async function writeLog(level: string, ...args: unknown[]) {
await ensureLogDir();
const timestamp = new Date().toISOString();
const message = args
.map((a) => {
if (a instanceof Error) {
return `${a.message}${a.stack ? `\n${a.stack}` : ""}`;
}
if (typeof a === "object") {
try {
return JSON.stringify(a);
} catch {
return String(a);
}
}
return String(a);
})
.join(" ");
const line = `[${timestamp}] [${level}] ${message}\n`;
const logFile = getLogFileForDate(new Date(timestamp));
try {
await appendFile(logFile, line);
} catch (error) {
console.error("Failed to write log:", error);
}
}
export const logger = {
log: (...args: unknown[]) => {
writeLog("INFO", ...args);
},
debug: (...args: unknown[]) => {
writeLog("DEBUG", ...args);
},
warn: (...args: unknown[]) => {
writeLog("WARN", ...args);
},
error: (...args: unknown[]) => {
writeLog("ERROR", ...args);
},
info: (...args: unknown[]) => {
writeLog("INFO", ...args);
},
};