Why Log4j thinks my project run in Servlet Environment

11,181

Solution 1

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.

Solution 2

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.

Share:
11,181
dinesh707
Author by

dinesh707

Updated on July 26, 2022

Comments

  • dinesh707
    dinesh707 almost 2 years

    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 ?

  • BetaRide
    BetaRide over 3 years
    We use interfaces with annotations from servlet-api for open-feign. So yes, there are scenarios where you need the servelt-api when not running in a servlet container.