I would like to override log4j configuration at runtime with DOMConfigurator.configureAndWatch() function. It’s work when the string name of file parameter is hard-coded. For example : DOMConfigurator.configureAndWatch("C:/some/path/log4j.xml");
But my log4j.xml is in EAR file (in a JAR in the lib directory) and how can I access it at runtime ?
I tested DOMConfigurator.configureAndWatch("log4j.xml"); but it seems that Glassfish is trying to search file from ${GLASSFISH_HOME}/glassfish/domains/domain1/config where is located my old log4j.xml file to override…so actually my code load, reset and reload configuration from the same file…
I’m just looking for a way to load my log4j.xml inside my EAR
Thanks for ideas
Invoke
getClass().getResourceAsStream("/log4j.xml")on a class in your JAR/EAR to read thelog4j.xmlfile located at the root of the JAR/EAR. Then, pass the resultingInputStreamtoDOMConfigurator.doConfigure(InputStream, LoggerRepository)Alternatively, you can invoke
DOMConfigurator.configure(getClass().getResource("/log4j.xml")), wheregetClass()returns aClassobject in your JAR/EAR.