spring boot 使用log4j2

不引入默认的log 引入log4j2

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <!--排队默认的 log-->
            <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>

添加log4j2的配置文件

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="warn">
    <properties>

        <Property name="app_name">spring-boot-log-log4j2</Property>
        <Property name="log_path">logs</Property>
        <!-- 控制台格式 -->
        <property name="lOBACK_CONSOLE_PATTERN" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %highlight{%p} %style{%logger}{Normal,cyan} :%L %style{->}{yellow} %msg%n" />
        <!-- 日志文本格式 -->
        <property name="lOBACK_FILE_PATTERN" value="[%d{yyyy-MM-dd HH:mm:ss}] [%thread] {%p} %logger{50} :%L -> %msg%n" />
    </properties>
    <appenders>
        <console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${lOBACK_CONSOLE_PATTERN}" />
        </console>

        <RollingFile name="RollingFileInfo" fileName="${log_path}/${date:yyyy-MM-dd}/info.log"
                     filePattern="${log_path}/${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log.gz">
            <Filters>
                <ThresholdFilter level="INFO" />
            </Filters>
            <PatternLayout pattern="${lOBACK_FILE_PATTERN}" />
            <Policies>
                <!-- 归档每天的文件 -->
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <!-- 限制单个文件大小 -->
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
            <!-- 限制每天文件个数 -->
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        </RollingFile>

        <RollingFile name="RollingFileWarn" fileName="${log_path}/${date:yyyy-MM-dd}/warn.log"
                     filePattern="${log_path}/${date:yyyy-MM-dd}/warn-%d{yyyy-MM-dd}-%i.log.gz">
            <Filters>
                <ThresholdFilter level="WARN" />
            </Filters>
            <PatternLayout pattern="${lOBACK_FILE_PATTERN}" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        </RollingFile>

        <RollingFile name="RollingFileError" fileName="${log_path}/${date:yyyy-MM-dd}/error.log"
                     filePattern="${log_path}/${date:yyyy-MMdd}/error-%d{yyyy-MM-dd}-%i.log.gz">
            <ThresholdFilter level="ERROR" />
            <PatternLayout pattern="${lOBACK_FILE_PATTERN}" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        </RollingFile>

        <RollingFile name="RollingFileFatal" fileName="${log_path}/${date:yyyy-MM-dd}/fatal.log"
                     filePattern="${log_path}/${date:yyyy-MM-dd}/fatal-%d{yyyy-MM-dd}-%i.log.gz">
            <ThresholdFilter level="FATAL" />
            <PatternLayout pattern="${lOBACK_FILE_PATTERN}" />
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                <SizeBasedTriggeringPolicy size="20 MB" />
            </Policies>
            <DefaultRolloverStrategy compressionLevel="0" max="10"/>
        </RollingFile>
    </appenders>

    <loggers>
        <root level="info">
            <appender-ref ref="Console" />
            <appender-ref ref="RollingFileInfo" />
            <appender-ref ref="RollingFileWarn" />
            <appender-ref ref="RollingFileError" />
            <appender-ref ref="RollingFileFatal" />
        </root>
    </loggers>
</configuration>

在application.yml中指明配置文件的位置

logging:
  config: classpath:logger/log4j2-spring.xml

猜你喜欢

转载自blog.csdn.net/weixin_42115175/article/details/80195652