码迷,mamicode.com
首页 > 其他好文 > 详细

perf4j+logback配置 可使用注解

时间:2016-06-08 12:26:41      阅读:446      评论:0      收藏:0      [点我收藏+]

标签:

最近项目打算使用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><!-- use this goal to weave all your main classes -->
                            <goal>test-compile</goal><!-- use this goal to weave all your test classes -->
                        </goals>
                    </execution>
                </executions>
            </plugin>

 

至此logback+perf4j配置就完成了。

关于perf4j生成图表没有搭建成功,个人觉得logback和perf4j的结合使用不如log4j来的方便。

perf4j+logback配置 可使用注解

标签:

原文地址:http://www.cnblogs.com/maoxiaolv/p/5569522.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!