Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why Log4j thinks my project run in Servlet Environment

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 ?

like image 844
dinesh707 Avatar asked May 20 '16 10:05

dinesh707


People also ask

Where to put Log4j2 xml?

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.

What does@ Log4j2 do?

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.

What is Rootlogger Log4j2?

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.


2 Answers

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.

like image 156
rgoers Avatar answered Sep 17 '22 22:09

rgoers


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.

like image 39
PeterS Avatar answered Sep 21 '22 22:09

PeterS