最近web项目使用了log4j2,但发现无法打印spring和struts2的日志,然后查了下官方文档,描述如下:
Using Log4j 2 in Web Applications
You must take particular care when using Log4j or any other logging framework within a Java EE web application. It's important for logging resources to be properly cleaned up (database connections closed, files closed, etc.) when the container shuts down or the web application is undeployed. Because of the nature of class loaders within web applications, Log4j resources cannot be cleaned up through normal means. Log4j must be "started" when the web application deploys and "shut down" when the web application undeploys. How this works varies depending on whether your application is a Servlet 3.0 or newer or Servlet 2.5 web application.
In either case, you'll need to add the log4j-web module to your deployment as detailed in the Maven, Ivy, and Gradle Artifacts manual page.
To avoid problems the Log4j shutdown hook will automatically be disabled when the log4j-web jar is included.
按照官网的说法,web项目在一般java项目的基础上添加log4j-web、servlet升级至3.0以上即可,但是操作完还是不行,折腾了很久,原来还需要加入jcl包,在此记录一下。
加入log4j2所需要的包如下:
log4j-core-2.5.jar、log4j-api-2.5.jar、log4j-web-2.5.jar、log4j-jcl-2.5.jar
参考官方文档:
http://logging.apache.org/log4j/2.x/