Why Log4j thinks my project run in Servlet Environment
Asked Answered
C

3

10

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 ?

Campy answered 20/5, 2016 at 10:34 Comment(1)
the source of that error is here logging.apache.org/log4j/2.0/log4j-core/apidocs/src-html/org/…Joan
Q
6

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.

Quaker answered 20/5, 2016 at 11:6 Comment(1)
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.Bullbat
H
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.

Horsy answered 15/8, 2016 at 10:25 Comment(0)
W
0

I had the same issue with me using the depedency:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>connect-json</artifactId>
    <version>2.0.1</version>
</dependency>

I could resolve it when I also included the dependency:

<dependency>
    <groupId>org.apache.kafka</groupId>
    <artifactId>kafka-clients</artifactId>
    <version>2.0.0</version>
</dependency>

to the pom.xml file. I don't have much of a clarification here, but that's what I ran into when I was working in the domain of Kafka clients. Hope that helps somebody.

Waterresistant answered 26/5, 2021 at 7:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.