Procházet zdrojové kódy

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

master
Carpenter Lee GitHub před 6 roky
rodič
revize
ba10d10aa9
V databázi nebyl nalezen žádný známý klíč pro tento podpis ID GPG klíče: 4AEE18F83AFDEB23
2 změnil soubory, kde provedl 31 přidání a 7 odebrání
  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 Zobrazit soubor

@@ -27,7 +27,12 @@ import java.util.logging.LogRecord;


class DateFileLogHandler extends Handler { 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; private volatile FileHandler handler;


@@ -64,10 +69,13 @@ class DateFileLogHandler extends Handler {


@Override @Override
public void publish(LogRecord record) { 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) { if (System.currentTimeMillis() - startDate > 25 * 60 * 60 * 1000) {
String msg = record.getMessage(); String msg = record.getMessage();
record.setMessage("missed file rolling at: " + new Date(endDate) + "\n" + msg); record.setMessage("missed file rolling at: " + new Date(endDate) + "\n" + msg);
@@ -75,6 +83,13 @@ class DateFileLogHandler extends Handler {
handler.publish(record); handler.publish(record);
} }


private boolean shouldRotate(LogRecord record) {
if (endDate <= record.getMillis() || !logFileExits()) {
return true;
}
return false;
}

@Override @Override
public void setFormatter(Formatter newFormatter) { public void setFormatter(Formatter newFormatter) {
super.setFormatter(newFormatter); super.setFormatter(newFormatter);
@@ -83,7 +98,13 @@ class DateFileLogHandler extends Handler {


private boolean logFileExits() { private boolean logFileExits() {
try { 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(); return logFile.exists();
} catch (Throwable e) { } catch (Throwable e) {


@@ -93,7 +114,10 @@ class DateFileLogHandler extends Handler {


private void rotateDate() { private void rotateDate() {
this.startDate = System.currentTimeMillis(); 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())); String newPattern = pattern.replace("%d", format.format(new Date()));
// Get current date. // Get current date.
Calendar next = Calendar.getInstance(); Calendar next = Calendar.getInstance();


+ 1
- 1
sentinel-core/src/main/java/com/alibaba/csp/sentinel/log/LogBase.java Zobrazit soubor

@@ -96,7 +96,7 @@ public class LogBase {
String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid(); String fileName = LogBase.getLogBaseDir() + logName + ".pid" + PidUtil.getPid();
Handler handler = null; Handler handler = null;
try { 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.setFormatter(formatter);
handler.setEncoding(LOG_CHARSET); handler.setEncoding(LOG_CHARSET);
} catch (IOException e) { } catch (IOException e) {


Načítá se…
Zrušit
Uložit