原文地址:http://www.sanjinbest.com/blog/94c7142f17734327bb62694ecaea4cb7.html
最近项目打算使用perf4j进行性能监控,由于项目没有使用spring,而又不想对代码入侵过高,打算使用注解的方式进行接入。perf4j采用AspectJ库实现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配置结束-->
在META-INF下创建文件 aop.xml
<!DOCTYPE aspectj PUBLIC "-//AspectJ//DTD//EN"
"http://www.eclipse.org/aspectj/dtd/aspectj.dtd">
<aspectj>
<aspects>
<aspect name="org.perf4j.slf4j.aop.TimingAspect"/>
</aspects>
<weaver options="-verbose -showWeaveInfo">
<!-- Remember to include this-->
<include within="org.perf4j.slf4j.aop.TimingAspect" />
<include within="com.xxx.*" />
</weaver>
</aspectj>
pom.xml
<properties>
<perf4j.version>0.9.16</perf4j.version>
<aspectj.version>1.8.5</aspectj.version>
<commons.jexl.version>1.1</commons.jexl.version>
</properties>
<dependency>
<groupId>org.perf4j</groupId>
<artifactId>perf4j</artifactId>
<version>${perf4j.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjrt</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>${aspectj.version}</version>
</dependency>
<dependency>
<groupId>commons-jexl</groupId>
<artifactId>commons-jexl</artifactId>
<version>${commons.jexl.version}</version>
</dependency>
pom.xml内添加plugin
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>aspectj-maven-plugin</artifactId>
<version>1.7</version>
<configuration>
<complianceLevel>1.7</complianceLevel>
<showWeaveInfo>true</showWeaveInfo>
<verbose>true</verbose>
<weaveDependencies>
<dependency>
<groupId>org.perf4j</groupId>
<artifactId>perf4j</artifactId>
</dependency>
</weaveDependencies>
</configuration>
<dependencies>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjtools</artifactId>
<version>${aspectj.version}</version>
</dependency>
</dependencies>
<executions>
<execution>
<goals>
<goal>compile</goal>
<goal>test-compile</goal>
</goals>
</execution>
</executions>
</plugin>