添加LogUtils日志文件自动裁剪功能

This commit is contained in:
2026-04-28 17:05:50 +08:00
parent e337bb7a04
commit d02d57d4dd
3 changed files with 46 additions and 4 deletions

View File

@@ -100,10 +100,52 @@ public class LogUtils {
// 加载当前应用下的所有类的 TAG
addClassTAGList();
loadTAGBeanSettings();
checkAndTrimLogFileSize();
_IsInited = true;
LogUtils.d(TAG, String.format("mapTAGList : %s", mapTAGList.toString()));
}
private static void checkAndTrimLogFileSize() {
if (_mfLogCatchFile == null || !_mfLogCatchFile.exists()) {
return;
}
final long MAX_FILE_SIZE = 6291456L;
final long KEEP_FILE_SIZE = 3145728L;
long fileSize = _mfLogCatchFile.length();
if (fileSize <= MAX_FILE_SIZE) {
return;
}
long needSkip = fileSize - KEEP_FILE_SIZE;
try (FileInputStream fis = new FileInputStream(_mfLogCatchFile);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
FileOutputStream fos = new FileOutputStream(_mfLogCatchFile);
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fos))) {
StringBuilder sb = new StringBuilder();
String line;
long skippedTotal = 0;
final String lineBreak = System.lineSeparator();
while ((line = reader.readLine()) != null) {
byte[] lineBytes = line.getBytes();
skippedTotal += lineBytes.length + lineBreak.getBytes().length;
if (skippedTotal > needSkip) {
sb.append(line).append(lineBreak);
}
}
writer.write(sb.toString());
} catch (IOException e) {
e.printStackTrace();
}
}
public static Map<String, Boolean> getMapTAGList() {
return mapTAGList;
}