英文:
Log4j's rollingPolicy.FileNamePattern is also changing name of files that are zipped in
问题
我已经使用了log4j的滚动策略来压缩达到一定大小的文件。以下是有效的log4j属性设置:
log4j.appender.FILE=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.FILE.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.FILE.rollingPolicy.maxIndex=13
log4j.appender.FILE.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.FILE.triggeringPolicy.MaxFileSize=80
log4j.appender.FILE.rollingPolicy.FileNamePattern=log/projectlog_${current.date.time}.%i.log.gz
log4j.appender.FILE.rollingPolicy.ActiveFileName=log/project_log_${current.date.time}.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
但问题在于,在生成压缩文件后,它还会将压缩gz文件中存在的文件重命名为gz文件的名称。
对于我的情况,我不希望log4j重命名压缩gz存档中存在的文件。是否有任何方法可以限制log4j在压缩时不更改文件名。
英文:
I have used log4j's rolling policy for compressing files that reach to certain amount in size. Below log4j properties are working properly.
log4j.appender.FILE=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.FILE.rollingPolicy=org.apache.log4j.rolling.FixedWindowRollingPolicy
log4j.appender.FILE.rollingPolicy.maxIndex=13
log4j.appender.FILE.triggeringPolicy=org.apache.log4j.rolling.SizeBasedTriggeringPolicy
log4j.appender.FILE.triggeringPolicy.MaxFileSize=80
log4j.appender.FILE.rollingPolicy.FileNamePattern=log/projectlog_${current.date.time}.%i.log.gz
log4j.appender.FILE.rollingPolicy.ActiveFileName=log/project_log_${current.date.time}.log
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=%m%n
But problem here is after it generates a compressed file, it also renames the file which is present in the compressed gz file with the name of gz file.
For my scenario, I don't want log4j to rename file that is present in the compressed gz archive. Is there any way by which we can restrict log4j to not to change file names which it is compressing.
答案1
得分: 4
无法使用由@UmarTahir提供的代码来实现此功能。
正如您在FixedWindowRollingPolicy
类的源代码中所看到的,在实际处理rollover
的方法中,他们首先指示RollingFileAppender
同步地重命名文件,然后在需要时异步地进行压缩:
public RolloverDescription rollover(final String currentFileName) {
// ...
}
返回的RolloverDescription
将在RollingFileAppender
的rollover
方法中用于处理实际的翻转过程:
public boolean rollover() {
// ...
}
如果您需要这种特殊行为,也许您可以创建自己的滚动策略,并尝试以最适合您要求的方式实现类似于FixedWindowRollingPolicy
中所做的操作,并配置log4j以使用这种新的滚动策略。
话虽如此,请注意gzip只是对数据进行压缩,并没有实际的文件名或文件条目,只有压缩的数据。如果您使用gzip
工具,它会将要压缩的存档名称,将数据压缩,并将结果存储在一个带有后缀(通常是.gz
)的新文件中。这就是log4j在其代码中试图模仿的行为。
英文:
You cannot with the code provided by log4j @UmarTahir.
As you can see in the source code of the FixedWindowRollingPolicy
class, in the method that actually handles the rollover
, they first instruct RollingFileAppender
to rename, synchronously, the file, and then compress it, asynchronously, if necessary:
public RolloverDescription rollover(final String currentFileName) {
if (maxIndex >= 0) {
int purgeStart = minIndex;
if (!explicitActiveFile) {
purgeStart++;
}
if (!purge(purgeStart, maxIndex)) {
return null;
}
StringBuffer buf = new StringBuffer();
formatFileName(new Integer(purgeStart), buf);
String renameTo = buf.toString();
String compressedName = renameTo;
Action compressAction = null;
if (renameTo.endsWith(".gz")) {
renameTo = renameTo.substring(0, renameTo.length() - 3);
compressAction =
new GZCompressAction(
new File(renameTo), new File(compressedName), true);
} else if (renameTo.endsWith(".zip")) {
renameTo = renameTo.substring(0, renameTo.length() - 4);
compressAction =
new ZipCompressAction(
new File(renameTo), new File(compressedName), true);
}
FileRenameAction renameAction =
new FileRenameAction(
new File(currentFileName), new File(renameTo), false);
return new RolloverDescriptionImpl(
currentFileName, false, renameAction, compressAction);
}
return null;
}
The returned RolloverDescription
will be used in the RollingFileAppender
rollover
method to handle the actual rollover process:
public boolean rollover() {
//
// can't roll without a policy
//
if (rollingPolicy != null) {
Exception exception = null;
synchronized (this) {
//
// if a previous async task is still running
//}
if (lastRolloverAsyncAction != null) {
//
// block until complete
//
lastRolloverAsyncAction.close();
//
// or don't block and return to rollover later
//
//if (!lastRolloverAsyncAction.isComplete()) return false;
}
try {
RolloverDescription rollover = rollingPolicy.rollover(getFile());
if (rollover != null) {
if (rollover.getActiveFileName().equals(getFile())) {
closeWriter();
boolean success = true;
if (rollover.getSynchronous() != null) {
success = false;
try {
success = rollover.getSynchronous().execute();
} catch (Exception ex) {
exception = ex;
}
}
if (success) {
if (rollover.getAppend()) {
fileLength = new File(rollover.getActiveFileName()).length();
} else {
fileLength = 0;
}
if (rollover.getAsynchronous() != null) {
lastRolloverAsyncAction = rollover.getAsynchronous();
new Thread(lastRolloverAsyncAction).start();
}
setFile(
rollover.getActiveFileName(), rollover.getAppend(),
bufferedIO, bufferSize);
} else {
setFile(
rollover.getActiveFileName(), true, bufferedIO, bufferSize);
if (exception == null) {
LogLog.warn("Failure in post-close rollover action");
} else {
LogLog.warn(
"Exception in post-close rollover action", exception);
}
}
} else {
Writer newWriter =
createWriter(
createFileOutputStream(
rollover.getActiveFileName(), rollover.getAppend()));
closeWriter();
setFile(rollover.getActiveFileName());
this.qw = createQuietWriter(newWriter);
boolean success = true;
if (rollover.getSynchronous() != null) {
success = false;
try {
success = rollover.getSynchronous().execute();
} catch (Exception ex) {
exception = ex;
}
}
if (success) {
if (rollover.getAppend()) {
fileLength = new File(rollover.getActiveFileName()).length();
} else {
fileLength = 0;
}
if (rollover.getAsynchronous() != null) {
lastRolloverAsyncAction = rollover.getAsynchronous();
new Thread(lastRolloverAsyncAction).start();
}
}
writeHeader();
}
return true;
}
} catch (Exception ex) {
exception = ex;
}
}
if (exception != null) {
LogLog.warn(
"Exception during rollover, rollover deferred.", exception);
}
}
return false;
}
If you need this special behavior, maybe you can create your own rolling policy, and try to implement something similar to what is done in FixedWindowRollingPolicy
, in a way best fits your requirements, and configure log4j to use this new rolling policy.
That been said, be aware that gzip means just compressing your data, there is no actual filename or file entry, just data compressed. If you use the gzip
tool, it takes the name of the archive to compress, compress the data, and store the result in a new file with a suffix, typically .gz
. This is the behavior log4j is trying to mimic in their code.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论