美文网首页java 基础
log4j2.yml配置总结

log4j2.yml配置总结

作者: 纳米君 | 来源:发表于2019-06-06 12:40 被阅读0次

    pom.xml:

    <!-- log4j2 -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
    
    <!-- 加上这个才能辨认到log4j2.yml文件 -->
    <dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-yaml</artifactId>
    </dependency>
    

    application.yml

    logging:
      config: classpath:log4j2.yml
    

    log4j2.yml

    # 共有8个级别,按照从低到高为:ALL < TRACE < DEBUG < INFO < WARN < ERROR < FATAL < OFF。
    # status:用于设置log4j2自身内部的信息输出,可以不设置
    # monitorInterval:log4j2监测配置文件的时间间隔,如果文件被修改,则重新加载
    Configuration:
      status: warn
      monitorInterval: 30
    
      Properties: # 定义全局变量
        Property:
          - name: log.level.console
            value: info
          - name: log.path
          # “sys:”表示:如果VM参数中没指定这个变量值,则使用本文件中定义的缺省全局变量值
          # ${sys:catalina.home} 指向tomcat安装目录
            value: ${sys:catalina.home}/logs
          - name: project.name
            value: CloudEmploy
          - name: log.pattern
            value: "%d{yyyy-MM-dd HH:mm:ss.SSS} -%5p ${PID:-} [%15.15t] %-30.30C{1.} : %m%n"
    
      Appenders:
        Console:  # 输出到控制台
          name: CONSOLE
          target: SYSTEM_OUT
          PatternLayout:
            pattern: ${log.pattern}
        RollingFile: # 打印到文件
          - name: ROLLING_FILE
            # fileName  指定当前日志文件的位置和文件名称
            fileName: ${log.path}/${project.name}.log
            # filePattern  指定当发生Rolling时,文件的转移和重命名规则
            filePattern: "${log.path}/$${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
            PatternLayout:
              pattern: ${log.pattern}
            Filters:
              ThresholdFilter:
                - level: ${sys:log.level.console}
                  onMatch: ACCEPT
                  onMismatch: DENY
            Policies:
              TimeBasedTriggeringPolicy:  # 按天分类
                modulate: true
                interval: 1
            DefaultRolloverStrategy:     # 文件最多100个
              max: 100
    
    # 只有定义了 Loggers,并引入 Appenders,Appenders才会生效
      Loggers:
        Root:
          level: error
          AppenderRef:
            - ref: CONSOLE
            - ref: ROLLING_FILE
    
        Logger: # 为com.***包配置特殊的Log级别,方便调试,trace会打印SQL具体结果,debug只打印SQL语句
          - name: com.***.mapper
            additivity: false
            level: trace
            AppenderRef:
              - ref: CONSOLE
              - ref: ROLLING_FILE
    

    相关文章

      网友评论

        本文标题:log4j2.yml配置总结

        本文链接:https://www.haomeiwen.com/subject/luktgqtx.html