feat: 日志优化

This commit is contained in:
zdl
2025-10-18 12:26:08 +08:00
parent 4ebb17190f
commit a7695c7365
7 changed files with 127 additions and 36 deletions

View File

@@ -4,12 +4,17 @@
* 对应Flask后端的所有财务API接口
*/
import { logger } from '../utils/logger';
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : process.env.REACT_APP_API_URL;
const apiRequest = async (url, options = {}) => {
try {
console.log(`Making Financial API request to: ${API_BASE_URL}${url}`);
logger.debug('financialService', 'API请求', {
url: `${API_BASE_URL}${url}`,
method: options.method || 'GET'
});
const response = await fetch(`${API_BASE_URL}${url}`, {
...options,
@@ -22,16 +27,24 @@ const apiRequest = async (url, options = {}) => {
if (!response.ok) {
const errorText = await response.text();
console.error(`Financial API request failed: ${response.status} - ${errorText}`);
logger.error('financialService', 'apiRequest', new Error(`HTTP ${response.status}`), {
url,
status: response.status,
errorText: errorText.substring(0, 200)
});
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(`Financial API response from ${url}:`, data);
logger.debug('financialService', 'API响应', {
url,
success: data.success,
hasData: !!data.data
});
return data;
} catch (error) {
console.error(`Financial API request failed for ${url}:`, error);
logger.error('financialService', 'apiRequest', error, { url });
throw error;
}
};

View File

@@ -4,12 +4,17 @@
* 对应Flask后端的所有市场API接口
*/
import { logger } from '../utils/logger';
const isProduction = process.env.NODE_ENV === 'production';
const API_BASE_URL = isProduction ? "" : process.env.REACT_APP_API_URL;
const apiRequest = async (url, options = {}) => {
try {
console.log(`Making Market API request to: ${API_BASE_URL}${url}`);
logger.debug('marketService', 'API请求', {
url: `${API_BASE_URL}${url}`,
method: options.method || 'GET'
});
const response = await fetch(`${API_BASE_URL}${url}`, {
...options,
@@ -22,16 +27,24 @@ const apiRequest = async (url, options = {}) => {
if (!response.ok) {
const errorText = await response.text();
console.error(`Market API request failed: ${response.status} - ${errorText}`);
logger.error('marketService', 'apiRequest', new Error(`HTTP ${response.status}`), {
url,
status: response.status,
errorText: errorText.substring(0, 200)
});
throw new Error(`HTTP error! status: ${response.status}`);
}
const data = await response.json();
console.log(`Market API response from ${url}:`, data);
logger.debug('marketService', 'API响应', {
url,
success: data.success,
hasData: !!data.data
});
return data;
} catch (error) {
console.error(`Market API request failed for ${url}:`, error);
logger.error('marketService', 'apiRequest', error, { url });
throw error;
}
};
@@ -449,7 +462,7 @@ export const marketCacheManager = {
if (cached) {
const { data, timestamp } = cached;
if (Date.now() - timestamp < maxAge) {
console.log(`Cache hit for key: ${key}`);
logger.debug('marketCacheManager', '缓存命中', { key });
return data;
}
this.cache.delete(key);
@@ -467,15 +480,16 @@ export const marketCacheManager = {
data,
timestamp: Date.now()
});
console.log(`Data cached for key: ${key}`);
logger.debug('marketCacheManager', '数据已缓存', { key });
},
/**
* 清除所有缓存
*/
clear: function() {
const cacheSize = this.cache.size;
this.cache.clear();
console.log('All market cache cleared');
logger.debug('marketCacheManager', '清除所有缓存', { clearedCount: cacheSize });
},
/**
@@ -483,12 +497,17 @@ export const marketCacheManager = {
* @param {string} stockCode - 股票代码
*/
clearStock: function(stockCode) {
let clearedCount = 0;
for (const key of this.cache.keys()) {
if (key.includes(`market_${stockCode}_`)) {
this.cache.delete(key);
clearedCount++;
}
}
console.log(`Cache cleared for stock: ${stockCode}`);
logger.debug('marketCacheManager', '清除股票缓存', {
stockCode,
clearedCount
});
}
};