Struts 2 简单使用 log4j2(timer 拦截器不输出信息)

版权声明:本文为博主原创文章,转载请标明地址。欢迎关注微信订阅号:PM实验室。 https://blog.csdn.net/plain_maple/article/details/60479246

今天刚学习拦截器的时候,书上说使用内置的 timer 可以打印出执行时间:

<package name="helloworld" extends="struts-default">
	<action name="HelloWorldAction" class="com.pm.ssh.HelloWorldAction">
		<result name="success">/helloname.jsp</result>
        <result name="error">/error.jsp</result>
        <interceptor-ref name="completeStack"/>
        <interceptor-ref name="timer"/>
	</action>
</package>

但我使用 struts-2.5.10-min-lib.zip 的包时,运行报错:

ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console

错误很明显是缺包,因为 struts-2.5.10-min-lib.zip 中只提供了 log4j-api-2.7,并没有提供 log4j-core 包。可以去 http://logging.apache.org/ 下载。

导入运行后,还是报错:ERROR StatusLogger No log4j2 configuration file found,因为还没有配置 log4j2,那么就进行配置呗,在 src 目录下新建 log4j2.xml,进行最基本的配置,这里是打印 info 往后级别的 log。

<?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>

重新测试:

21:15:24.378 [http-nio-8080-exec-39] INFO  com.opensymphony.xwork2.interceptor.TimerInterceptor - Executed action [//HelloWorldAction!execute] took 282 ms.
21:15:24.378 [http-nio-8080-exec-39] INFO  com.opensymphony.xwork2.interceptor.LoggingInterceptor - Finishing execution stack for action //HelloWorldAction

OK啦。

猜你喜欢

转载自blog.csdn.net/plain_maple/article/details/60479246