添加LogUtils日志文件自动裁剪功能
This commit is contained in:
@@ -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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user