log4j.xml文件
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j=‘http://jakarta.apache.org/log4j/‘ > <appender name="myConsole" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{dd HH:mm:ss,SSS\} %-5p] [%t] %c{2\} - %m%n" /> </layout> <!--过滤器设置输出的级别--> <filter class="org.apache.log4j.varia.LevelRangeFilter"> <param name="levelMin" value="debug" /> <param name="levelMax" value="warn" /> <param name="AcceptOnMatch" value="true" /> </filter> </appender> <appender name="myFile" class="org.apache.log4j.RollingFileAppender"> <param name="File" value="D:/output.log" /><!-- 设置日志输出文件名称 --> <!-- 设置是否在又一次启动服务时,在原有日志的基础加入新日志 --> <param name="Append" value="true" /> <param name="MaxBackupIndex" value="10" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p (%c:%L)- %m%n" /> </layout> </appender> <appender name="activexAppender" class="org.apache.log4j.DailyRollingFileAppender"> <param name="File" value="E:/activex.log" /> <param name="DatePattern" value="‘.‘yyyy-MM-dd‘.log‘" /> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%d{MMdd HH:mm:ss SSS\} %-5p] [%t] %c{3\} - %m%n" /> </layout> </appender> <!-- 指定logger的设置,additivity指示是否遵循缺省的继承机制--> <logger name="com.util.TimerLogger" additivity="false"> <priority value ="debug"/> <appender-ref ref="activexAppender" /> </logger> <!-- 根logger的设置--> <root> <priority value ="debug"/> <appender-ref ref="myConsole"/> <appender-ref ref="myFile"/> <appender-ref ref="activexAppender"/> </root> </log4j:configuration>
web.xml中载入
<context-param> <param-name>log4jConfigLocation</param-name> <param-value>classpath:log4j.xml</param-value> </context-param>
package com.aop; import org.apache.log4j.Logger; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.ProceedingJoinPoint; import com.service.imp.UserServiceImpl; public class MyAspect { //private static final Logger LOGGER=Logger.getLogger(MyAspect.class); //输出到指定文件 private static final Logger LOGGER=Logger.getLogger("com.util.TimerLogger"); public Object doAround(ProceedingJoinPoint pjp) throws Throwable { long time = System.currentTimeMillis(); Object retVal = pjp.proceed(); time = System.currentTimeMillis() - time; Object[] o=pjp.getArgs(); for(int i=0;i<o.length;i++){ LOGGER.info("第"+(i+1)+"输入參数 = "+o[i]); System.err.println("111111111111111");//測试是否进入切面 } LOGGER.info("当前方法为 " + pjp.getTarget().getClass().getName() + "." + pjp.getSignature().getName()+" 运行时间为"+time+"ms"); return retVal; } }
測试类
package function; import java.io.IOException; import org.junit.Test; import com.model.User; import com.service.UserService; import com.util.BeanFactoryUtil; public class MyAspectTest { @Test public void aopTest() throws IOException{ UserService userservice=(UserService)BeanFactoryUtil.getInstance().getBean("userService"); User user=userservice.getUserByUid(3); System.err.println(user.getName()); } }
output.log文件
[26 10:50:39,024 INFO ] [main] aop.MyAspect - 第1输入參数 = 3
[26 10:50:39,025 INFO ] [main] aop.MyAspect - 当前方法为 com.service.imp.UserServiceImpl.getUserByUid ?运行时间为277ms
张三
该日志文件记录了每一个方法的运行时间,利用AOP切面。