Hi,
I had also this issue and I didn't find a way to share the same log file between different JVM. This come from the fact that a file can't be open twice in append mode. Then I use two different files, but in this case log4j doesn't provide a way to create different file name for the same package running in different JVMs.
To solve it I create a new version of the FileAppender called SPPFileAppender (SystemProperty Postfix). This appender will add to the file name the value of a give system property attribute name. Then I create a new RollFileAppender and DailyFileAppender called respectively SPPRollFileAppender and SPPDailyFileAppender which extend SPPFileAppender instead of FileAppender.
Then when I start the JVM with a given -D value to differentiate all JVMs or use any other existing system property, it can be also the startup JVM time
My log4j.xml became:
<appender name="ROLLAppenderRise" class="org.apache.log4j.SPPRollingFileAppender">
<param name="File" value="test"/>
<param name="SystemPropertyName" value="mysystemproperty"/>
<param name="MaxFileSize" value="5000KB"/>
<param name="MaxBackupIndex" value="10"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d %-5p [%t] %C{2} (%F:%L) - %m%n"/>
</layout>
</appender>
Let's say that the system property 'mysystemproperty' has for value 'jvm1' on one running JVM and 'jvm2' on the other then the file names are test.jvm1 and test.jvm2.
And the SPPFileAppender activeOption method:
public void activateOptions() {
if (fileName != null) {
try {
String postfix = null;
if (systemPropertyName != null) {
String propertyValue = System.getProperty(systemPropertyName);
if (propertyValue != null)
fileName += "." + System.getProperty(systemPropertyName);
}
setFile(fileName, fileAppend, bufferedIO, bufferSize);
} catch (java.io.IOException e) {
errorHandler.error(
"setFile(" + fileName + "," + fileAppend + ") call failed.",
e,
ErrorCode.FILE_OPEN_FAILURE);
}
} else {
//LogLog.error("File option not set for appender ["+name+"].");
LogLog.warn("File option not set for appender [" + name + "].");
LogLog.warn("Are you using FileAppender instead of ConsoleAppender?");
}
}
I have also getter and setter for the systemPropertyName variable.
And now I have nomore problem with multiple JVM's running the same packages. This solution can be use for other issues and can maybe be a part of a future FileAppender version.
Regards
Dominique
