Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 68 additions & 0 deletions src/core/logger.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/**
* Rotating file logger. Writes to data/logs/phantom.log.
* Rotates to phantom.log.1 when the file exceeds MAX_SIZE_BYTES.
* Designed to never throw - logging must not crash the main process.
*/

import { appendFileSync, existsSync, mkdirSync, renameSync, statSync } from "node:fs";
import { dirname, resolve } from "node:path";

const MAX_SIZE_BYTES = 10 * 1024 * 1024; // 10 MB

type LogLevel = "ERROR" | "WARN" | "INFO";

export class Logger {
private path: string;
private ready = false;

constructor(logPath: string) {
this.path = resolve(logPath);
try {
mkdirSync(dirname(this.path), { recursive: true });
this.ready = true;
} catch {
// Can't create log directory - silently degrade
}
}

error(tag: string, message: string): void {
this.write("ERROR", tag, message);
}

warn(tag: string, message: string): void {
this.write("WARN", tag, message);
}

info(tag: string, message: string): void {
this.write("INFO", tag, message);
}

getPath(): string {
return this.path;
}

private write(level: LogLevel, tag: string, message: string): void {
if (!this.ready) return;
try {
this.maybeRotate();
const line = `${new Date().toISOString()} [${level}] [${tag}] ${message}\n`;
appendFileSync(this.path, line, "utf-8");
} catch {
// Silently degrade
}
}

private maybeRotate(): void {
try {
if (!existsSync(this.path)) return;
const { size } = statSync(this.path);
if (size >= MAX_SIZE_BYTES) {
renameSync(this.path, `${this.path}.1`);
}
} catch {
// Ignore rotation errors
}
}
}

export const logger = new Logger("data/logs/phantom.log");
15 changes: 15 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TelegramChannel } from "./channels/telegram.ts";
import { WebhookChannel } from "./channels/webhook.ts";
import { loadChannelsConfig, loadConfig } from "./config/loader.ts";
import { installShutdownHandlers, onShutdown } from "./core/graceful.ts";
import { logger } from "./core/logger.ts";
import {
setChannelHealthProvider,
setEvolutionVersionProvider,
Expand Down Expand Up @@ -53,10 +54,24 @@ import { createSecretToolServer } from "./secrets/tools.ts";
import { setPublicDir, setSecretSavedCallback, setSecretsDb } from "./ui/serve.ts";
import { createWebUiToolServer } from "./ui/tools.ts";

// Intercept console.error and console.warn to mirror into the log file.
// All existing code benefits without needing to import the logger directly.
const _origError = console.error.bind(console);
const _origWarn = console.warn.bind(console);
console.error = (...args: unknown[]) => {
_origError(...args);
logger.error("console", args.map(String).join(" "));
};
console.warn = (...args: unknown[]) => {
_origWarn(...args);
logger.warn("console", args.map(String).join(" "));
};

async function main(): Promise<void> {
const startedAt = Date.now();

console.log("[phantom] Starting...");
logger.info("phantom", `Starting - log file: ${logger.getPath()}`);

const config = loadConfig();
console.log(`[phantom] Config loaded: ${config.name} (${config.model}, effort: ${config.effort})`);
Expand Down