last day (15 days later) » 

8:17 PM
0
Q: setting log4j.configurationFile for log4j2

TryingI have different log4j2 xml files for different environments e.g. UAT, staging, prod. So i have named the files as follows: log4j2-development.xml log4j2-uat.xml log4j2-staging.xml log4j2-prod.xml And i am setting the log4j.configurationFile according to the environment. For example in "deve...

 
Using static { System.setProperty("log4j.configurationFile", "log4j2_specialName.xml"); } works fine for me... not sure about the "file:/" prefix.
 
@chuchikaeschtli let me check.
 
Do you get an error or are the configuration files ignored, i mean does Log4j just fall back to the standard configuration?
 
@chuchikaeschtli I think you solution should work. But the issue here is the class where i set the system property it extends another class. And the inside the superclass there is a stmt like private static final Logger LOGGER = LoggerFactory.getLogger(ParentClas.class); which throwing some exception and even though i am setting the configurationFile in the child class. it does not work. Hope you understood.
 
well it definitely should work ;-) otherwise you should provide a more detailed error describtion, because I still don't know whether you are actually getting an error with respect to log4j or whether the configuration is just ignored (which would indicate that your logger is instantiated before your read the configuration) - with respect to your edit: do you use a static initializer block to set the property? A minimal example to reproduce the problem would be useful as well.
 
8:17 PM
@chuchikaeschtli does my edit answer you question?
hi
 
you should definitely provide a minimal example
 
in my child class i am writing:
try {
System.setProperty("log4j.configurationFile", Skeletor.class.getResource("/log4j2_development.xml").toURI().toString());
} catch (Exception e) {
System.out.println(e);
throw new RuntimeException(e);
}
in a method.
 
imho this will not work
 
why?
 
because the property file is loaded after the static logger is created
 
8:20 PM
true
 
so one thing you could try is to use the static initializer as i proposed before
 
Are you talking about static block?
 
yes
sorry
 
nope it will also not work
because the initialization will go to parent then will come to child :)
so again to same problem.
one thing we can do is to may be someway reinitialize the log4j2 after setting the system property.
do you get my point?
 
8:23 PM
do you have any idea to reinitialize it?
 
however i am still not completly sure about the initialization order.... let me think about it for a second
 
sure pls.
 
btw. you could also use the programmatic interface as described here .logging.apache.org/log4j/2.x/manual/configuration.html but i guess this is not a solution....
 
ya
i think somehow we need to re-initialize the log4j2 after setting system property.
 
Not sure about reinitalizing it ... because this would basically mean that you have to throw the existing instance away....
I just checked the issue you mentioned with defining a logger in a superclass. you are right, adding a static block in the child class will not work.
so the only way I see is to add the static initializer block somewhere before actually construct/use the logger. Writing your own ConfigurationFactory would probably also work however I have to admit that I am at my wit's end :-(
 
8:46 PM
Anyways thanks for your help and the effort.
 
you're welcome.
 

last day (15 days later) »