springCloudAlibaba整合log4j2

文章目录

对spring-cloud-alibaba相关组件比较感兴趣的小伙伴,可以看下spring-cloud-alibaba 练习项目

简介

日志主要是记录系统发生的动作,无论是web请求记录接收到参数,连接数据所做的操作,还是在处理业务逻辑一些重要的操作都可以写入到日志中。系统日志一方面可以让开发人员方便查找系统问题,另一方面可以有些监测系统的健康性。

log4j2概述

Apache Log4j2 是对Log4j 的升级版本,参考logback 的一些优秀的设计,并且修复了一些问题,因此带来了一些重大的提升。

官网:https://logging.apache.org/log4j/2.x/

log4j2在springCloudAlibaba中的使用

排除依赖

将spring-boot-starter及spring-boot-starter-web中的spring-boot-starter-logging依赖排除

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <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>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

引入log4j2依赖

<dependency>
     <groupId>org.springframework.boot</groupId>
     <artifactId>spring-boot-starter-log4j2</artifactId>
 </dependency>

添加log4j配置文件

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration status="OFF">
    <Properties>
        <property name="console_log_pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%t] %l %n %m%n</property>
        <property name="file_log_pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} [%level] [%t] %C.%M[%L line] %n %m%n</property>
        <property name="every_file_size">20MB</property>
    </Properties>

    <appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${console_log_pattern}"/>
        </Console>

        <RollingFile name="DEBUG" fileName="./log/log4j2/debug.log"
                     filePattern="./log/log4j2/debug_log_archive/debug-%d{yyyy-MM-dd}-%i.log.zip">
            <PatternLayout pattern="${file_log_pattern}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="${every_file_size}"/>
            </Policies>
            <Filters>
                <ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </RollingFile>

        <RollingFile name="INFO" fileName="./log/log4j2/info.log"
                     filePattern="./log/log4j2/info_log_archive/info-%d{yyyy-MM-dd}-%i.log.zip">
            <PatternLayout pattern="${file_log_pattern}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="${every_file_size}"/>
            </Policies>
            <Filters>
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </RollingFile>

        <RollingFile name="WARN" fileName="./log/log4j2/warn.log"
                     filePattern="./log/log4j2/warn_log_archive/warn-%d{yyyy-MM-dd}-%i.log.zip">
            <PatternLayout pattern="${file_log_pattern}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="${every_file_size}"/>
            </Policies>
            <Filters>
                <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </RollingFile>

        <RollingFile name="ERROR" fileName="./log/log4j2/error.log"
                     filePattern="./log/log4j2/error_log_archive/error-%d{yyyy-MM-dd}-%i.log.zip">
            <PatternLayout pattern="${file_log_pattern}"/>
            <Policies>
                <SizeBasedTriggeringPolicy size="${every_file_size}"/>
            </Policies>
            <Filters>
                <ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>
        </RollingFile>

    </appenders>

    <loggers>
        <!-- 属性 level 是用于设置最低需要输出的日志输出级别 -->
        <root level="DEBUG">
            <appender-ref ref="Console"/>
            <appender-ref ref="DEBUG"/>
            <appender-ref ref="INFO"/>
            <appender-ref ref="WARN"/>
            <appender-ref ref="ERROR"/>
        </root>
    </loggers>
</configuration>

修改项目配置文件中添加配置

logging:
  config:  classpath:log4j2.xml
相关推荐
枫叶_v1 个月前
【SpringBoot】10 日志持久化(log4j2)
spring boot·后端·log4j·log4j2·日志·日志持久化
xiezhr1 个月前
万字长文带你了解Java日志框架使用Java日志框架
java·log4j·logback·log4j2·java日志框架·slf4j·jul·jcl
ideal-cs1 个月前
索引:SpringCloudAlibaba分布式组件全部框架笔记
nacos·gateway·sentinel·seata·springcloud·openfeign·cloudalibaba
Z3r4y5 个月前
【Web】log4j打JNDI专题刷题记录
java·log4j·web·ctf·log4j2
紫枫叶5205 个月前
SpringCloudAlibaba-整合gateway(五)
gateway·springcloud·cloudalibaba
老马啸西风8 个月前
log-01-日志组件之 Log4j 入门介绍
java·log4j·监控·log4j2·日志·log
京东云开发者1 年前
log4j2同步日志引发的性能问题
log4j2
京东云技术团队1 年前
log4j2同步日志引发的性能问题 | 京东物流技术团队
后端·性能优化·log4j2·日志·案例
scruffybear1 年前
Spring Boot整合Log4j2.xml的问题
xml·spring boot·log4j·log4j2·exception