Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change configuration file of log4j2 at runtime?

i would like to turn of my debug statements at runtime in log4j2. According to the documentation we can do this. I kept my log4j.xml file in default package and then made jar out of it. Since I cannot modify jar i specified xml file using Dlog4j.configuration=/path/to/log4j.xml. However,this is not working. I Tried everything using file:// uri to all the combination, still it is not picking the xml.

like image 697
Guru Avatar asked Sep 19 '13 22:09

Guru


People also ask

How do I change the log4j2 properties at runtime?

Use LogManager. resetConfiguration(); to clear the current config and configure it again. Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config).

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

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


1 Answers

The system property to specify the config file location is different from log4j-1.x. In log4j2, the property is "log4j.configurationFile".

So if the config is in a jar file you would use:

-Dlog4j.configurationFile=jar:file:///C:/path/to/jarfile.jar!/path/to/log4j2.xml

(I assume you have the monitorInterval set in your configuration as documented here: http://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticReconfiguration)

like image 112
Remko Popma Avatar answered Sep 19 '22 23:09

Remko Popma