pref: 日志管理优化

This commit is contained in:
zdl
2025-11-04 15:19:49 +08:00
parent 868b4ccebc
commit 8ed65b062b

View File

@@ -24,6 +24,7 @@ import EventScrollList from './DynamicNewsCard/EventScrollList';
import DynamicNewsDetailPanel from './DynamicNewsDetail';
import UnifiedSearchBox from './UnifiedSearchBox';
import { fetchDynamicNews, toggleEventFollow, selectEventFollowStatus } from '../../../store/slices/communityDataSlice';
import { logger } from '../../../utils/logger';
/**
* 分页逻辑自定义 Hook
@@ -70,7 +71,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
const expectedCount = Math.min(pageSize, total - targetPageStartIndex);
const isTargetPageCached = validTargetData.length >= expectedCount;
console.log('[checkTargetPageCache] 目标页缓存检查', {
logger.debug('DynamicNewsCard', '目标页缓存检查', {
targetPage,
targetPageStartIndex,
targetPageEndIndex,
@@ -114,7 +115,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
preloadRange = [targetPage];
}
console.log('[calculatePreloadRange] 计算预加载范围', {
logger.debug('DynamicNewsCard', '计算预加载范围', {
targetPage,
fromPage,
isSequentialNavigation,
@@ -136,7 +137,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
// 如果该页超出数组范围,说明未缓存
if (pageEndIndex > allCachedEvents.length) {
console.log(`[findMissingPages] 页面${page}超出数组范围`, {
logger.debug('DynamicNewsCard', `页面${page}超出数组范围`, {
pageStartIndex,
pageEndIndex,
allCachedEventsLength: allCachedEvents.length
@@ -150,7 +151,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
const expectedCount = Math.min(pageSize, total - pageStartIndex);
const hasNullOrIncomplete = validData.length < expectedCount;
console.log(`[findMissingPages] 页面${page}检查`, {
logger.debug('DynamicNewsCard', `页面${page}数据检查`, {
pageStartIndex,
pageEndIndex,
pageDataLength: pageData.length,
@@ -162,7 +163,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
return hasNullOrIncomplete;
});
console.log('[findMissingPages] 缺失页面检测完成', {
logger.debug('DynamicNewsCard', '缺失页面检测完成', {
preloadRange,
missingPages,
missingPagesCount: missingPages.length
@@ -185,7 +186,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
}
try {
console.log(`[loadPages] 开始加载`, {
logger.debug('DynamicNewsCard', '开始加载页面数据', {
missingPages,
targetPage,
silentMode,
@@ -194,7 +195,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
// 拆分为单页请求,避免 per_page 动态值导致后端返回空数据
for (const page of missingPages) {
console.log(`[loadPages] 开始加载第 ${page}`);
logger.debug('DynamicNewsCard', `开始加载第 ${page}`);
await dispatch(fetchDynamicNews({
page: page,
@@ -203,17 +204,21 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
clearCache: false
})).unwrap();
console.log(`[loadPages] ${page} 页加载完成`);
logger.debug('DynamicNewsCard', `${page} 页加载完成`);
}
console.log('[loadPages] 所有页面加载完成', {
logger.debug('DynamicNewsCard', '所有页面加载完成', {
missingPages,
silentMode
});
return true;
} catch (error) {
console.error('[loadPages] 加载失败', error);
logger.error('DynamicNewsCard', 'loadPages', error, {
targetPage,
silentMode,
missingPages
});
if (!silentMode) {
// 非静默模式下显示错误提示
@@ -239,7 +244,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
// 翻页处理(智能预加载)- 使用子函数重构
const handlePageChange = useCallback(async (newPage) => {
// 🔍 诊断日志 - 记录翻页开始状态
console.log('[handlePageChange] 开始翻页', {
logger.debug('DynamicNewsCard', '开始翻页', {
currentPage,
newPage,
pageSize,
@@ -262,7 +267,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
// 步骤4: 根据情况加载数据
if (isTargetPageCached && missingPages.length > 0 && hasMore) {
// 场景A: 目标页已缓存,立即切换,后台静默预加载其他页
console.log('[handlePageChange] 目标页已缓存,立即切换 + 后台预加载', {
logger.debug('DynamicNewsCard', '目标页已缓存,立即切换 + 后台预加载', {
currentPage,
newPage,
缺失页面: missingPages
@@ -272,7 +277,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
await loadPages(missingPages, newPage, true); // 静默模式
} else if (missingPages.length > 0 && hasMore) {
// 场景B: 目标页未缓存,显示 loading 并等待加载完成
console.log('[handlePageChange] 目标页未缓存,显示 loading', {
logger.debug('DynamicNewsCard', '目标页未缓存,显示 loading', {
currentPage,
newPage,
缺失页面: missingPages
@@ -284,7 +289,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
}
} else if (missingPages.length === 0) {
// 场景C: 所有页面均已缓存,直接切换
console.log('[handlePageChange] 无需加载,直接切换', {
logger.debug('DynamicNewsCard', '无需加载,直接切换', {
currentPage,
newPage,
reason: '所有页面均已缓存'
@@ -293,7 +298,7 @@ const usePagination = ({ allCachedEvents, total, cachedCount, dispatch, toast })
setCurrentPage(newPage);
} else {
// 场景D: 意外分支(有缺失页面但 hasMore=false
console.warn('[handlePageChange] 意外分支:有缺失页面但无法加载', {
logger.warn('DynamicNewsCard', '意外分支:有缺失页面但无法加载', {
missingPages,
hasMore,
currentPage,