const fs = require('fs'); const path = require('path'); // 日志文件路径 const logFilePath = path.join(__dirname, 'logs', 'output.log'); // 读取日志文件并搜索关键字 function searchGrossWeightLogs() { try { console.log(`正在搜索日志文件: ${logFilePath}`); console.log('寻找商品发布请求和毛重处理相关记录...\n'); // 读取日志文件内容 const logContent = fs.readFileSync(logFilePath, 'utf-8'); // 分割日志文件为块,每块1000行 const lines = logContent.split('\n'); const chunkSize = 1000; const chunks = []; for (let i = 0; i < lines.length; i += chunkSize) { chunks.push(lines.slice(i, i + chunkSize).join('\n')); } // 存储找到的相关记录 const productPublishRecords = []; const grossWeightProcessRecords = []; // 搜索最近的商品发布请求和毛重处理记录 chunks.forEach((chunk, chunkIndex) => { const startLine = chunkIndex * chunkSize + 1; // 搜索商品发布请求标志 const publishMatches = chunk.matchAll(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*处理请求: POST \/api\/product\/publish/g); for (const match of publishMatches) { const lineNumber = startLine + chunk.substring(0, match.index).split('\n').length; productPublishRecords.push({ timestamp: match[1], lineNumber: lineNumber, content: match[0] }); } // 搜索毛重处理相关记录 const weightMatches = chunk.matchAll(/(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}).*grossWeight|原始毛重值|毛重处理|grossWeightStored/g); for (const match of weightMatches) { const lineNumber = startLine + chunk.substring(0, match.index).split('\n').length; grossWeightProcessRecords.push({ timestamp: match[1] || '未知', lineNumber: lineNumber, content: match[0] }); } }); // 输出结果 console.log('===== 毛重相关日志搜索结果 =====\n'); // 显示最近的商品发布请求 console.log(`找到 ${productPublishRecords.length} 条商品发布请求记录`); if (productPublishRecords.length > 0) { console.log('\n最近的5条商品发布请求:'); productPublishRecords.slice(-5).forEach((record, index) => { console.log(`[${record.timestamp}] 第${record.lineNumber}行: ${record.content}`); }); } // 显示最近的毛重处理记录 console.log('\n\n最近的10条毛重处理记录:'); grossWeightProcessRecords.slice(-10).forEach((record, index) => { console.log(`[${record.timestamp}] 第${record.lineNumber}行: ${record.content}`); }); // 给出建议 console.log('\n\n===== 排查建议 ====='); if (productPublishRecords.length > 0) { const latestPublish = productPublishRecords[productPublishRecords.length - 1]; console.log(`1. 最近的商品发布请求在第${latestPublish.lineNumber}行,请查看该请求附近的详细日志`); } console.log('2. 手动搜索日志中的关键字:'); console.log(' - "grossWeight" - 查看所有毛重字段相关记录'); console.log(' - "原始毛重值" - 查看后端处理的原始值'); console.log(' - "最终处理的毛重值" - 查看处理后的存储值'); console.log('3. 检查前端传递的grossWeight格式是否正确'); console.log('4. 确认数据库中商品记录的grossWeight字段实际值'); } catch (error) { console.error('搜索日志时发生错误:', error.message); console.log('\n请尝试使用以下命令手动查看日志:'); console.log('在PowerShell中运行:'); console.log(`Get-Content -Path "${logFilePath}" | Select-String -Pattern "grossWeight|publish" -Context 2,5 | Select-Object -Last 20`); } } // 执行搜索 searchGrossWeightLogs();