Update logger.mjs

This commit is contained in:
被遗忘的记忆
2025-01-27 21:32:34 +08:00
committed by GitHub
parent c7ae8c0ed5
commit 82c9d18951
+49 -29
View File
@@ -1,12 +1,14 @@
import path from "path"; import path from "path";
import fs from "fs"; import fs from "fs";
import { fileURLToPath } from 'url'; import { fileURLToPath } from 'url';
import { Mutex } from 'async-mutex';
const __filename = fileURLToPath(import.meta.url); const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename); const __dirname = path.dirname(__filename);
class Logger { class Logger {
constructor() { constructor() {
this.logMutex = new Mutex();
this.logFilePath = path.join(__dirname, 'requests.log'); this.logFilePath = path.join(__dirname, 'requests.log');
this.statistics = {}; this.statistics = {};
this.monthStart = this.getMonthStart(); this.monthStart = this.getMonthStart();
@@ -32,7 +34,11 @@ class Logger {
// 加载日志 // 加载日志
loadStatistics() { loadStatistics() {
if (fs.existsSync(this.logFilePath)) { this.logMutex.runExclusive(() => {
if (!fs.existsSync(this.logFilePath)) {
fs.writeFileSync(this.logFilePath, '', 'utf8');
return;
}
const data = fs.readFileSync(this.logFilePath, 'utf-8'); const data = fs.readFileSync(this.logFilePath, 'utf-8');
const entries = data.split('\n').filter(line => line.trim()); const entries = data.split('\n').filter(line => line.trim());
const validEntries = []; const validEntries = [];
@@ -140,7 +146,9 @@ class Logger {
// 清理无效数据 // 清理无效数据
const cleanedData = validEntries.map(entry => JSON.stringify(entry)).join('\n') + '\n'; const cleanedData = validEntries.map(entry => JSON.stringify(entry)).join('\n') + '\n';
fs.writeFileSync(this.logFilePath, cleanedData); fs.writeFileSync(this.logFilePath, cleanedData);
} }).catch(err => {
console.error('loadStatistics() 加锁异常:', err);
});
} }
// 更新统计 // 更新统计
@@ -162,31 +170,30 @@ class Logger {
// 记录请求日志 // 记录请求日志
logRequest({provider, email, time, mode, model, completed, unusualQueryVolume}) { logRequest({provider, email, time, mode, model, completed, unusualQueryVolume}) {
provider = provider || process.env.ACTIVE_PROVIDER || 'you';
const logEntryArray = [ const logEntryArray = [
['provider', provider], // 提供者名称 ['provider', provider || process.env.ACTIVE_PROVIDER || 'you'],
['email', email], // 用户邮箱 ['email', email || 'unknown'],
['time', time], // 请求时间 ['time', time],
['mode', mode], // 请求模式 ['mode', mode || 'unknown'],
['model', model], // 请求模型 ['model', model || 'unknown'],
['completed', completed], // 是否完成 ['completed', completed || 'unknown'],
['unusualQueryVolume', unusualQueryVolume], // 是否异常请求量 ['unusualQueryVolume', unusualQueryVolume || 'unknown'],
]; ];
const logEntry = Object.fromEntries(logEntryArray); const logEntry = Object.fromEntries(logEntryArray);
// 写入日志
// 写日志与更新 statistics
this.logMutex.runExclusive(() => {
fs.appendFileSync(this.logFilePath, JSON.stringify(logEntry) + '\n'); fs.appendFileSync(this.logFilePath, JSON.stringify(logEntry) + '\n');
// 当前月份请求更新统计 const logDate = new Date(logEntry.time);
const logDate = new Date(time); const providerName = logEntry.provider;
if (logDate >= this.monthStart) { if (!this.statistics[providerName]) {
// 初始化 provider this.statistics[providerName] = {};
if (!this.statistics[provider]) {
this.statistics[provider] = {};
} }
const userEmail = email || 'unknown'; const userEmail = logEntry.email;
if (!this.statistics[provider][userEmail]) { if (!this.statistics[providerName][userEmail]) {
this.statistics[provider][userEmail] = { this.statistics[providerName][userEmail] = {
allRequests: [], allRequests: [],
monthlyRequests: [], monthlyRequests: [],
dailyRequests: [], dailyRequests: [],
@@ -205,7 +212,7 @@ class Logger {
}; };
} }
const stats = this.statistics[provider][userEmail]; const stats = this.statistics[providerName][userEmail];
stats.allRequests.push(logEntry); stats.allRequests.push(logEntry);
// 当日统计 // 当日统计
@@ -215,16 +222,29 @@ class Logger {
} }
// 本月统计 // 本月统计
if (logDate >= this.monthStart) {
stats.monthlyRequests.push(logEntry); stats.monthlyRequests.push(logEntry);
this.updateStatistics(stats.monthlyStats, logEntry); this.updateStatistics(stats.monthlyStats, logEntry);
} }
}).catch(err => {
console.error('logRequest() 加锁异常:', err);
});
} }
// 输出当前统计信息 // 输出当前统计信息
printStatistics() { printStatistics() {
const provider = process.env.ACTIVE_PROVIDER || 'you'; const provider = process.env.ACTIVE_PROVIDER || 'you';
const monthStart = this.monthStart.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric' }); const monthStartStr = this.monthStart.toLocaleDateString('zh-CN', {
const today = this.today.toLocaleDateString('zh-CN', { year: 'numeric', month: 'long', day: 'numeric' }); year: 'numeric',
month: 'long',
day: 'numeric'
});
const todayStr = this.today.toLocaleDateString('zh-CN', {
year: 'numeric',
month: 'long',
day: 'numeric'
});
if (!this.statistics[provider]) { if (!this.statistics[provider]) {
console.log(`===== 提供者 ${provider} 没有统计数据 =====`); console.log(`===== 提供者 ${provider} 没有统计数据 =====`);
return; return;
@@ -234,21 +254,21 @@ class Logger {
for (const email of emails) { for (const email of emails) {
const stats = this.statistics[provider][email]; const stats = this.statistics[provider][email];
console.log(`用户邮箱: ${email}`); console.log(`用户邮箱: ${email}`);
console.log(`---------- 本月[${monthStart}]统计 ----------`); console.log(`---------- 本月[${monthStartStr}]统计 ----------`);
console.log(`总请求次数: ${stats.monthlyStats.totalRequests}`); console.log(`总请求次数: ${stats.monthlyStats.totalRequests}`);
console.log(`default 请求次数: ${stats.monthlyStats.defaultModeCount}`); console.log(`default 请求次数: ${stats.monthlyStats.defaultModeCount}`);
console.log(`custom 请求次数: ${stats.monthlyStats.customModeCount}`); console.log(`custom 请求次数: ${stats.monthlyStats.customModeCount}`);
console.log('各模型请求次数:'); console.log('各模型请求次数:');
for (const [model, count] of Object.entries(stats.monthlyStats.modelCount)) { for (const [mdl, count] of Object.entries(stats.monthlyStats.modelCount)) {
console.log(` - ${model}: ${count}`); console.log(` - ${mdl}: ${count}`);
} }
console.log(`---------- 今日[${today}]统计 ----------`); console.log(`---------- 今日[${todayStr}]统计 ----------`);
console.log(`总请求次数: ${stats.dailyStats.totalRequests}`); console.log(`总请求次数: ${stats.dailyStats.totalRequests}`);
console.log(`default 请求次数: ${stats.dailyStats.defaultModeCount}`); console.log(`default 请求次数: ${stats.dailyStats.defaultModeCount}`);
console.log(`custom 请求次数: ${stats.dailyStats.customModeCount}`); console.log(`custom 请求次数: ${stats.dailyStats.customModeCount}`);
console.log('各模型请求次数:'); console.log('各模型请求次数:');
for (const [model, count] of Object.entries(stats.dailyStats.modelCount)) { for (const [mdl, count] of Object.entries(stats.dailyStats.modelCount)) {
console.log(` - ${model}: ${count}`); console.log(` - ${mdl}: ${count}`);
} }
console.log('------------------------------'); console.log('------------------------------');
} }