Log4j2-27-log4j2 与 springboot 整合例子

1.去除默认的依赖并导入log4j2、lombok依赖

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


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

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

上面log4j2是直接导入了SpringBoot对应的版本,也可以导入spring的版本,两种二选一:

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-apiartifactId>
    <version>2.11.2version>
dependency>

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-coreartifactId>
    <version>2.11.2version>
dependency>

<dependency>
    <groupId>org.apache.logging.log4jgroupId>
    <artifactId>log4j-slf4j-implartifactId>
    <version>2.11.2version>
dependency>

<dependency>
    <groupId>org.slf4jgroupId>
    <artifactId>slf4j-apiartifactId>
    <version>1.7.30version>
dependency>

2.在资源目录下新建log4j2.xml文件

1)通用日志记录:所有等级的日志都记录在同一个文件中



<configuration status="info">
    <Properties>
        
        <Property name="LOG_HOME">E:/logs/app/Property>
        <Property name="APP_NAME">logProperty>
        <Property name="LOG_PATTERN"
                  value="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread][%class{36}:%line] - %msg%n">Property>
    Properties>
    <Appenders>
        
        <Console name="Console" target="SYSTEM_OUT">
            
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="${LOG_PATTERN}"/>
        Console>

        
        <RollingFile name="RollingFile" fileName="${LOG_HOME}/${APP_NAME}.log"
                     filePattern="${LOG_HOME}/%d{yyyy-MM}/%d{yyyy-MM-dd}_%i.log">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100MB"/>
            Policies>
            
            <DefaultRolloverStrategy max="365"/>
        RollingFile>
    Appenders>
    
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="RollingFile"/>
        Root>
    Loggers>

configuration>

在配置中指定了日志存储的目录是E:\logs\app,日志文件使用日期作为重要区分的条件。

2)分级日志记录:不同等级的日志记录到对应的文件中



<configuration status="info">
    <Properties>
        
        <Property name="LOG_HOME">E:/logs/app/Property>
        <Property name="LOG_PATTERN"
                  value="%date{yyyy-MM-dd HH:mm:ss.SSS} %-5level [%thread][%class{36}:%line] - %msg%n">Property>
    Properties>
    <Appenders>
        
        <Console name="Console" target="SYSTEM_OUT">
            
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            
            <PatternLayout pattern="${LOG_PATTERN}"/>
        Console>
        
        <RollingFile name="infoAppender" fileName="${LOG_HOME}/info.log"
                     filePattern="${LOG_HOME}/%d{yyyy-MM}/%d{yyyy-MM-dd}_info.log">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100MB"/>
            Policies>
        RollingFile>
        
        <RollingFile name="errorAppender" fileName="${LOG_HOME}/error.log"
                     filePattern="${LOG_HOME}/%d{yyyy-MM}/%d{yyyy-MM-dd}_error.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="${LOG_PATTERN}"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100MB" />
            Policies>
        RollingFile>
    Appenders>
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="infoAppender"/>
            <AppenderRef ref="errorAppender"/>
        Root>
    Loggers>

configuration>

对于是否分级记录,根据需求决定。两者选一即可。

3.在配置文件配置log4j2文件的位置

  • application.properties
logging.config = classpath:log4j2.xml

4.添加测试方法进行测试

package com.zys.springboottestexample;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
public class SpringbootTestExampleApplicationTests {

    //创建日志对象
    Logger logger = LogManager.getLogger(this.getClass());

    @Test
    void test() {
        logger.info("我是info日志");
        logger.warn("我是warn日志");
        logger.error("我是error日志");
    }

}

参考资料

https://www.cnblogs.com/zys2019/p/14798374.html

Log4j2-27-log4j2 与 springboot 整合例子_第1张图片

你可能感兴趣的:(log,log4j,spring,boot,单元测试)