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 the log4j.xml file located at the root of the JAR/EAR. Then, pass the resulting InputStream to DOMConfigurator.doConfigure(InputStream, LoggerRepository)
Alternatively, you can invoke DOMConfigurator.configure(getClass().getResource("/log4j.xml")), where getClass() returns a Class object in your JAR/EAR.
Related
I'm using the log4j2 library to manage the logging process.
I created a configuration file named log4j2.xml containing the Appenders and Loggers configurations. Then, I defined a Logger in each class
private static Logger my_logger = LogManager.getLogger(my_class);
I did not specify anywhere the name of the conf file, so I think that the library implicitly get and read it.
Now, I need to provide my application in the form of a jar file, so I need to make the config file available so that the user can modify and configure it.
In my case, I suggest to create a XXX folder at the level of the jar file, containing all the configuration files used by my app.
My question is how can I say to the app "get XXX/log4j2.xml" rather than the xml contained into the jar.
that config file must be located in the class path, if you want the app to read the configuration from any other location then you need to specify that using
PropertyConfigurator.configure("/myPath/log4j.properties");
Make any folder and put your property or xml file in that. In order to read the property file you can do something like this:
Properties objProperties = new Properties();
<your-class>.class.getClassLoader().getResource("folder/log4j.properties");
objProperties.load(isFile);
or, Also this:
InputStream ist = Thread.currentThread().getContextClassLoader().getResourceAsStream("folder/log4j.properties");
In case of java web application please use the link
I had a similar task a few weeks ago.
I solved it this way:
Store a template of your log4j2.xml inside your jar files resource folder
When running your application, check for a file named log4j2.xml in the jar files current directory
If there is one, use that to create your logger
If not, copy your template from within your jar to the jar files directory and then use that to create your logger.
Cheers
I try to load webapp project's settings from own config.properties file, but there's no success: the file not found. I can't use ServletContext method, because i've access the file from ordinary class.
file = Config.class.getClassLoader().getResourceAsStream("/config.properties");
- it's returns null.
I've tried to put the file to WEB-INF and resources folders, still doesn't work :(
Any ideas how to force this work?
Try putting files in WEB-INF/classes .
If you put your config in src, than after compile it will be in WEB-INF/classes.
My recommendation for configuration files that would solve your problem is to place them in a folder that depends on an environment variable.
If you application is called MyApp, then you can force the user to place that file in the folder that points the environment variable MY_APP. You could in fact make it mandatory to run the application with a start up check.
Usually you should specify default values for your configuration parameters if you want your application to run without the environment variable.
To get enviroment variables in Java you can use System.getenv();
I am running a tool (a virtual globe) through code from a jar file. The code reads a resource (an XML file) to provide some configuration options, using syntax like this, in a method of class Config:
URL localURL = Config.class.getResource("Config.xml");
I would like to provide my own Config.xml file with settings that override those in the file contained in the jar file.
I am not clear about how I can do this. I understand that the getResource() method explores the classpath to find the resource. So I thought of this:
- putting a copy of the file with my own settings in a specific directory
- putting this directory in front of the classpath
But to no avail: the getResource() still always loads the resource from the jar file.
I must be missing something ...
I tried removing the Config.xml file from the application jar. That fails: the application fails because getResource() returns null. It seems to me like
Config.class.getResource("Config.xml")
only looks for resources inside the jar file that contains class Config, whereas I thought it was looking in the classpath.
OK, got it. The issue is this: my Config class is really in a package, i.e. vis.globe.Config.java, so getResource("Config.Xxml") really looks for a file called vis.globe.Config.xml.
Therefore, with a classpath such as "../config:../jar/appl.jar", it will look for file Config.xml, not in ../config, but in ../config/vis/globe.
So the solution was to create a sub-directory structure in ../config that reflects the fully qualified name of class Config.java, i.e.
../config/vis/globe/Config.xml
You could always look to do something programmatically. So for example you would first search for a file named UserConfig.xml and if it was not found fall back on the file named Config.xml which would be found in the jar file.
How to load property files placed in resource folder of an executable jar file. Here my app itself is a jar and it executes on it own. It need to find this property file (placed within itself under resource folder) at runtime depending on the path mentioned in the code. I have used below two methods but it didn't help me. Point here is, both these options are working fine when i execute in eclipse, but doesn't work when I pack it into an executable jar. It throws NullPointerException. Only problem I see here is that jar is not able to pick the property files with given path. Any help would be appreciated.
Method 1: Using Apache Commons Configuration
URL propFileURL = XYZ.class.getClassLoader().getResource("/config.properties");
Configuration propertyConfiguration = null;
propertyConfiguration = new PropertiesConfiguration(propFileURL);
In above case I'm getting ConfigurationException. Class is not able to find file mentioned in given path.
Method 2: Using getResourceAsStream. I know that getResource doesn't work if we are to load files from network on in any other location.
InputStream is =XYZ.class.getClassLoader().getResourceAsStream("/config.properties");
Properties prop = new Properties();
prop.load(is);
In this case, I'm getting nullPointerException.
Let me know if you need more details.
jar content Heirarchy
Properties file - /java-file-io-application/src/main/resources/config.properties
XYZ class - /java-file-io-application/src/main/java/org/bc/xyz/iplugin/utilities/XYZ.java
Looks like you might be building your jar incorrectly. Files from 'src/main/resources' would be expected at the root of the jar file. If your jar file contains the 'src/main/resources' directory, something's off with your build.
Is it possible to add or override log4j configuration previously loaded by log4j.xml file with another log4j.xml file loaded at runtime ?
My Glassfish server loads at startup log4j.xml file and I would like to change this configuration with another log4j.xml file in EAR file.
I tried to use DOMConfigurator.configure("log4j.xml") but it seems that the previously configuration is not overrided (but this method find log4j.xml file because when I change to non existing file, I have Exception).
How can I do this please ?
A log4j Configurator always applies its configuration starting from the "current" settings. If you want the new settings to replace the old ones, simply call LogManager.resetConfiguration() first, before calling the DOMConfigurator.