优化日志分割

hdrplus
Matthew 12 months ago
parent 1532a14c0f
commit 7d925b78b6

@ -155,7 +155,7 @@ public class MpMasterService extends Service {
logger.addHandler(logcatHandler);
}
FileHandler fileHandler = null;
RotatingHandler rotatingHandler = null;
try {
String appPath = MicroPhotoContext.buildMasterAppDir(getApplicationContext());
String logPath = appPath + "logs";
@ -164,17 +164,11 @@ public class MpMasterService extends Service {
fi.mkdirs();
}
File logFile = new File(fi, "log.%g.txt");
int logFileLimit = BuildConfig.DEBUG ? (16 * 1024) : (2 * 1024 * 1024);
int logFileCount = BuildConfig.DEBUG ? 4 : 128;
File logFile = new File(fi, "log.txt");
fileHandler = new FileHandler(logFile.getAbsolutePath(), logFileLimit, logFileCount, true);
rotatingHandler = new RotatingHandler(logFile.getAbsolutePath(), logFormatter);
fileHandler.setLevel(Level.ALL);//级别为ALL记录所有消息
// fileHandler.
fileHandler.setFormatter(logFormatter);
logger.addHandler(fileHandler);
logger.addHandler(rotatingHandler);
} catch (Throwable e) {
System.out.println("Failed to create directory:" + e.getMessage());
@ -936,7 +930,11 @@ public class MpMasterService extends Service {
try {
Method method = subscriptionManager.getClass().getDeclaredMethod("setDefaultDataSubId", int.class);
method.invoke(subscriptionManager, subId);
try {
method.invoke(subscriptionManager, subId);
} catch (Exception ex) {
ex.printStackTrace();
}
TelephonyManager telephonyManager = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
telephonyManager.setDataEnabled(true);

@ -0,0 +1,125 @@
package com.xypower.mpmaster;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.StreamHandler;
public class RotatingHandler extends StreamHandler {
private long mPrevMillis;
private String mLogFilePath;
public RotatingHandler(String filePath, Formatter formatter) {
// super(null, formatter);
mPrevMillis = 0;
mLogFilePath = filePath;
File file = new File(mLogFilePath);
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
if (file.exists()) {
long millis = file.lastModified();
millis -= millis % 1000;
Date dt = new Date(millis);
dt.setHours(0);
dt.setMinutes(0);
mPrevMillis = dt.getTime();
}
FileOutputStream out = null;
try {
out = new FileOutputStream(file, true);
} catch (Exception ex) {
}
setFormatter(formatter);
setOutputStream(out);
}
@Override
public void publish(LogRecord record) {
if (!isLoggable(record)) {
return;
}
if (mPrevMillis == 0) {
long millis = record.getMillis();
millis -= millis % 1000;
Date dt = new Date(millis);
dt.setHours(0);
dt.setMinutes(0);
mPrevMillis = dt.getTime();
} else {
long millis = record.getMillis();
if (millis - mPrevMillis >= 86400000) {
// Date changed
Date dt = new Date(mPrevMillis);
SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
String str = fmt.format(dt);
File file = new File(mLogFilePath);
File parentFile = file.getParentFile();
String fileName = file.getName();
String baseName = null;
String extName = null;
int pos = fileName.lastIndexOf('.');
if (pos > 0) {
baseName = fileName.substring(pos);
extName = fileName.substring(pos);
} else {
baseName = fileName;
extName = "";
}
String newFileName = fileName + "-" + str;
File newFile = null;
int idx = 1;
do {
newFile = new File(parentFile, newFileName + extName);
if (newFile.exists()) {
try {
file.renameTo(newFile);
} catch (Exception ex) {
ex.printStackTrace();
}
break;
}
newFileName = fileName + "-" + str + "." + Integer.toString(idx);
idx ++;
} while (true);
FileOutputStream out = null;
try {
out = new FileOutputStream(mLogFilePath, true);
} catch (Exception ex) {
ex.printStackTrace();
}
setOutputStream(out);
dt = new Date(millis);
dt.setHours(0);
dt.setMinutes(0);
mPrevMillis = dt.getTime();
}
}
super.publish(record);
flush();
}
}
Loading…
Cancel
Save