Bug fix: fix wrong log file name checking (#182)

This commit is contained in:
Carpenter Lee 2018-10-16 17:03:22 +08:00 committed by GitHub
parent 52d16e62f2
commit ba10d10aa9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 7 deletions

View File

@ -27,7 +27,12 @@ import java.util.logging.LogRecord;
class DateFileLogHandler extends Handler {
private final SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
private final ThreadLocal<SimpleDateFormat> dateFormatThreadLocal = new ThreadLocal<SimpleDateFormat>() {
@Override
public SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd");
}
};
private volatile FileHandler handler;
@ -64,10 +69,13 @@ class DateFileLogHandler extends Handler {
@Override
public void publish(LogRecord record) {
synchronized (monitor) {
if (endDate < record.getMillis() || !logFileExits()) { rotateDate(); }
if (shouldRotate(record)) {
synchronized (monitor) {
if (shouldRotate(record)) {
rotateDate();
}
}
}
if (System.currentTimeMillis() - startDate > 25 * 60 * 60 * 1000) {
String msg = record.getMessage();
record.setMessage("missed file rolling at: " + new Date(endDate) + "\n" + msg);
@ -75,6 +83,13 @@ class DateFileLogHandler extends Handler {
handler.publish(record);
}
private boolean shouldRotate(LogRecord record) {
if (endDate <= record.getMillis() || !logFileExits()) {
return true;
}
return false;
}
@Override
public void setFormatter(Formatter newFormatter) {
super.setFormatter(newFormatter);
@ -83,7 +98,13 @@ class DateFileLogHandler extends Handler {
private boolean logFileExits() {
try {
File logFile = new File(pattern);
SimpleDateFormat format = dateFormatThreadLocal.get();
String fileName = pattern.replace("%d", format.format(new Date()));
// When file count is not 1, the first log file name will end with ".0"
if (count != 1) {
fileName += ".0";
}
File logFile = new File(fileName);
return logFile.exists();
} catch (Throwable e) {
@ -93,7 +114,10 @@ class DateFileLogHandler extends Handler {
private void rotateDate() {
this.startDate = System.currentTimeMillis();
if (handler != null) { handler.close(); }
if (handler != null) {
handler.close();
}
SimpleDateFormat format = dateFormatThreadLocal.get();
String newPattern = pattern.replace("%d", format.format(new Date()));
// Get current date.
Calendar next = Calendar.getInstance();

View File

@ -96,7 +96,7 @@ public class LogBase {
String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid();
Handler handler = null;
try {
handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 1, true);
handler = new DateFileLogHandler(fileName + ".%d", 1024 * 1024 * 200, 4, true);
handler.setFormatter(formatter);
handler.setEncoding(LOG_CHARSET);
} catch (IOException e) {