Skip to content

Commit

Permalink
Bug fix: fix wrong log file name checking (alibaba#182)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarpenterLee committed Oct 16, 2018
1 parent 52d16e6 commit ba10d10
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,12 @@

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;

Expand Down Expand Up @@ -64,17 +69,27 @@ public void flush() {

@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);
}
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);
Expand All @@ -83,7 +98,13 @@ public void setFormatter(Formatter newFormatter) {

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) {

Expand All @@ -93,7 +114,10 @@ private boolean logFileExits() {

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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ protected static Handler makeLogger(String logName, Logger heliumRecordLog) {
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) {
Expand Down

0 comments on commit ba10d10

Please sign in to comment.