Spring Boot整合Log4j2实践日志

1、Log4j2的性能测试

Spring Boot整合Log4j2实践日志_第1张图片

从图中不难看出,在线程数为 2~16 之间,混合使用同步和异步的logger来打印日志,性能是最好的。

2/ 目标

  • 混合 sync/async

  • 彩色日志

  • 分类输出到不同文件

  • 自动压缩日志文件并归档

Spring Boot整合Log4j2实践日志_第2张图片

3/ 实现

0x01 Maven 依赖 pom.xml

 
  1.    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

  2.    4.0.0

  3.  

  4.    org.spring

  5.    springboot

  6.    0.0.1-SNAPSHOT

  7.    jar

  8.  

  9.    springboot

  10.    Demo Log4j2 for Spring Boot

  11.  

  12.    

  13.        org.springframework.boot

  14.        spring-boot-starter-parent

  15.        1.5.4.RELEASE

  16.    

  17.  

  18.    

  19.        UTF-8

  20.        UTF-8

  21.        1.8

  22.    

  23.  

  24.    

  25.  

  26.        

  27.            org.springframework.boot

  28.            spring-boot-starter-web

  29.            

  30.                

  31.                    org.springframework.boot

  32.                    spring-boot-starter-logging

  33.                

  34.            

  35.        

  36.  

  37.        

  38.        

  39.            org.projectlombok

  40.            lombok

  41.            1.16.16

  42.        

  43.  

  44.        

  45.        

  46.            org.springframework.boot

  47.            spring-boot-starter-log4j2

  48.        

  49.  

  50.        

  51.        

  52.            com.lmax

  53.            disruptor

  54.            3.3.6

  55.        

  56.  

  57.    

  58.  

  59.    

  60.        

  61.            

  62.                org.springframework.boot

  63.                spring-boot-maven-plugin

  64.            

  65.        

  66.    

  67.  

0x02 配置 Log4j2,在 resources 文件目录下添加文件 log4j2.xml,会被自动配置

 
  1.  

  2.    

  3.    

  4.        /Users/admin/Code/log

  5.        /Users/admin/Code/log/7z

  6.        ????

  7.        %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

  8.    

  9.  

  10.    

  11.        

  12.        

  13.            

  14.            

  15.            

  16.            

  17.        

  18.  

  19.        

  20.        

  21.                                    filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-info.gz">

  22.            

  23.  

  24.            

  25.                

  26.            

  27.  

  28.            

  29.                

  30.                

  31.                

  32.            

  33.  

  34.            

  35.            

  36.        

  37.  

  38.        

  39.        

  40.                                    filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-error.gz">

  41.            

  42.  

  43.            

  44.                

  45.            

  46.  

  47.            

  48.                

  49.                

  50.            

  51.  

  52.            

  53.            

  54.        

  55.    

  56.  

  57.    

  58.    

  59.        

  60.            

  61.            

  62.            

  63.        

  64.  

  65.        

  66.            

  67.            

  68.            

  69.        

  70.    

  71.  

0x03 添加 Application 启动类

 
  1. @SpringBootApplication

  2. @EnableScheduling

  3. public class Application {

  4.  

  5.    public static void main(String[] args) {

  6.        SpringApplication.run(Application.class, args);

  7.    }

  8.  

  9. }

0x04 添加测试的 Job 类

 
  1. @Component

  2. @Log4j2

  3. public class LogJob {

  4.  

  5.    /**

  6.     * 2秒钟执行1次

  7.     */

  8.    @Scheduled(fixedRate = 2 * 1000)

  9.    public void logging(){

  10.        Date now = new Date();

  11.        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");

  12.        log.info(simpleDateFormat.format(now));

  13.        log.debug("-------DEBUG---------");

  14.        log.error(now.getTime());

  15.    }

  16.  

  17. }

0x05 大致文件目录结构

Spring Boot整合Log4j2实践日志_第3张图片

4/ 参考文档

  • RollingRandomAccessFileAppender

  • MixedSync-Async

Spring Boot 整合 HBase

Spring Batch 轻量级批处理框架实践

Spring Boot CMS 开发实践

你可能感兴趣的:(Springboot)