remove log util

This commit is contained in:
musistudio
2025-09-06 09:06:18 +08:00
parent e5741ae470
commit 1a7e90df39
3 changed files with 15 additions and 58 deletions

View File

@@ -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) => {

View File

@@ -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");
}

View File

@@ -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;