学习拦截器的timer时,myeclipse始终无法在控制台打印输出timer拦截器的时间信息,此时控制台中有如下提示:
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console...
上网查找后,发现需要导入log4j-core的jar包,但是在下载了log4j-core-2.6.1导入后仍然报上述错误,后无意间在网页中看到有人说,如果同时有log4j-core-2.9.1.jar和log4j-api-2.9.1.jar将不会出现上述问题,因此采取该方法后,果然问题得以解决。至于是因为log4j-core的jar包版本低造成的,还是没有跟log4j-api版本一致造成的,本人并未对此进行深入研究。
在解决了上述问题后,重新启动Tomcat后,控制台虽未报上述错误,但是出现了错误信息如下:
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Set system property 'log4j2.debug' to show Log4j2 internal initialization logging.
该错误属于,控制台默认只将错误级别为error的 错误信息进行打印,而timer拦截器中的信息因属于info级别。因此需要在src文件夹下新建一个log4j2.xml文件,将文件中的level设置为info,该配置文件的路径和内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
此时控制台中能正确打印出timer的信息
00:49:26.932 [http-apr-8080-exec-4] INFO com.opensymphony.xwork2.interceptor.TimerInterceptor - Executed action [/Pages/addLeague!execute] took 341 ms.