You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by "Vernier, Dominique (DVernier)" <dv...@relaystar.com> on 2004/06/21 09:30:30 UTC

Topic related to FAQ: How do I configure log4j so that programs in multiple JVMs can log to the same file and rollover daily

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  

Re: Topic related to FAQ: How do I configure log4j so that programs in multiple JVMs can log to the same file and rollover daily

Posted by Ceki Gülcü <ce...@qos.ch>.
Hi Dominique,

Ah, you can use variable substitution to achieve the same effect with less 
effort. You are aware of variable substitution in config files, right? If 
not, see pages 53-54 of the complete log4j manual. See also the javadocs 
for PropertyConfigurator. DOMConfigurator behaves much in the same way. 
JoranConfigurator (log4j 1.3) offers even more powerful ways of dealing 
with variable substiution.

HTH,


At 09:30 AM 6/21/2004, Vernier, Dominique (DVernier) wrote:

>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

-- 
Ceki Gülcü

      For log4j documentation consider "The complete log4j manual"
      ISBN: 2970036908 http://www.qos.ch/shop/products/clm_t.jsp  



---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-dev-help@logging.apache.org