聊聊springboot中整合log4g2的問題

1.導入jar

springboot默認是用logback的日志框架的,所以需要排除logback,不然會出現jar依賴沖突的報錯。

 <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions><!-- 去掉springboot默認配置 -->
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency> <!-- 引入log4j2依賴 -->
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

2.配置文件

1.如果自定義瞭文件名,需要在application.yml中配置

2.默認名log4j2-spring.xml,就省下瞭在application.yml中配置
在applicaiton.yaml中添加配置

在這裡插入圖片描述

logging:
  #日志文件
  config: classpath:log4g2.xml
  level:
    com.alibaba.nacos.client.config.impl: WARN
    cn.jay.repository: trace
  file:
    #${file.name} 後期可以改成${spring.application.name}
    path: /log/${file.name}
file:
  name: dome

在config中配置log4g2.xml文件

<?xml version="1.0" encoding="UTF-8"?>
<!-- status="OFF",可以去掉,它的含義為是否記錄log4j2本身的event信息,默認是OFF -->
<configuration status="off" monitorInterval="60">
    <!--	<properties>-->
    <!--		<property name="projectName">-->
    <!--			riiot-->
    <!--		</property>-->
    <!--	</properties>-->


    <appenders>
        <!-- 開發環境用 -->
        <Console name="debug_console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n"/>
            <Filters>
                <ThresholdFilter level="DEBUG"/>
                <ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
        </Console>
        <Console name="console" target="SYSTEM_OUT">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n"/>
            <Filters>
                <ThresholdFilter level="info"/>
            </Filters>
        </Console>

        <!-- 輸出日志到文件 每天一個文件(ERROR-FATAL級別) -->
        <RollingRandomAccessFile name="AppErrorDailyRollingFile" fileName="${sys:LOG_PATH}/AppError.log" append="true" bufferedIO="false" bufferSize="256"
                                 filePattern="${sys:LOG_PATH}/AppError.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="ERROR"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 輸出日志到文件 每天一個文件(WARN級別) -->
        <RollingRandomAccessFile name="AppWarnDailyRollingFile" fileName="${sys:LOG_PATH}/AppWarn.log" append="true" bufferedIO="true" bufferSize="10240" immediateFlush="false"
                                 filePattern="${sys:LOG_PATH}/AppWarn.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="WARN"/>
                <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 輸出日志到文件 每天一個文件(INFO級別) -->
        <RollingRandomAccessFile name="AppInfoDailyRollingFile" fileName="${sys:LOG_PATH}/AppAccess.log" append="true" bufferedIO="true" bufferSize="409600" immediateFlush="false"
                                 filePattern="${sys:LOG_PATH}/AppAccess.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="INFO"/>
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 輸出日志到文件 每天一個文件(INFO級別) -->
        <RollingRandomAccessFile name="DBAccessDailyRollingFile" fileName="${sys:LOG_PATH}/DBAccess.log" append="true" bufferedIO="true" bufferSize="409600" immediateFlush="false"
                                 filePattern="${sys:LOG_PATH}/DBAccess.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="INFO"/>
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 輸出日志到文件 每天一個文件(ERROR級別) -->
        <RollingRandomAccessFile name="DBErrorDailyRollingFile" fileName="${sys:LOG_PATH}/DBError.log" append="true" bufferedIO="false" bufferSize="256"
                                 filePattern="${sys:LOG_PATH}/DBError.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="WARN"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 輸出日志到文件 每天一個文件(ERROR級別) -->
        <RollingRandomAccessFile name="SysErrorDailyRollingFile" fileName="${sys:LOG_PATH}/SysError.log" append="true" bufferedIO="false"
                                 filePattern="${sys:LOG_PATH}/SysError.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="WARN"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

        <!-- 啟動日志 -->
        <RollingRandomAccessFile name="BootLog" fileName="${sys:LOG_PATH}/Boot.log" append="false" bufferedIO="false"
                                 filePattern="${sys:LOG_PATH}/Boot.log.%d{yyyy-MM-dd}.log">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [%t] {%c}-%m%n" />
            <Filters>
                <ThresholdFilter level="INFO"/>
            </Filters>
            <Policies>
                <TimeBasedTriggeringPolicy modulate="true" interval="1"/>
            </Policies>
        </RollingRandomAccessFile>

     <!--   <Kafka name="SyncKafka" topic="logs" syncSend="false">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [riiot] [${jar.name}] [%X{ip}] [dev] [%t] {%c}-%m"/>
            <Property name="bootstrap.servers">192.168.1.49:9092</Property>
            <Property name="acks">0</Property>
            <Property name="compression.type">gzip</Property>
            <Property name="max.block.ms">10000</Property>
            <Filters>
                <ThresholdFilter level="INFO"/>
            </Filters>
        </Kafka>
        &lt;!&ndash; 異步發送kafka &ndash;&gt;
        <Async name="Kafka" bufferSize="512" blocking="false">
            <AppenderRef ref="SyncKafka"/>
        </Async>

        <Kafka name="KafkaErrLog" topic="errLogTopic" syncSend="false">
            <PatternLayout pattern="[%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p] [%traceId] [riiot] [${jar.name}] [%X{ip}] [dev] [%t] {%c}-%m"/>
            <Property name="bootstrap.servers">192.168.1.49:9092</Property>
            <Property name="acks">0</Property>
            <Property name="compression.type">gzip</Property>
            <Filters>
                <ThresholdFilter level="ERROR"/>
            </Filters>
        </Kafka>
        &lt;!&ndash; 異步發送kafka &ndash;&gt;
        <Async name="AsyncKafkaErrLog" bufferSize="512" blocking="false">
            <AppenderRef ref="KafkaErrLog"/>
        </Async>
-->
    </appenders>

    <loggers>
        <!-- 隻把包名是com.cmsr的日志輸出到文件 -->
        <logger name="com.cmsr" additivity="false" level="debug">
            <appender-ref ref="AppErrorDailyRollingFile" />
            <appender-ref ref="AppWarnDailyRollingFile" />
            <appender-ref ref="AppInfoDailyRollingFile" />
            <appender-ref ref="console" />

            <appender-ref ref="debug_console" />
        </logger>

        <!-- 隻把包名是com.cmsr.sicp.common.mybatis(DB正常執行,異常分別寫到不同的log文件)的日志輸出到文件 -->
        <logger name="com.cmsr.sicp.common.mybatis" additivity="false" level="debug">
            <appender-ref ref="DBAccessDailyRollingFile" />
            <appender-ref ref="DBErrorDailyRollingFile" />
            <appender-ref ref="console" />

        </logger>

        <!-- 過濾springframework輸出,提高啟動速度 (生產環境中需要整體刪除)-->
        <logger name="org.springframework" additivity="false" level="warn">
            <appender-ref ref="console" />
        </logger>

        <!-- 啟動日志單獨輸出 -->
        <logger name="com.cmsr.Launcher" additivity="false" level="info">
            <appender-ref ref="BootLog" />
            <appender-ref ref="console" />
        </logger>

        <!-- 定義全局。其他包的日志隻輸出到控制臺,不輸出到日志文件 -->
        <root level="error">
            <appender-ref ref="SysErrorDailyRollingFile" />
            <appender-ref ref="console" />

        </root>
    </loggers>
</configuration>

到此這篇關於springboot中整合log4g2的文章就介紹到這瞭,更多相關springboot整合log4g2內容請搜索WalkonNet以前的文章或繼續瀏覽下面的相關文章希望大傢以後多多支持WalkonNet!

推薦閱讀: