Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4J2 - assigning file appender filename at runtime

Tags:

java

log4j

log4j2

I have a log4j2.xml config file in the class path. One of the appenders is a File appender, and I would like to set the target file name at run time in the Java application.

According to the docs I should be able to use a double "$" and a context prefix in the log4j2.xml file:

<appenders>     <File name="MyFile" fileName="$${sys:logFilename}">         <PatternLayout pattern="%-4r %d{${datestamp}} [%t] %-5level %logger{36} - %msg%n"/>     </File> </appenders> 

where the "sys" prefix indicates that the Configurator will lookup the property "logFilename" in the system properties. So in the application, I call (rather early on):

System.setProperty("logFilename", filename); 

I have also turned on auto-reconfiguration for log4j2 in the xml file:

<configuration status="debug" monitorInterval="5">> 

Unfortunately, this has no effect whatsoever, and the log file is never created. Some of the log4j2 status output is below:

2013-02-13 15:36:37,574 DEBUG Calling createAppender on class org.apache.logging.log4j.core.appender.FileAppender for element File with params(fileName="${sys:logFilename}", append="null", locking="null", name="MyFile", immediateFlush="null", suppressExceptions="null", bufferedIO="null", PatternLayout(%-4r %d{yyyy-MM-dd/HH:mm:ss.SSS/zzz} [%t] %-5level %logger{36} - %msg%n), null)

2013-02-13 15:36:37,576 DEBUG Starting FileManager ${sys:logFilename}

How can I have the value of "fileName" in the File Appender be set at run time? Alternatively, how can I simply add a new File Appender to the root logger at run time? In Log4j 2.0 most of the API to change the configuration is hidden.

like image 465
user84756 Avatar asked Feb 13 '13 20:02

user84756


People also ask

How do you give a dynamic file name in the Appender in log4j2 XML?

System. setProperty("logfilename", "a_cool_logname"); Once that is set you can go ahead and get your loggers as normal and they will log to the dynamic file (be careful of those static Loggers that create loggers before your main method executes).

How do you set log file path in Log4j properties dynamically?

System. setProperty("{my. log", "C:/logfile. log");

What is rolling file Appender in log4j2?

Log4j2 RollingFileAppender is an OutputStreamAppender that writes log messages to files, following a configured triggering policy about when a rollover (backup) should occur. It also has a configured rollover strategy about how to rollover the file.


1 Answers

h/t rgoers The FileAppender doesn't support two dollar signs on the file name as the file is opened when the appender is started. What you are indicating with two dollar signs is that you want - potentially - a different file name for each event.

With a single $ (as in ${sys:logFilename}), the system will look for property "logFilename" in the system properties.

Thus, the log4j2.xml should have:

<appenders>     <File name="MyFile" fileName="${sys:logFilename}">         <PatternLayout pattern="%-4r %d{${datestamp}} [%t] %-5level %logger{36} - %msg%n"/>     </File> </appenders> 

The Java application should set the system property:

System.setProperty("logFilename", filename); 

and reconfigure the logger:

org.apache.logging.log4j.core.LoggerContext ctx =     (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false); ctx.reconfigure(); 

This produces the desired behavior.

like image 57
user84756 Avatar answered Sep 22 '22 18:09

user84756