You cannot with the code provided by log4j @UmarTahir.
As you can see in the source code of the FixedWindowRollingPolicyclass, 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.