I am writing an application that will be using log4j2, and when all is said in done it will be put in one JAR file.
I need to be able to alter the properties of log4j2 though.
Right now I have a method that uses ConfigurationBuilder to build a configuration and then at the end it uses ConfigurationBuilder's writeXmlConfiguration to write the new configuration to log4j2.xml ....
however this is not going to work when everything is done and wrapped up in a JAR file as I am not going to want to modify the xml file in the JAR file if that is even possible.
So what I am wondering is, after I have my ConfigurationBuilder configuration, is there a way I can just programatically re-initialize log4j2 with this configuration instead of using the original log4j2.xml file? At this point it would be using the ConfigurationBuilder configuration instead of the xml file.
Is this possible?
If not... is there a different way to accomplish this? I know I can write a class to extend ConfigurationBuilder and make it a plugin right, with this method would I not even need an xml file at all?
If so, maybe this would be the best way to do this and I could have a ConfigurationBuilder extended class that would set up a configuration, initialize log4j2 with it, and perhaps even have methods to alter the configuration and re-initialize during run time?
So basically my questions are...
After using ConfigurationBuidler to make a configuration, can I programatically make log4j2 use this new configuration instead of the xml file, without altering the xml file?
If I use Configuration builder extended class and make it a plug-in, can I then eliminate the need for even having an xml file and just use this class as a plugin to programatically initialize log4j2 with the Configuration Builder class setting?
If I use the above method, can I have methods in that class to alter the current used configuration of log4j2 programatically?
Out of the two methods above, if both of them work, (updating log4j2 configuration programatically without altering the xml file and using ConfigurationBuilder extended class as a plug-in), then which would be better for log4j2 based application that is wrapped up in a JAR file for deployment?
thanks for any help!
I have written this custom configuration factory extended class to use log4j2 without a configuration file while I have been waiting on replies here and stack overflow....
according to ConfigurationSource API, I believe passing null as the configuration source means that there will be no "real" configuration file, aka do everything programatically...
after I have these methods writtin, the createConfiguration, and getConfiguration, I am kind of confused on how to go about using this plugin/Configuration Factory to set up my log4j configuration.
According to Apache Instructions, it says -
The below example overrides the getConfiguration() method to return a Configuration created by the ConfigurationBuilder. This will cause the Configuration to automatically be hooked into Log4j when the LoggerContext is created. In the example below, because it specifies a supported type of "*" it will override any configuration files provided.
I am not sure what it means by calling the getConfiguration method will cause the configuration to automatically be hooked into log4j when the LoggerContext is created? When is /should the LoggerContext be created? the getConfiguration method is asking for a LoggerContext as an input variable? Do I create a LoggerContext in my main class and pass it into this getConfiguration method and that will do the trick?
I dont really think so because looking at that code, the getConfiguration method asks for a LoggerContext input variable but then proceeds to not do anything with it at all?
Does anyone know how I use this to make log4j use this custom configuration builder instead of an xml file?