Using a default log4j2 configuration file from inside the jar - java

I placed a default log4j2 config file (log4j2.xml) in my application jar.
If the user doesn't define a own config file, I want to load my default config.
I could get a stream of the default config this way:
InputStream defaultConfigStream = MyApp.class.getClassLoader().getResourceAsStream("log4j2.xml");
But that won't help me, because following code takes a full path to the config file to check its existance and to load it.
System.setProperty("log4j.configurationFile", file.toUri().toURL().toString());
How can I accomplish that? I don't want to hardcode my default config settings, like here.

Use a DomConfigurator, see: https://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html, you can parse the XML and feed it to the configurator and when initializing logger it will automatically consider the configuration

As I found out, it's pretty simple. Like described in the documentation (point 7), Log4j2 will automatically look for a log4j2.xml on the classpath.
So if the user doesn't define a own config file, I don't call any method to load my default config.

Related

How to know if logback configuration file has loaded?

I am new to SLF4j and I don't know if the logback.xml file has loaded properly or not. The logback.xml file is in PROJECTNAME/src/main/java where all my packages are found.
My questions are:
How can I know if the configuration file has properly loaded or not
?
How can restrict the logging only from an explicit set of class,
only to avoid logging from libraries
You can add the debug="true" attribute to the <configuration> element to enable debug of the logback configuration. It will print the configuration to the console. See https://logback.qos.ch/manual/configuration.html#dumpingStatusData.
Simple answer, if the configuration file is loaded properly, you will see results in log file or console, depending on your configuration.
By default, logback searches file in src/main/resources instead of src/main/java if I remember correctly.
In the configuration file, you can define log lever on a specific logger. Normally you'll still want to see logs of the libraries, but maybe only WARN or ERROR, so you could set the root level to WARN/ERROR, and add a logger of your root package with DEBUG/INFO level.
Also, use a logback-test file (under src/test/resources) for your own dev environment.

Set where to find the config file for audit4j

I've looked all over the documentation, but was unable to find one. How can I achieve this?
According to the Audit4j documentation and javadoc there are multiple ways to specify the configuration:
It can be injected into the Context directly by calling static methods of the Context class (e.g. at application startup before doing any audit!):
Context#initWithConfiguration(Configuration configuration) with the appropriate Configuration
or Context#initWithConfiguration(String configFilePath) to specify the configuration as file.
Other ways to specify the path to the configuration file without manually initializing the Context are:
Setting path in environment variable "AUDIT4J_CONF_FILE_PATH"
Setting path in Java system property variable "audit4j.conf.file.path"
Also the configuration file can be put into the application classpath or the user directory. In this case the name of the configuration file has to be "audit4j.conf.yml" or "audit4j.conf.yaml" for YAML style or "audit4j.conf.xml" for XML style.

How to set the log level on a class in log4j2 properties

In log4j I could specify a class in the properties file to log at the debug level like this:
log4j.logger.com.mycompany.mypackage.ClassName=DEBUG
How do I do this in log4j2? Note I still need to use the new property file (not xml or json).
As the log4j2 configuration documentation states
As of version 2.4, Log4j now supports configuration via properties
files. Note that the property syntax is NOT the same as the syntax
used in Log4j 1.
It then provides a substantial example for all types of configuration elements.
Concerning your question, you need to specify your loggers in a loggers element, then configure each of them. For example
loggers = mine
logger.mine.name = com.mycompany.mypackage.ClassName
logger.mine.level = DEBUG
Note that log4j2 looks for a .properties file on the classpath by default.
If a test file cannot be located the properties ConfigurationFactory
will look for log4j2.properties on the classpath.
But you can also configure the location yourself. You can use the system property
-Dlog4j.configurationFile=conf/log4j.properties
with an appropriate path.
If you are using spring boot, you can use
logging.level.com.mycompany.mypackage.ClassName=DEBUG
in application.properties.

log4j logger overwriting into jxl.log file

I am using log4j for logger purpose. At the same time I am also using JXL to read/write Excel file.
But instead of writing log into log4j logger file, it is writing into jxl.log file.
What can be issue?
Looks like you have been using jxl-2.6.3.jar or similar version.
Log4j picks up the first configuration file with default file name ( i.e. log4j.xml or log4j.properties ) in your classpath if you haven't specified a specific name via JVM parameters. As jxl-2.6.3.jar contains a log4j.xml you ended up printing everything to jxl.log as defined in the log4j.xml
The best way to deal with these kind of problems is to run your application with -Dlog4j.debug JVM parameter. This would print a few line snippet when the log4j is initialized.
log4j: Using URL [jar:file:/C:/YourApp/WEB-INF/lib/jxl-2.6.3.jar!/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
...{Blah Blah Blah}
There are many ways in which you can solve this problem.
Use the newer versions of jxl which doesn't contain log4j.xml.
Make sure your log4j.properties file is on top of classpath.
Remove the log4j.xml from the jxl-2.6.3.jar (Dirty solution).
Pass the configuration file name in VM parameter as -Dlog4j.configuration=log4j.properties. This would atleast make sure log4j.xml inside jxl-2.6.3.jar will not be used. (But what if another jar with same name as log4j.properties?).
Rename your log4j.properties file to log4j-yourApp.properties and add VM parameter -Dlog4j.configuration=log4j-yourApp.properties This would definitely help and this is how it should be done to avoid these kind of situations.
More details on Log4j here

Multiple log4j xml files for multiple applications

I have 5 applications which have different log4j xml configuration file. And I want each of them to be configured according to the given file and logs correctly when called from one main method.
Log4j will automatically look for and use config files it finds on the classpath. It looks for files called log4j.properties and log4j.xml and possibly others.
Alternatively you can programatically load config using;
String filename = "/path/to/config/file.xml";
DOMConfigurator.configure(filename);

Categories

Resources