旧日志文件被覆盖了
Closed this issue · 6 comments
我设置了fileNameGenerator和backupStrategy,结果日志文件被不断互相覆盖了。代码是这样的:
Printer filePrinter = new FilePrinter // Printer that print the log to the file system
.Builder(path) // Specify the path to save log file
.fileNameGenerator(new FileNameGenerator() {
ThreadLocal<SimpleDateFormat> mLocalDateFormat = new ThreadLocal<SimpleDateFormat>() {
@Override
protected SimpleDateFormat initialValue() {
return new SimpleDateFormat("yyyy-MM-dd", Locale.US);
}
};
@Override
public boolean isFileNameChangeable() {
return false;
}
@Override
public String generateFileName(int logLevel, long timestamp) {
SimpleDateFormat sdf = mLocalDateFormat.get();
sdf.setTimeZone(TimeZone.getDefault());
return "log-" + sdf.format(new Date(timestamp)) + ".txt";
}
})
.backupStrategy(new FileSizeBackupStrategy(4 << 20)) // 4MB
.logFlattener(new PatternFlattener("{d yyyy-MM-dd HH:mm:ss.SSS} " + Prefix + "{l}/{m}"))
.build();
XLog.init(LogLevel.ALL, conf, new AndroidPrinter(), filePrinter);
期待:
日志文件名是2019-03-04.txt
,如果超过4MB,则自动移动到2019-03-04.txt.bak
并重开一个2019-03-04.txt
文件, 如果新文件再超过4MB,则自动移动到2019-03-04.txt.2.bak
、2019-03-04.txt.3.bak
等
实际:
日志文件名是2019-03-04.txt
,如果超过4MB,则自动移动到2019-03-04.txt.bak
并重开一个2019-03-04.txt
文件, 如果新文件再超过4MB,则自动移动到2019-03-04.txt.bak
并重开2019-03-04.txt
文件。
注意:这时候旧的2019-03-04.txt.bak
被第二个txt文件覆盖了,以后第三个txt会覆盖第二个,依次覆盖下去。也就是,只要fileNameGenerator还返回相同的文件名,那么始终只有1个bak文件,最多只会留下4+4=8MB的日志文件。
@gdh1995 It is designed to be. If you want to keep more logs, just enlarge the size of FileSizeBackupStrategy
, and if you want to keep all logs, just use NeverBackupStrategy
.
呃,那强烈建议在readme里加warning。。。
我看了看发布的包里的backupStrategy()和FileSizeBackupStrategy的注释,里边都没说具体的行为是会覆盖,但我觉得warning是很有必要的。
@gdh1995 You are right, we should keep all the backup files. Would you create a pull request?
Sorry, but I have a heavy job in the daytime, and in the night I would rather write my personal Chrome extension project, so I have no enough rest time in recent weeks.
Since release 1.9.0, multiple backup files is supported now. Use FileSizeBackupStrategy2
or extend AbstractBackupStrategy
instead of using BackupStrategy
, and README is updated too.