How to retrieve log4j2.xml if stored outside application - java

I am doing a maven application. If I place my log4j2.xml configuration file in the src/resources folder it works fine.
However, I need to store it outside the application. How can I call the location of the log4j2.xml file if it is stored, for example, on my desktop? Should I create a file in the src/resource folder that reads in my log file location and how can I do that?

The Log4j2 configuration file doesn't need to be in the classpath.
You can specify a relative or a full path with system property -Dlog4j.configurationFile=path/to/log4j2.xml.
See also the Log4j2 FAQ page.

Related

Proper location of log4j2.xml files within a tomcat webapp

We have a webapp that contains two log4j2.xml files:
one within WEB-INF/classes containing webapp-specific logging settings (1), and
one within the root directory of a dependency located in WEB-INF/lib containing default logging settings that we use also in other webapps (2)
My questions are:
How does tomcat read and evaluate these files?
In which order are they read?
Is it possible to overwrite settings of (2) by (1) and how would I do that?
Thank you.
Reading the documentation here you will find that:
Log4j allows the configuration file to be specified in web.xml using the log4jConfiguration context parameter. Log4j will search for configuration files by:
If a location is provided it will be searched for as a servlet context resource. For example, if log4jConfiguration contains "logging.xml" then Log4j will look for a file with that name in the root directory of the web application.
If no location is defined Log4j will search for a file that starts with "log4j2" in the WEB-INF directory. If more than one file is found, and if a file that starts with "log4j2-name" is present, where name is the name of the web application, then it will be used. Otherwise the first file will be used.
The "normal" search sequence using the classpath and file URLs will be used to locate the configuration file.
if I understand correctly, this answers all your questions.

log4j configuration file and jar application

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

Where to place log4j.xml file in a non Maven Project

I have my log4j.xml file stored in the project directory but i am getting following error:
log4j:WARN No appenders could be found for logger (p1.Employee).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more
info.
When i palced it in the bin folder Everything works fine. But i dont check in the bin folder in the code repository so i dont want to place it there.
I dont want to use DOMConfigurator.configure("log4j.xml") as well
Below is my Project Stucture.
In a normal Java Project, you can place a log4j configuration file, i.e., a log4j.properties or a log4j.xml, in a resources directory. The resources directory should be on the same level as src. Putting it here, will allow log4j to use the configuration file automatically.
Otherwise, you can set the log4j configuration file to use via a VM option by doing something like:
-Dlog4j.configuration=file:///path/to/log4j.xml
In your case, if you want to place it in the TrailProject project directory, you can do
-Dlog4j.configuration=file:///path/to/TrailProject/log4j.xml
As per the maven java project folder structure, the ideal path to put the config.xml would be in /src/main/resources directory, so for a non maven project you would put it in an equivalent /resources directory.

Where to put logback.xml in Tomcat?

Where to put the logback.xml file in Tomcat when we want to have it configurable?
And how to make it accessible for the Java application(s) running inside?
You typically want to have logback.xml on the classpath. Per the Logback FAQ:
For web-applications, configuration files can be placed directly under WEB-INF/classes/.
You therefore need to put it in:
/webapps/your-app/WEB-INF/classes/
Logback has some conventions for where it looks for it. Those are documented here.
Logback tries to find a file called logback.groovy in the classpath.
If no such file is found, logback tries to find a file called logback-test.xml in the classpath.
If no such file is found, it checks for the file logback.xml in the classpath..
If neither file is found, logback configures itself automatically using the BasicConfigurator which will cause logging output to be
directed to the console.
But you can also tell it where to find the file.
You may specify the location of the default configuration file with a
system property named "logback.configurationFile". The value of this
property can be a URL, a resource on the class path or a path to a
file external to the application.
java -Dlogback.configurationFile=/path/to/config.xml chapters.configuration.MyApp1
Note that the file extension must be ".xml" or ".groovy". Other
extensions are ignored. Explicitly registering a status listener may
help debugging issues locating the configuration file.

Referencing log4j config file within executable JAR

I am creating an executable JAR that uses a couple XML config files, one for the application and one for log4j. To reference my app config file, I do this:
InputStream config = Util.class.getResourceAsStream("/config/config.xml");
This works fine for my app config, but the problem is that I can't configure log4j like this. Here is the code that configures log4j:
DOMConfigurator.configure("/config/log4j.xml");
This won't work because the XML file is going to be stored within the packaged JAR. How can I configure log4j to use an XML or properties file within the JAR?
You can use the URL version of the DOMConfigurator.configure method. The resource will have to be available at/config/log4j.xml.
DOMConfigurator.configure(Util.class.getResource("/config/log4j.xml")
You can try
DOMConfigurator.configure(Util.class.getResource("/config/log4j.xml"));

Categories