elvishew/xLog

如果当天日志文件被删了,貌似就无法写文件了

Closed this issue · 4 comments

w2xx commented

貌似目前写文件时候没有判断文件是否存在,如果不存在也没有新建文件。只能杀进程重新初始化

有解决吗?我这边上传日志到服务器,删除文件后发现没有创建新

我这边自定义Printer,在doPrintln里做了下判断

private void doPrintln(long timeMillis, int logLevel, String tag, String msg) {
String lastFileName = this.writer.getLastFileName();
if (lastFileName == null || this.fileNameGenerator.isFileNameChangeable()) {
String newFileName = this.fileNameGenerator.generateFileName(logLevel, System.currentTimeMillis());
if (newFileName == null || newFileName.trim().length() == 0) {
throw new IllegalArgumentException("File name should not be empty.");
}

        if (!newFileName.equals(lastFileName)) {
            if (this.writer.isOpened()) {
                this.writer.close();
            }

            this.cleanLogFilesIfNecessary();
            if (!this.writer.open(newFileName)) {
                return;
            }

            lastFileName = newFileName;
        }
    }

    File lastFile = this.writer.getFile();

//这里做了判断处理
if (!lastFile.exists()) {
this.writer.open(lastFile.getName());
}

    if (this.backupStrategy.shouldBackup(lastFile)) {
        this.writer.close();
        File backupFile = new File(this.folderPath, lastFileName + ".bak");
        if (backupFile.exists()) {
            backupFile.delete();
        }

        lastFile.renameTo(backupFile);
        if (!this.writer.open(lastFileName)) {
            return;
        }
    }

    String flattenedLog = this.flattener.flatten(timeMillis, logLevel, tag, msg).toString();
    this.writer.appendLog(flattenedLog);
}
w2xx commented

@vip110880 你是直接导入的源码吧。我没有导入源码,不知道有没有其他方式优化下

Fixed in v1.8.0, close this issue