美文网首页
spring boots 配置log4j2 日志

spring boots 配置log4j2 日志

作者: 陈凌川 | 来源:发表于2018-11-20 17:40 被阅读0次

    在spring boots 配置log4j2 日志

    1.引入依赖

    在 pom.xml文件中配置:

    <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.apache.logging.log4j</groupId>
        <artifactId>log4j-api</artifactId>
        <version>2.8.1</version>
    </dependency>
    <dependency>
        <groupId>org.apache.logging.log4j</groupId>
        <artifactId>log4j-core</artifactId>
        <version>2.8.1</version>
    </dependency>
    

    2.添加log4j2的配置文件

    在项目路径下添加log4j2.xml的配置文件

    <?xml version="1.0" encoding="UTF-8"?> 
    <Configuration status="WARN"  monitorInterval="300">
        <!-- 定义引用名 -->
        <Properties>
            <!-- 输出样式 -->
            <Property name="Console_pattern">%5c{36}:%L %M [%5level] >> %msg%n</Property>
            <!-- 输出路径 -->
            <Property name="file_name">logs/common.log</Property>
            <Property name="info_name">logs/Info.log</Property>
            <Property name="html_name">logs/log.html</Property>
            <!-- 备份路径 -->
            <Property name="rolling_file">logs/$${date:yyyy-MM-dd}/common-%d{yyy-MM-dd}-%i.logg.gz</Property>
            <Property name="rolling_info">logs/$${date:yyyy-MM}/Info-%d{yyy-MM-dd}-%i.logg.gz</Property>
            <!-- 日志最小切割单位 -->
            <Property name="file_size">20 MB</Property>
            <!-- 日志输出级别 -->
            <Property name="level_name">debug</Property>
        </Properties>
        
        <!-- 定义appender -->
        <Appenders>
            <!--这个是控制台的配置-->
            <Console name="Console" target="SYSTEM_OUT">
                <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
                <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>
                <!--这个都知道是输出日志的格式-->
                <PatternLayout pattern="${Console_pattern}"/>
            </Console>
            <!--<File name="File" fileName="${html_name}">-->
                <!--<HTMLLayout title="ocg-Common"/>-->
            <!--</File>-->
            <!-- 这个是回滚输出所以配置 -->
            <RollingFile name="RollingFile" fileName="${file_name}" filePattern="${rolling_file}">
                <PatternLayout>
                <pattern>[%d{yyyy-MM-dd HH:mm:ss z}] %t %5c{36}:%L %M %5level ---- %msg%xEx%n</pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                    <SizeBasedTriggeringPolicy size="${file_size}"/>
                </Policies> 
                <DefaultRolloverStrategy fileIndex="max" max="50"/>
            </RollingFile>
            <!-- 这个是回滚输出info配置 -->
            <RollingFile name="RollingInfo" fileName="${info_name}" filePattern="${rolling_info}">
                <PatternLayout>
                <pattern>[%d{yyyy-MM-dd HH:mm:ss z}] %t %5c{36}:%L %M %5level ---- %msg%xEx%n</pattern>
                </PatternLayout>
                <Policies>
                    <TimeBasedTriggeringPolicy interval="1" modulate="true" />
                    <SizeBasedTriggeringPolicy size="${file_size}"/>
                </Policies> 
                    <Filters>  
                        <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
                        <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="deny"/>   
                    </Filters>
                
                <DefaultRolloverStrategy fileIndex="max" max="50"/>
            </RollingFile>
            
             <!-- 异步日志 -->  
            <Async name="Async">    
                <AppenderRef ref="RollingFile" />   
                <AppenderRef ref="RollingInfo"/>   
            </Async>
            
        </Appenders>
        
        <!-- 日志类别 -->
        <Loggers>
            
             <!-- 使用异步 -->
             <Logger name="asyncConsole" additivity="false" level="${level_name}">  
                <AppenderRef ref="Async"/>  
            </Logger>
            
            <!-- 定义根日志类别  查看level -->   
            <Root level="${level_name}"> 
                <AppenderRef ref="Console"/>
                <AppenderRef ref="Async" />
                <AppenderRef ref="File" />
            </Root>
            
        </Loggers>
    </Configuration>
    

    3.在spring boots 的配置文件中配置日志属性

    在spring boots 的application.yaml 配置日志属性

    # 日志配置
    debug: false
    logging:
      config: classpath:log4j2.xml
    

    4.在类中使用日志

    Logger logger = LogManager.getLogger ( this.getClass () );
    

    5.注意

    1. application.yaml 中logging.config:后面一定要加 classpath:,不然会报错:
    Caused by: java.io.FileNotFoundException: D:\SL\app-mini\log4j2.xml (系统找不到指定的文件。)
        at java.io.FileInputStream.open(Native Method) ~[?:1.8.0_31]
        at java.io.FileInputStream.<init>(FileInputStream.java:138) ~[?:1.8.0_31]
        at java.io.FileInputStream.<init>(FileInputStream.java:93) ~[?:1.8.0_31]
        at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:90) ~[?:1.8.0_31]
        at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:188) ~[?:1.8.0_31]
        at java.net.URL.openStream(URL.java:1038) ~[?:1.8.0_31]
        at org.springframework.boot.context.logging.LoggingApplicationListener.initializeSystem(LoggingApplicationListener.java:294) ~[spring-boot-2.1.0.RELEASE.jar:2.1.0.RELEASE]
        ... 39 more
    
    1. pom.xml文件中需要配置:
    <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>
    

    否则将会报错:

    Caused by: java.lang.IllegalStateException: Logback configuration error detected: 
    

    参考:Spring Boot与Log4j2集成之java.lang.IllegalStateException: Logback configuration error detected:

    相关文章

      网友评论

          本文标题:spring boots 配置log4j2 日志

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