1*41ddce91Smaotoumaoimport { fileAsyncTransport, logger } from "react-native-logs"; 2*41ddce91Smaotoumaoimport RNFS, { readDir, readFile } from "react-native-fs"; 3*41ddce91Smaotoumaoimport pathConst from "@/constants/pathConst"; 4*41ddce91Smaotoumaoimport Config from "../core/config.ts"; 5*41ddce91Smaotoumaoimport { addLog } from "@/lib/react-native-vdebug/src/log"; 6242960d3S猫头猫 7242960d3S猫头猫const config = { 8242960d3S猫头猫 transport: fileAsyncTransport, 9242960d3S猫头猫 transportOptions: { 10242960d3S猫头猫 FS: RNFS, 11242960d3S猫头猫 filePath: pathConst.logPath, 125589cdf3S猫头猫 fileName: 'error-log-{date-today}.log', 13242960d3S猫头猫 }, 14242960d3S猫头猫 dateFormat: 'local', 15242960d3S猫头猫}; 16242960d3S猫头猫 17242960d3S猫头猫const traceConfig = { 18242960d3S猫头猫 transport: fileAsyncTransport, 19242960d3S猫头猫 transportOptions: { 20242960d3S猫头猫 FS: RNFS, 21242960d3S猫头猫 filePath: pathConst.logPath, 225589cdf3S猫头猫 fileName: 'trace-log.log', 23242960d3S猫头猫 }, 24242960d3S猫头猫 dateFormat: 'local', 25242960d3S猫头猫}; 26242960d3S猫头猫 27242960d3S猫头猫const log = logger.createLogger(config); 28242960d3S猫头猫const traceLogger = logger.createLogger(traceConfig); 29242960d3S猫头猫 30242960d3S猫头猫export function trace( 31242960d3S猫头猫 desc: string, 321e263108S猫头猫 message?: any, 33242960d3S猫头猫 level: 'info' | 'error' = 'info', 34242960d3S猫头猫) { 35242960d3S猫头猫 if (__DEV__) { 36242960d3S猫头猫 console.log(desc, message); 37242960d3S猫头猫 } 38242960d3S猫头猫 // 特殊情况记录操作路径 39*41ddce91Smaotoumao if (Config.getConfig('debug.traceLog')) { 40242960d3S猫头猫 traceLogger[level]({ 41242960d3S猫头猫 desc, 42242960d3S猫头猫 message, 43242960d3S猫头猫 }); 44242960d3S猫头猫 } 45242960d3S猫头猫} 46242960d3S猫头猫 4760dec65dS猫头猫export async function clearLog() { 4860dec65dS猫头猫 const files = await RNFS.readDir(pathConst.logPath); 4960dec65dS猫头猫 await Promise.all( 5060dec65dS猫头猫 files.map(async file => { 5160dec65dS猫头猫 if (file.isFile()) { 5260dec65dS猫头猫 try { 5360dec65dS猫头猫 await RNFS.unlink(file.path); 5460dec65dS猫头猫 } catch {} 5560dec65dS猫头猫 } 5660dec65dS猫头猫 }), 5760dec65dS猫头猫 ); 5860dec65dS猫头猫} 5960dec65dS猫头猫 60fc2b6cc8S猫头猫export async function getErrorLogContent() { 61fc2b6cc8S猫头猫 try { 62fc2b6cc8S猫头猫 const files = await readDir(pathConst.logPath); 63fc2b6cc8S猫头猫 console.log(files); 64fc2b6cc8S猫头猫 const today = new Date(); 65fc2b6cc8S猫头猫 // 两天的错误日志 66fc2b6cc8S猫头猫 const yesterday = new Date(); 67fc2b6cc8S猫头猫 yesterday.setDate(today.getDate() - 1); 68fc2b6cc8S猫头猫 const todayLog = files.find( 69fc2b6cc8S猫头猫 _ => 70fc2b6cc8S猫头猫 _.isFile() && 71fc2b6cc8S猫头猫 _.path.endsWith( 72fc2b6cc8S猫头猫 `error-log-${today.getDate()}-${ 73fc2b6cc8S猫头猫 today.getMonth() + 1 74fc2b6cc8S猫头猫 }-${today.getFullYear()}.log`, 75fc2b6cc8S猫头猫 ), 76fc2b6cc8S猫头猫 ); 77fc2b6cc8S猫头猫 const yesterdayLog = files.find( 78fc2b6cc8S猫头猫 _ => 79fc2b6cc8S猫头猫 _.isFile() && 80fc2b6cc8S猫头猫 _.path.endsWith( 81fc2b6cc8S猫头猫 `error-log-${yesterday.getDate()}-${ 82fc2b6cc8S猫头猫 yesterday.getMonth() + 1 83fc2b6cc8S猫头猫 }-${yesterday.getFullYear()}.log`, 84fc2b6cc8S猫头猫 ), 85fc2b6cc8S猫头猫 ); 86fc2b6cc8S猫头猫 let logContent = ''; 87fc2b6cc8S猫头猫 if (todayLog) { 88fc2b6cc8S猫头猫 logContent += await readFile(todayLog.path, 'utf8'); 89fc2b6cc8S猫头猫 } 90fc2b6cc8S猫头猫 if (yesterdayLog) { 91fc2b6cc8S猫头猫 logContent += await readFile(yesterdayLog.path, 'utf8'); 92fc2b6cc8S猫头猫 } 93fc2b6cc8S猫头猫 return logContent; 94fc2b6cc8S猫头猫 } catch { 95fc2b6cc8S猫头猫 return ''; 96fc2b6cc8S猫头猫 } 97fc2b6cc8S猫头猫} 98fc2b6cc8S猫头猫 99242960d3S猫头猫export function errorLog(desc: string, message: any) { 100*41ddce91Smaotoumao if (Config.getConfig('debug.errorLog')) { 101242960d3S猫头猫 log.error({ 102242960d3S猫头猫 desc, 103242960d3S猫头猫 message, 104242960d3S猫头猫 }); 105242960d3S猫头猫 trace(desc, message, 'error'); 106242960d3S猫头猫 } 107242960d3S猫头猫} 108242960d3S猫头猫 109ea6d708fS猫头猫export function devLog( 110ea6d708fS猫头猫 method: 'log' | 'error' | 'warn' | 'info', 111ea6d708fS猫头猫 ...args: any[] 112ea6d708fS猫头猫) { 113*41ddce91Smaotoumao if (Config.getConfig('debug.devLog')) { 114ea6d708fS猫头猫 addLog(method, args); 115ea6d708fS猫头猫 } 116ea6d708fS猫头猫} 117ea6d708fS猫头猫 118242960d3S猫头猫export {log}; 119