Why Log4j thinks my project run in Servlet Environment

天大地大妈咪最大 提交于 2021-01-27 03:54:19

问题


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 ?


回答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.




回答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.



来源:https://stackoverflow.com/questions/37344431/why-log4j-thinks-my-project-run-in-servlet-environment

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!