天天看点

Guns整合log4j2

文章目录

  • ​​一、排除内置logback​​
  • ​​1. 删除logback.xml​​
  • ​​2. 排除内置的logback​​
  • ​​二、整合log4j2​​
  • ​​2.1. 依赖log4j2​​
  • ​​2.2. 配置文件中配置​​
  • ​​2.3. log4j2日志文件​​
  • ​​2.4. 效果图​​
  • ​​2.5. 输出要素​​
Sringboot 开源框架默认logback日志框架,Guns开源项目也是用logback日志框架
一、排除内置logback

1. 删除logback.xml

在resources目录下面删除logback.xml

2. 排除内置的logback

和小伙伴们分享一下怎样排除内置logback依赖?

拿​

​spring-boot-starter-web​

​给大家演示:

思路:​

​spring-boot-starter-logging​

​​首先它是一个单独的日志启动器,对吧!一般​

​spring-boot-starter-*​

​​大部分都会有,给大家的思路是,一级一级找,很快就能找到,大家也可以直接把常用的以​

​spring-boot-starter-*​

​​开头的都排除​

​spring-boot-starter-logging​

Guns整合log4j2
Guns整合log4j2
Guns整合log4j2
Guns整合log4j2
<!--amqp协议-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-amqp</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-logging</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
            <exclusions>
                <!-- 排除自带的logback依赖 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <!-- 排除自带的logback依赖 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
<!--quartz定时任务-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-quartz</artifactId>
            <exclusions>
                <!-- 排除自带的logback依赖 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>      
二、整合log4j2

2.1. 依赖log4j2

<!-- 支持识别yml配置 -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
            <version>2.10.0</version>
        </dependency>
        <!--log4j2-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>      

2.2. 配置文件中配置

mybatis-plus:
  typeAliasesPackage: cn.stylefeng.guns.modular.system.model
  mapper-locations:
    - classpath*:cn/stylefeng/guns/**/mapping/*.xml
  configuration:
    log-impl: org.apache.ibatis.logging.log4j2.Log4j2Impl

#log4j2 預警      

2.3. log4j2日志文件

Appenders:
  Console:  #输出控制台的配置-
    name: CONSOLE #Appender命名
    target: SYSTEM_OUT
    PatternLayout:  #Pattern Layouts是一个灵活的布局,是最常用的日志格式配置。
#    JsonLayout:  #JsonLayout是用于console查看json时方便查看
      pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"

#  这个会打印出所有的信息,每次大小超过50MB size,则这size大小的日志会自动存入按(年份-月份)/(yyyy-MM-dd)建立的文件夹下面以HH-mm-ss格式作为存档
#  如果需要压缩,在文件名后面添加.gz即可
  RollingFile:
  - name: ROLLING_FILE
    ignoreExceptions: false
    fileName: /app/dca_apply/log/dca_health.log  #linux环境下生成的文件目录  一般为/**/**.log
#    fileName: D:/dca_apply/logs/dca_health.log       #windows环境下生成的文件目录  一般为*:/**/**.log
#    filePattern: "D:/dca_apply/logs/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/dca-%d{HH-mm-ss}-%i.log"
    filePattern: "/app/dca_apply/logs/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/dca-%d{HH-mm-ss}-%i.log"
#    filePattern: "/app/dca_apply/logs/$${date:yyyy-MM}/$${date:yyyy-MM-dd}/dca-%d{HH-mm-ss}-%i.log.gz"
    PatternLayout:
      pattern: "{\"@timestamp\":\"%d{yyyy-MM-dd'T'HH:mm:ss.SSS'Z'}\",\"LogId\":\"%X{logid}\",\"level\":\"%-5level\",\"threadID\":\"%threadId\",\"threadName\":\"%t\",\"ip\":\"${sys:local-ip}\",\"class\":\"%C\",\"method\":\"%M\",\"Line\":\"%L\",\"applicationName\":\"dca\",\"type\":\"common\",\"message\":\"%replace{%replace{%msg%xEx}{\"}{\\\\\"}}{[\f\n\r\t\v]}{ }\"}%n"
#      pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%4p %t (%F:%L) - %m%n"
    Policies:
      SizeBasedTriggeringPolicy:
        size: "50 MB"  #输出到文件最大内存
    DefaultRolloverStrategy:
      max: 1000

Loggers:
  Root:
    level: info
    AppenderRef:
    - ref: CONSOLE
    - ref: ROLLING_FILE
  Logger: #单独设置某些包的输出级别
  - name: com.sinosoft #复数加上-
    additivity: false #去除重复的log
    level: debug
    AppenderRef:
    - ref: CONSOLE #复数加上-
    - ref: ROLLING_FILE #复数加上-      

2.4. 效果图

2.5. 输出要素