From 1a7e90df391af6e3f952b697bddf70a7d6b4956a Mon Sep 17 00:00:00 2001 From: musistudio Date: Sat, 6 Sep 2025 09:06:18 +0800 Subject: [PATCH] remove log util --- src/index.ts | 12 +++++++++--- src/utils/log.ts | 45 --------------------------------------------- src/utils/router.ts | 16 ++++++---------- 3 files changed, 15 insertions(+), 58 deletions(-) delete mode 100644 src/utils/log.ts diff --git a/src/index.ts b/src/index.ts index 28fe238..0b120aa 100644 --- a/src/index.ts +++ b/src/index.ts @@ -14,7 +14,6 @@ import { import { CONFIG_FILE } from "./constants"; import { createStream } from 'rotating-file-stream'; import { HOME_DIR } from "./constants"; -import { configureLogging } from "./utils/log"; import { sessionUsageCache } from "./utils/cache"; import {SSEParserTransform} from "./utils/SSEParser.transform"; import {SSESerializerTransform} from "./utils/SSESerializer.transform"; @@ -63,8 +62,6 @@ async function run(options: RunOptions = {}) { await cleanupLogFiles(); const config = await initConfig(); - // Configure logging based on config - configureLogging(config); let HOST = config.HOST || "127.0.0.1"; @@ -139,6 +136,15 @@ async function run(options: RunOptions = {}) { }, logger: loggerConfig, }); + + // Add global error handlers to prevent the service from crashing + process.on("uncaughtException", (err) => { + server.log.error("Uncaught exception:", err); + }); + + process.on("unhandledRejection", (reason, promise) => { + server.log.error("Unhandled rejection at:", promise, "reason:", reason); + }); // Add async preHandler hook for authentication server.addHook("preHandler", async (req, reply) => { return new Promise((resolve, reject) => { diff --git a/src/utils/log.ts b/src/utils/log.ts deleted file mode 100644 index 1ff6fb9..0000000 --- a/src/utils/log.ts +++ /dev/null @@ -1,45 +0,0 @@ -import fs from "node:fs"; -import path from "node:path"; -import { HOME_DIR } from "../constants"; - -const LOG_FILE = path.join(HOME_DIR, "claude-code-router.log"); - -// Ensure log directory exists -if (!fs.existsSync(HOME_DIR)) { - fs.mkdirSync(HOME_DIR, { recursive: true }); -} - -// Global variable to store the logging configuration -let isLogEnabled: boolean | null = null; -let logLevel: string = "info"; - -// Function to configure logging -export function configureLogging(config: { LOG?: boolean; LOG_LEVEL?: string }) { - isLogEnabled = config.LOG !== false; // Default to true if not explicitly set to false - logLevel = config.LOG_LEVEL || "debug"; -} - -export function log(...args: any[]) { - // If logging configuration hasn't been set, default to enabled - if (isLogEnabled === null) { - isLogEnabled = true; - } - - if (!isLogEnabled) { - return; - } - - const timestamp = new Date().toISOString(); - const logMessage = `[${timestamp}] ${ - Array.isArray(args) - ? args - .map((arg) => - typeof arg === "object" ? JSON.stringify(arg) : String(arg) - ) - .join(" ") - : "" - }\n`; - - // Append to log file - fs.appendFileSync(LOG_FILE, logMessage, "utf8"); -} diff --git a/src/utils/router.ts b/src/utils/router.ts index 79d939e..1977e2e 100644 --- a/src/utils/router.ts +++ b/src/utils/router.ts @@ -4,7 +4,6 @@ import { Tool, } from "@anthropic-ai/sdk/resources/messages"; import { get_encoding } from "tiktoken"; -import { log } from "./log"; import { sessionUsageCache, Usage } from "./cache"; const enc = get_encoding("cl100k_base"); @@ -94,11 +93,8 @@ const getUseModel = async ( (lastUsageThreshold || tokenCountThreshold) && config.Router.longContext ) { - log( - "Using long context model due to token count:", - tokenCount, - "threshold:", - longContextThreshold + req.log.info( + `Using long context model due to token count: ${tokenCount}, threshold: ${longContextThreshold}` ); return config.Router.longContext; } @@ -122,12 +118,12 @@ const getUseModel = async ( req.body.model?.startsWith("claude-3-5-haiku") && config.Router.background ) { - log("Using background model for ", req.body.model); + req.log.info(`Using background model for ${req.body.model}`); return config.Router.background; } // if exits thinking, use the think model if (req.body.thinking && config.Router.think) { - log("Using think model for ", req.body.thinking); + req.log.info(`Using think model for ${req.body.thinking}`); return config.Router.think; } if ( @@ -167,7 +163,7 @@ export const router = async (req: any, _res: any, context: any) => { event }); } catch (e: any) { - log("failed to load custom router", e.message); + req.log.error(`failed to load custom router: ${e.message}`); } } if (!model) { @@ -175,7 +171,7 @@ export const router = async (req: any, _res: any, context: any) => { } req.body.model = model; } catch (error: any) { - log("Error in router middleware:", error.message); + req.log.error(`Error in router middleware: ${error.message}`); req.body.model = config.Router!.default; } return;