perf4j+logback配置 可使用注解

Posted 木子三金

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了perf4j+logback配置 可使用注解相关的知识,希望对你有一定的参考价值。

最近项目打算使用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配置 可使用注解的主要内容,如果未能解决你的问题,请参考以下文章

logback配置文件配置日志

logback配置文件配置日志

Tomcat控制台总是打印日志问题的解决办法

logback配置

rocketmq client 日志整合logback

springboot项目下logback配置