Spring Boot 整合 Log4j2 日志并压测性能
程序员文章站
2022-03-25 16:26:42
1/ Log4j2的性能测试 从图中不难看出,在线程数为 2~16 之间,混合使用同步和异步的logger来打印日志,性能是最好的。 2/ 目标 混合 sync/async 彩色日志 分类输出到不同文件 自动压缩日志文件并归档 3/ 实现 0x01 Maven 依赖 pom.xml 0x02 配置 ......
1/ log4j2的性能测试
从图中不难看出,在线程数为 2~16 之间,混合使用同步和异步的logger来打印日志,性能是最好的。
2/ 目标
- 混合 sync/async
- 彩色日志
- 分类输出到不同文件
- 自动压缩日志文件并归档
3/ 实现
0x01 maven 依赖 pom.xml
<?xml version="1.0" encoding="utf-8"?> <project xmlns="http://maven.apache.org/pom/4.0.0" xmlns:xsi="http://www.w3.org/2001/xmlschema-instance" xsi:schemalocation="http://maven.apache.org/pom/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelversion>4.0.0</modelversion> <groupid>org.spring</groupid> <artifactid>springboot</artifactid> <version>0.0.1-snapshot</version> <packaging>jar</packaging> <name>springboot</name> <description>demo log4j2 for spring boot</description> <parent> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-parent</artifactid> <version>1.5.4.release</version> </parent> <properties> <project.build.sourceencoding>utf-8</project.build.sourceencoding> <project.reporting.outputencoding>utf-8</project.reporting.outputencoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-web</artifactid> <exclusions> <exclusion> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-logging</artifactid> </exclusion> </exclusions> </dependency> <!-- 代码简化 --> <dependency> <groupid>org.projectlombok</groupid> <artifactid>lombok</artifactid> <version>1.16.16</version> </dependency> <!-- 日志 log4j2 --> <dependency> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-starter-log4j2</artifactid> </dependency> <!-- log4j2 异步支持 --> <dependency> <groupid>com.lmax</groupid> <artifactid>disruptor</artifactid> <version>3.3.6</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupid>org.springframework.boot</groupid> <artifactid>spring-boot-maven-plugin</artifactid> </plugin> </plugins> </build> </project>
0x02 配置 log4j2,在 resources 文件目录下添加文件 log4j2.xml,会被自动配置
<?xml version="1.0" encoding="utf-8"?> <!-- configuration后面的status,这个用于设置log4j2自身内部的信息输出,可以不设置,当设置成trace时, 你会看到log4j2内部各种详细输出。可以设置成off(关闭) 或 error(只输出错误信息)。 30s 刷新此配置 --> <configuration status="warn" monitorinterval="30"> <!-- 日志文件目录、压缩文件目录、日志格式配置 --> <properties> <property name="filename">/users/admin/code/log</property> <property name="filegz">/users/admin/code/log/7z</property> <property name="pid">????</property> <property name="log_pattern">%clr{%d{yyyy-mm-dd hh:mm:ss.sss}}{faint} %clr{%5p} %clr{${sys:pid}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwex</property> </properties> <appenders> <!-- 输出控制台日志的配置 --> <console name="console" target="system_out"> <!--控制台只输出level及以上级别的信息(onmatch),其他的直接拒绝(onmismatch)--> <thresholdfilter level="debug" onmatch="accept" onmismatch="deny"/> <!-- 输出日志的格式 --> <patternlayout pattern="${log_pattern}"/> </console> <!-- 打印出所有的信息,每次大小超过size,则这size大小的日志会自动存入按年份-月份建立的文件夹下面并进行压缩,作为存档 --> <rollingrandomaccessfile name="infofile" filename="${filename}/web-info.log" immediateflush="false" filepattern="${filegz}/$${date:yyyy-mm}/%d{yyyy-mm-dd}-%i.web-info.gz"> <patternlayout pattern="${log_pattern}"/> <policies> <sizebasedtriggeringpolicy size="20 mb"/> </policies> <filters> <!-- 只记录info和warn级别信息 --> <thresholdfilter level="error" onmatch="deny" onmismatch="neutral"/> <thresholdfilter level="info" onmatch="accept" onmismatch="deny"/> </filters> <!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 --> <defaultrolloverstrategy max="50"/> </rollingrandomaccessfile> <!-- 存储所有error信息 --> <rollingrandomaccessfile name="errorfile" filename="${filename}/web-error.log" immediateflush="false" filepattern="${filegz}/$${date:yyyy-mm}/%d{yyyy-mm-dd}-%i.web-error.gz"> <patternlayout pattern="${log_pattern}"/> <policies> <sizebasedtriggeringpolicy size="50 mb"/> </policies> <filters> <!-- 只记录error级别信息 --> <thresholdfilter level="error" onmatch="accept" onmismatch="deny"/> </filters> <!-- 指定每天的最大压缩包个数,默认7个,超过了会覆盖之前的 --> <defaultrolloverstrategy max="50"/> </rollingrandomaccessfile> </appenders> <!-- mixed sync/async --> <loggers> <root level="debug" includelocation="true"> <appenderref ref="console"/> <appenderref ref="infofile"/> <appenderref ref="errorfile"/> </root> <asyncroot level="debug" includelocation="true"> <appenderref ref="console"/> <appenderref ref="infofile"/> <appenderref ref="errorfile"/> </asyncroot> </loggers> </configuration>
0x03 添加 application 启动类
@springbootapplication @enablescheduling public class application { public static void main(string[] args) { springapplication.run(application.class, args); } }
0x04 添加测试的 job 类
@component @log4j2 public class logjob { /** * 2秒钟执行1次 */ @scheduled(fixedrate = 2 * 1000) public void logging(){ date now = new date(); simpledateformat simpledateformat = new simpledateformat("yyyy-mm-dd hh:mm"); log.info(simpledateformat.format(now)); log.debug("-------debug---------"); log.error(now.gettime()); } }
0x05 大致文件目录结构
4/ 参考文档
文末福利
java 资料大全 链接:https://pan.baidu.com/s/1puccpstpnlgdcljtbvusxq 密码:b2xc
更多资料: 2020 年 精选阿里 java、架构、微服务精选资料等,加 v ❤ :qwerdd111
转载,请保留原文地址,谢谢 ~
上一篇: 生产者消费者算法模拟 c++