Skip to content

Commit

Permalink
Core&Base: Support LogReserveCount property
Browse files Browse the repository at this point in the history
  • Loading branch information
LinShunKang committed Mar 11, 2019
1 parent 1796f8d commit 080eab1
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 40 deletions.
2 changes: 1 addition & 1 deletion MyPerf4J-ASM/src/test/java/MyPerf4J/PreMainTest.java
Expand Up @@ -45,7 +45,7 @@ private void test(int metricsProcessorType) {
private void prepare(int metricsProcessorType) {
String propertiesFile = "/tmp/MyPerf4J.properties";
System.setProperty(PropertyKeys.PRO_FILE_NAME, propertiesFile);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(propertiesFile);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(propertiesFile, 1);
writer.write("AppName=MyPerf4JTest\n");
writer.write("MetricsProcessorType=" + metricsProcessorType + "\n");
writer.write("IncludePackages=MyPerf4J\n");
Expand Down
Expand Up @@ -30,6 +30,8 @@ public class ProfilingConfig {

private String logRollingTimeUnit;

private int LogReserveCount;

private String recorderMode;

private int backupRecorderCount;
Expand Down Expand Up @@ -136,6 +138,14 @@ public void setLogRollingTimeUnit(String logRollingTimeUnit) {
this.logRollingTimeUnit = logRollingTimeUnit;
}

public int getLogReserveCount() {
return LogReserveCount;
}

public void setLogReserveCount(int logReserveCount) {
LogReserveCount = logReserveCount;
}

public String getRecorderMode() {
return recorderMode;
}
Expand Down
Expand Up @@ -25,6 +25,8 @@ public interface PropertyKeys {

String LOG_ROLLING_TIME_TIME_UNIT = "LogRollingTimeUnit";

String LOG_RESERVE_COUNT = "LogReserveCount";

String RECORDER_MODE = "RecorderMode";

String BACKUP_RECORDERS_COUNT = "BackupRecordersCount";
Expand Down
Expand Up @@ -21,6 +21,8 @@ public interface PropertyValues {

int METRICS_PROCESS_TYPE_INFLUX_DB = 2;

int DEFAULT_LOG_RESERVE_COUNT = 7;

String LOG_ROLLING_TIME_MINUTELY = "MINUTELY";

String LOG_ROLLING_TIME_HOURLY = "HOURLY";
Expand Down
Expand Up @@ -10,16 +10,16 @@ public class AutoRollingLogger implements ILogger {

private final AutoRollingFileWriter writer;

AutoRollingLogger(String logFile, String rollingTimeUnit) {
AutoRollingLogger(String logFile, String rollingTimeUnit, int reserveFileCount) {
switch (rollingTimeUnit.toUpperCase()) {
case PropertyValues.LOG_ROLLING_TIME_HOURLY:
this.writer = new HourlyRollingFileWriter(logFile);
this.writer = new HourlyRollingFileWriter(logFile, reserveFileCount);
break;
case PropertyValues.LOG_ROLLING_TIME_MINUTELY:
this.writer = new MinutelyRollingFileWriter(logFile);
this.writer = new MinutelyRollingFileWriter(logFile, reserveFileCount);
break;
default:
this.writer = new DailyRollingFileWriter(logFile);
this.writer = new DailyRollingFileWriter(logFile, reserveFileCount);
}
}

Expand Down
Expand Up @@ -8,6 +8,8 @@

public final class LoggerFactory {

private static final ProfilingConfig config = ProfilingConfig.getInstance();

private static final Map<String, ILogger> LOGGER_MAP = new HashMap<>();

static {
Expand Down Expand Up @@ -39,7 +41,7 @@ public static synchronized ILogger getLogger(String logFile) {
return logger;
}

logger = new AutoRollingLogger(logFile, ProfilingConfig.getInstance().getLogRollingTimeUnit());
logger = new AutoRollingLogger(logFile, config.getLogRollingTimeUnit(), config.getLogReserveCount());
LOGGER_MAP.put(logFile, logger);
return logger;
}
Expand Down
Expand Up @@ -6,6 +6,7 @@
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Calendar;
import java.util.Date;

/**
Expand All @@ -14,8 +15,12 @@
*/
public abstract class AutoRollingFileWriter {

private static final int MAX_INACTIVITY_EPOCHS = 30;

private final String fileName;

private final int reserveFileCount;

private volatile String rollingFileName;

private volatile BufferedWriter bufferedWriter;
Expand All @@ -25,31 +30,53 @@ public abstract class AutoRollingFileWriter {
private volatile long nextRollingTime;


public AutoRollingFileWriter(String fileName) {
public AutoRollingFileWriter(String fileName, int reserveFileCount) {
Date now = new Date();

this.fileName = fileName;
this.reserveFileCount = reserveFileCount;
this.closed = false;
this.nextRollingTime = getNextRollingTime(now);
this.rollingFileName = formatDateFileName(fileName, now);

File targetFile = new File(fileName);
if (!targetFile.exists()) {
createWriter(targetFile, false);
return;
try {
File targetFile = new File(fileName);
if (!targetFile.exists()) {
createWriter(targetFile, false);
return;
}

Date lastModifiedDate = new Date(targetFile.lastModified());
if (isSameEpoch(now, lastModifiedDate)) {
createWriter(targetFile, true);
return;
}

this.rollingFileName = formatDateFileName(fileName, lastModifiedDate);
rollingFile(now);
} finally {
clean(now, MAX_INACTIVITY_EPOCHS);//尽可能的删除过期的日志文件
}
}

Date lastModifiedDate = new Date(targetFile.lastModified());
if (isSameEpoch(now, lastModifiedDate)) {
createWriter(targetFile, true);
return;
private void clean(Date now, int epochs) {
for (int i = 0; i < epochs; ++i) {
int epochOffset = (-reserveFileCount - 1) - i;
Date date = computeEpochCal(now, epochOffset).getTime();
File file2Del = new File(formatDateFileName(fileName, date));
if (file2Del.exists() && file2Del.isFile()) {
boolean delete = file2Del.delete();
Logger.info("AutoRollingFileWriter.clean(" + now + ", " + epochs + "): delete " + file2Del.getName() + " " + (delete ? "success" : "fail"));
}
}
}

this.rollingFileName = formatDateFileName(fileName, lastModifiedDate);
rollingFile(now);
private long getNextRollingTime(Date now) {
Calendar calendar = computeEpochCal(now, 1);
return calendar.getTime().getTime();
}

abstract long getNextRollingTime(Date now);
abstract Calendar computeEpochCal(Date now, int epochOffset);

abstract String formatDateFileName(String fileName, Date date);

Expand Down Expand Up @@ -87,10 +114,12 @@ private void rollingFile(Date now) {

File file = new File(fileName);
boolean rename = file.renameTo(targetFile);
Logger.info("AutoRollingFileWriter.rollingFile(" + now + "): rename " + fileName + " to " + targetFile.getName() + " " + rename);
Logger.info("AutoRollingFileWriter.rollingFile(" + now + "): rename " + fileName + " to " + targetFile.getName() + " " + (rename ? "success" : "fail"));

createWriter(new File(fileName), false);
rollingFileName = datedFilename;

clean(now, 1);//删除最近一个过期的日志文件
} catch (Exception e) {
Logger.error("AutoRollingFileWriter.rollingFile(" + now + "): rollingFile=" + rollingFileName, e);
}
Expand Down
Expand Up @@ -18,20 +18,20 @@ protected SimpleDateFormat initialValue() {
}
};

public DailyRollingFileWriter(String fileName) {
super(fileName);
public DailyRollingFileWriter(String fileName, int reserveFileCount) {
super(fileName, reserveFileCount);
}

@Override
long getNextRollingTime(Date now) {
Calendar computeEpochCal(Date now, int epochOffset) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.set(Calendar.HOUR_OF_DAY, 0);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.DATE, 1);
return cal.getTime().getTime();
cal.add(Calendar.DATE, epochOffset);
return cal;
}

@Override
Expand Down
Expand Up @@ -18,19 +18,19 @@ protected SimpleDateFormat initialValue() {
}
};

public HourlyRollingFileWriter(String fileName) {
super(fileName);
public HourlyRollingFileWriter(String fileName, int reserveFileCount) {
super(fileName, reserveFileCount);
}

@Override
long getNextRollingTime(Date now) {
Calendar computeEpochCal(Date now, int epochOffset) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.set(Calendar.MINUTE, 0);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.HOUR_OF_DAY, 1);
return cal.getTime().getTime();
cal.add(Calendar.HOUR_OF_DAY, epochOffset);
return cal;
}

@Override
Expand Down
Expand Up @@ -18,8 +18,8 @@ protected SimpleDateFormat initialValue() {
}
};

public MinutelyRollingFileWriter(String fileName) {
super(fileName);
public MinutelyRollingFileWriter(String fileName, int reserveFileCount) {
super(fileName, reserveFileCount);
}

@Override
Expand All @@ -28,13 +28,13 @@ String formatDateFileName(String fileName, Date date) {
}

@Override
long getNextRollingTime(Date now) {
Calendar computeEpochCal(Date now, int epochOffset) {
Calendar cal = Calendar.getInstance();
cal.setTime(now);
cal.set(Calendar.SECOND, 0);
cal.set(Calendar.MILLISECOND, 0);
cal.add(Calendar.MINUTE, 1);
return cal.getTime().getTime();
cal.add(Calendar.MINUTE, epochOffset);
return cal;
}

@Override
Expand Down
Expand Up @@ -33,7 +33,7 @@ public abstract class BaseTest {
@BeforeClass
public static void init() {
System.setProperty(PropertyKeys.PRO_FILE_NAME, TEMP_FILE);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(TEMP_FILE);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(TEMP_FILE, 1);
writer.write("AppName=" + APP_NAME + "\n");
writer.write("MetricsProcessorType=" + METRICS_PROCESSOR_TYPE + "\n");
writer.write("IncludePackages=" + INCLUDE_PACKAGES + "\n");
Expand Down
Expand Up @@ -12,7 +12,8 @@ public class ILoggerTest {
@Test
public void test() {
ProfilingConfig.getInstance().setLogRollingTimeUnit(PropertyValues.LOG_ROLLING_TIME_MINUTELY);

ProfilingConfig.getInstance().setLogReserveCount(PropertyValues.DEFAULT_LOG_RESERVE_COUNT);

test(LoggerFactory.getLogger("/tmp/testLogger.log"));
test(LoggerFactory.getLogger(PropertyValues.NULL_FILE));
}
Expand Down
Expand Up @@ -13,13 +13,13 @@ public class RollingFileWriterTest {

@Test
public void test() {
AutoRollingFileWriter writer1 = new MinutelyRollingFileWriter("/tmp/test1.log");
AutoRollingFileWriter writer1 = new MinutelyRollingFileWriter("/tmp/test1.log", 1);
test(writer1);

AutoRollingFileWriter writer2 = new HourlyRollingFileWriter("/tmp/test2.log");
AutoRollingFileWriter writer2 = new HourlyRollingFileWriter("/tmp/test2.log", 1);
test(writer2);

AutoRollingFileWriter writer3 = new DailyRollingFileWriter("/tmp/test3.log");
AutoRollingFileWriter writer3 = new DailyRollingFileWriter("/tmp/test3.log", 1);
test(writer3);
}

Expand Down
Expand Up @@ -149,6 +149,7 @@ private boolean initProfilingConfig() {
}
config.setThreadMetricsFile(MyProperties.getStr(PropertyKeys.THREAD_METRICS_FILE, PropertyValues.NULL_FILE));
config.setLogRollingTimeUnit(MyProperties.getStr(PropertyKeys.LOG_ROLLING_TIME_TIME_UNIT, PropertyValues.LOG_ROLLING_TIME_DAILY));
config.setLogReserveCount(MyProperties.getInt(PropertyKeys.LOG_RESERVE_COUNT, PropertyValues.DEFAULT_LOG_RESERVE_COUNT));

config.setRecorderMode(MyProperties.getStr(PropertyKeys.RECORDER_MODE, PropertyValues.RECORDER_MODE_ROUGH));
config.setBackupRecorderCount(MyProperties.getInt(PropertyKeys.BACKUP_RECORDERS_COUNT, PropertyValues.MIN_BACKUP_RECORDERS_COUNT));
Expand Down
Expand Up @@ -52,7 +52,7 @@ public boolean initOther() {
private void initPropertiesFile(int metricsProcessorType) {
String propertiesFile = "/tmp/MyPerf4J.properties";
System.setProperty(PropertyKeys.PRO_FILE_NAME, propertiesFile);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(propertiesFile);
AutoRollingFileWriter writer = new MinutelyRollingFileWriter(propertiesFile, 1);
writer.write("AppName=MyPerf4JTest\n");
writer.write("IncludePackages=MyPerf4J\n");
writer.write("MetricsProcessorType=" + metricsProcessorType + "\n");
Expand Down
Expand Up @@ -36,5 +36,6 @@ private void init() {
config.setBufferPoolMetricsFile("/tmp/metrics.log");
config.setThreadMetricsFile("/tmp/metrics.log");
config.setLogRollingTimeUnit("DAILY");
config.setLogReserveCount(7);
}
}

0 comments on commit 080eab1

Please sign in to comment.