瀏覽代碼

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

master
Carpenter Lee GitHub 6 年之前
父節點
當前提交
ba10d10aa9
沒有發現已知的金鑰在資料庫的簽署中 GPG Key ID: 4AEE18F83AFDEB23
共有 2 個文件被更改,包括 31 次插入7 次删除
  1. +30
    -6
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/DateFileLogHandler.java
  2. +1
    -1
      sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java

+ 30
- 6
sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/DateFileLogHandler.java 查看文件

@@ -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();


+ 1
- 1
sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java 查看文件

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


Loading…
取消
儲存