完美spring boot 使用log4j2按级别输出到不同文件

1. pom.xml引入需要的jar

<dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

        
        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>

        <dependency>
            <groupId>org.projectlombokgroupId>
            <artifactId>lombokartifactId>
            <optional>trueoptional>
        dependency>

2. 编写log4j2配置文件(注意看注释)





<Configuration status="WARN" monitorInterval="30">
    
    <Properties>
        <Property name="APP_NAME">luis-log4j2Property>
        <Property name="LOG_FILE_PATH">${sys:user.home}/logs/${APP_NAME}Property>
        <Property name="PATTERN_FORMAT">%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%nProperty>
    Properties>

    <Appenders>
        
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="${PATTERN_FORMAT}"/>
        Console>


        
        <RollingFile name="RollingInfoFile" fileName="${LOG_FILE_PATH}/info.log"
                     filePattern="${LOG_FILE_PATH}/$${date:yyyyMM}/info-%d{yyyyMMdd}-%i.log.gz">
            
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>

            <PatternLayout>
                <pattern>${PATTERN_FORMAT}pattern>
            PatternLayout>

            <Policies>
                
                <OnStartupTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
                <TimeBasedTriggeringPolicy/>
            Policies>
        RollingFile>

        
        <RollingFile name="RollingWarnFile" fileName="${LOG_FILE_PATH}/warn.log"
                     filePattern="${LOG_FILE_PATH}/$${date:yyyyMM}/warn-%d{yyyyMMdd}-%i.log.gz">
            
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>

            <PatternLayout>
                <pattern>${PATTERN_FORMAT}pattern>
            PatternLayout>

            <Policies>
                
                <OnStartupTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
                <TimeBasedTriggeringPolicy/>
            Policies>
        RollingFile>

        
        <RollingFile name="RollingErrorFile" fileName="${LOG_FILE_PATH}/error.log"
                     filePattern="${LOG_FILE_PATH}/$${date:yyyyMM}/error-%d{yyyyMMdd}-%i.log.gz">
            
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>

            <PatternLayout>
                <pattern>${PATTERN_FORMAT}pattern>
            PatternLayout>

            <Policies>
                
                <OnStartupTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
                <TimeBasedTriggeringPolicy/>
            Policies>
        RollingFile>
    Appenders>

    <Loggers>

        
        <Logger name="org.springframework" level="INFO"/>
        <Logger name="org.mybatis" level="INFO"/>

        
        <Logger name="com.luis" level="INFO"/>

        
        <Root level="ALL">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingInfoFile"/>
            <AppenderRef ref="RollingWarnFile"/>
            <AppenderRef ref="RollingErrorFile"/>
        Root>
    Loggers>

Configuration>

注意:按日志级别输出到不同的文件,需要使用Filters,并且先拒绝不符合的,在接受符合的顺序;

3. 编写测试类

@Log4j2
@RestController
public class LombokLogController {

    @GetMapping("lombokLog")
    public String lombokLog(){
        log.trace("lombok info log!");
        log.debug("lombok debug log!");
        log.info("lombok info log!");
        log.warn("lombok warn log!");
        log.error("lombok error log!");
        log.fatal("lombok fatal log!");
        return "use lombok log!";
    }
}

4. 查看结果

完美spring boot 使用log4j2按级别输出到不同文件_第1张图片
完美spring boot 使用log4j2按级别输出到不同文件_第2张图片
warn级别
完美spring boot 使用log4j2按级别输出到不同文件_第3张图片

你可能感兴趣的:(Java)