为什么Log4j认为我的项目在Servlet环境中运行

时间:2016-05-20 10:34:22

标签: java maven log4j log4j2

我有一个简单的java项目(maven)。这构建了一个jar,我们在其上执行main方法。但是当我在项目上运行mvn clean test时,我从log4j得到一个日志行

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.

log4j2.xml文件位于src/main/resources/log4j2.xml

有什么想法吗?

3 个答案:

答案 0 :(得分:5)

如果你的类路径中有servlet-api Jar,Log4j会认为你在Servlet容器中运行。具体来说,它会查找javax.servlet.ServletContext的存在。如果你的应用程序没有在Servlet容器中运行,那么你真的不需要那个Jar。

答案 1 :(得分:1)

同样,如果您说要将其作为JUnit测试的一部分加载并希望删除错误,那么您始终可以像这样包含文件:

<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-web</artifactId>
    <version>2.6.2</version>
    <scope>test</scope>
</dependency>

请注意,范围已设置为test,因此该文件不会包含在任何构建工件中。

另请注意,这个&#34;错误&#34;实际上只是设置在&#34; info&#34;等级,所以log4j将继续非网络支持。

答案 2 :(得分:0)

我在使用依赖时遇到了同样的问题:

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

当我还包含依赖项时,我可以解决它:

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

到 pom.xml 文件。 我在这里没有太多说明,但这就是我在 Kafka 客户领域工作时遇到的问题。希望对某人有所帮助。