I have a simple java project (maven). Which builds a jar and we execute the main method on it. But when I run mvn clean test
on the project I get a log line from log4j saying
INFO Log4j appears to be running in a Servlet environment, but there's no log4j-web module available. If you want better web container support, please add the log4j-web JAR to your web archive or server lib directory.
The log4j2.xml
file is in src/main/resources/log4j2.xml
.
Any ideas whats going on ?
We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.
It allows for easy configuration of advanced logging best practices such as rolling files, different types of logging output destinations, support for structured logging formats such as JSON or XML, using multiple loggers and filters, and custom log levels.
This concept is known as Logger Hierarchy. Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.
If you have the servlet-api
Jar in your classpath, Log4j is going to think you are running in a Servlet container. Specifically, it looks for the presence of javax.servlet.ServletContext
. If your application is not running in a Servlet container, then you really shouldn't need that Jar.
Equally, if you're say loading that as part of a JUnit test and wish to remove the error, then you can always include the file like so:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>2.6.2</version>
<scope>test</scope>
</dependency>
Note the scope has been set to test, so the file will not be included in any build artifact.
Also note, that this "error" is in fact just set at "info" level, so log4j will continue with non-web support.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With