Dynamic file name in log4j’s file appender

In your log4j.xml, set the file name as a variable

 	<appender name="my-file-appender" class="org.apache.log4j.FileAppender">
		<param name="file" value="${myFilePath}" />
		<!-- ... --> 

Then in your java code, make sure the following is called before the first getLogger() call

 	System.setProperty("myFilePath", someFilePath); 

Leave a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.