Log4j2的性能测试
从图中不难看出,在线程数为 2~16 之间,混合使用同步和异步的logger来打印日志,性能是最好的。2/ 目标
混合 sync/async
彩色日志
分类输出到不同文件
自动压缩日志文件并归档
与Spring Boot集成的Maven 依赖 pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/ma ... gt%3B
4.0.0
org.spring
springboot
0.0.1-SNAPSHOT
jar
springboot
Demo Log4j2 for Spring Boot
org.springframework.boot
spring-boot-starter-parent
1.5.4.RELEASE
UTF-8
UTF-8
1.8
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.projectlombok
lombok
1.16.16
org.springframework.boot
spring-boot-starter-log4j2
com.lmax
disruptor
3.3.6
org.springframework.boot
spring-boot-maven-plugin
配置 Log4j2,在 resources 文件目录下添加文件 log4j2.xml,会被自动配置
/Users/admin/Code/log
/Users/admin/Code/log/7z
????
%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx
filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-info.gz">
filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-error.gz">
添加 Application 启动类
@SpringBootApplication
@EnableScheduling
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
添加测试的 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());
}
}
大致文件目录结构