今天将perf4j基于spring aop方式进入了接入,接入方法还是比较简单。具体配置如下:
logback.xml
<!--perf4j配置--> <appender name="statistics" class="ch.qos.logback.core.rolling.RollingFileAppender"> <Encoding>UTF-8</Encoding> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--日志文件输出的文件名 --> <FileNamePattern>logs/statistics.%d{yyyy-MM-dd}.log</FileNamePattern> </rollingPolicy> <layout class="ch.qos.logback.classic.PatternLayout"> <pattern>%msg%n</pattern> </layout> </appender> <appender name="coalescingStatistics" class="org.perf4j.logback.AsyncCoalescingStatisticsAppender"> <timeSlice>10000</timeSlice> <appender-ref ref="statistics"/> </appender> <logger name="org.perf4j.TimingLogger" level="info" additivity="false"> <appender-ref ref="coalescingStatistics" /> </logger> <!--perf4j配置结束-->
aop.xml
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:config> <aop:aspect id="aspect" ref="timingAspect"> <aop:pointcut id="timingCut" expression="execution(* com.xxx..*.*(..)) and @annotation(profiled))"/> <aop:around pointcut-ref="timingCut" method="doPerfLogging"/> </aop:aspect> </aop:config> <bean id="timingAspect" class="org.perf4j.slf4j.aop.TimingAspect" /> </beans>
当然还有更方便的配置方式
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd"> <aop:aspectj-autoproxy /> <bean id="timingAspect" class="org.perf4j.slf4j.aop.TimingAspect" /> </beans>
OK!
现在就可以在需要监控的方法上添加@Profiled了。