SpringBoot 切換日誌

1,切換成log4j

①,排除logback的包html

②,排除轉換包log4j-to-slf4jweb

③,引入slf4j-log4j12spring

④,官方說明apache

⑤,最終pom.xml 以下app

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>ch.qos.logback</groupId>
					<artifactId>logback-classic</artifactId>
				</exclusion>
				<exclusion>
					<groupId>org.apache.logging.log4j</groupId>
					<artifactId>log4j-to-slf4j</artifactId>
				</exclusion>
			</exclusions>
		</dependency>
	<dependency>
	    <groupId>org.slf4j</groupId>
	    <artifactId>slf4j-log4j12</artifactId>
	</dependency>

⑥,雖然能夠切換成log4j,但 不建議這麼作,就是由於log4j有性能問題,纔出來log4j2和logback。默認的logback已經能知足咱們不少場景了。async

⑦,log4j.xmlspring-boot

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration>

    <!-- 將日誌信息輸出到控制檯 -->
    <appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
        <!-- 設置日誌輸出的樣式 -->
        <layout class="org.apache.log4j.PatternLayout">
            <!-- 設置日誌輸出的格式 -->
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n" />
        </layout>
        <!--過濾器設置輸出的級別-->
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <!-- 設置日誌輸出的最小級別 -->
            <param name="levelMin" value="WARN" />
            <!-- 設置日誌輸出的最大級別 -->
            <param name="levelMax" value="ERROR" />
            <!-- 設置日誌輸出的xxx,默認是false -->
            <param name="AcceptOnMatch" value="true" />
        </filter>
    </appender>

    <!-- 將日誌信息輸出到文件,可是當文件的大小達到某個閾值的時候,日誌文件會自動回滾 -->
    <appender name="RollingFileAppender" class="org.apache.log4j.RollingFileAppender">
        <!-- 設置日誌信息輸出文件全路徑名 -->
        <param name="File" value="D:/log4j/RollingFileAppender.log" />
        <!-- 設置是否在從新啓動服務時,在原有日誌的基礎添加新日誌 -->
        <param name="Append" value="true" />
        <!-- 設置保存備份回滾日誌的最大個數 -->
        <param name="MaxBackupIndex" value="10" />
        <!-- 設置當日志文件達到此閾值的時候自動回滾,單位能夠是KB,MB,GB,默認單位是KB -->
        <param name="MaxFileSize" value="10KB" />
        <!-- 設置日誌輸出的樣式 -->
        <layout class="org.apache.log4j.PatternLayout">
            <!-- 設置日誌輸出的格式 -->
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n" />
        </layout>
    </appender>

    <!-- 將日誌信息輸出到文件,能夠配置多久產生一個新的日誌信息文件 -->
    <appender name="DailyRollingFileAppender" class="org.apache.log4j.DailyRollingFileAppender">
        <!-- 設置日誌信息輸出文件全路徑名 -->
        <param name="File" value="D:/log4j/DailyRollingFileAppender.log" />
        <!-- 設置日誌每分鐘回滾一次,即產生一個新的日誌文件 -->
        <param name="DatePattern" value="'.'yyyy-MM-dd-HH-mm'.log'" />
        <!-- 設置日誌輸出的樣式 -->
        <layout class="org.apache.log4j.PatternLayout">
            <!-- 設置日誌輸出的格式 -->
            <param name="ConversionPattern" value="[%d{yyyy-MM-dd HH:mm:ss:SSS}] [%-5p] [method:%l]%n%m%n%n" />
        </layout>
    </appender>



    <!-- 根logger的設置-->
    <root>
        <level value ="INFO"/>
        <appender-ref ref="ConsoleAppender"/>
        <appender-ref ref="DailyRollingFileAppender"/>
    </root>

</log4j:configuration>

2,切換成log4j2

log4j2的切換跟切換log4j是不一樣的,SpringBoot提供了log4j2的starter性能

①,排除spring-boot-starter-loggingspa

②,引入spring-boot-starter-log4j2debug

③,pom.xml

<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-web</artifactId>
			<exclusions>
				<exclusion>
					<groupId>org.springframework.boot</groupId>
					<artifactId>spring-boot-starter-logging</artifactId>
				</exclusion>
			</exclusions>
		</dependency>

		<dependency>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-starter-log4j2</artifactId>
		</dependency>

 ④,log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>    
<configuration status="error">  
<!--     先定義全部的appender -->  
    <appenders>  
<!--         這個輸出控制檯的配置 -->  
        <Console name="Console" target="SYSTEM_OUT">  
<!--             控制檯只輸出level及以上級別的信息(onMatch),其餘的直接拒絕(onMismatch) -->  
            <ThresholdFilter level="trace" onMatch="ACCEPT" onMismatch="DENY"/>  
<!--             這個都知道是輸出日誌的格式 -->  
            <PatternLayout pattern="%d{HH:mm:ss.SSS} log4j22xiaosu2 %-5level %class{36} %L %M - %msg%xEx%n"/>  
        </Console>  
        <RollingFile name="RollingFile" fileName="logs/web.log"  
                     filePattern="logs/$${date:yyyy-MM}/web-%d{MM-dd-yyyy}-%i.log.gz">  
            <PatternLayout pattern="%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n"/>  
            <SizeBasedTriggeringPolicy size="2MB"/>  
        </RollingFile>  
    </appenders>  
      
<!--     而後定義logger,只有定義了logger並引入的appender,appender纔會生效 -->  
    <loggers>  
<!--         創建一個默認的root的logger -->  
        <root level="info">  
            <appender-ref ref="Console"/>  
            <appender-ref ref="RollingFile"/>  
        </root>  
  
    </loggers>  
</configuration>

3,彩蛋

①,logback-spring.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="60 seconds" debug="false">

    <property name="LOG_HOME" value="${user.dir}/logs/webflux"/>
    <property name="ENCODER_PATTERN"
              value="%d{yyyy-MM-dd HH:mm:ss.SSS}[%thread] %-5level %logger{80} - %msg%n"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>${ENCODER_PATTERN}</pattern>
        </layout>
    </appender>

    <!-- 訪問日誌 -->
    <appender name="COMMON_ACCESS" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/common-access.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/history/common-access-%d{yyyyMMdd}.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>${ENCODER_PATTERN}</pattern>
        </encoder>
    </appender>



    <!-- 系統INFO日誌 -->
    <appender name="INFO_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/info.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/history/info-%d{yyyyMMdd}.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>${ENCODER_PATTERN}</pattern>
        </encoder>
    </appender>

    <!-- 系統ERROR日誌 -->
    <appender name="ERROR_LOG" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_HOME}/error.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/history/error-%d{yyyyMMdd}.log</fileNamePattern>
            <maxHistory>7</maxHistory>
        </rollingPolicy>
        <encoder charset="UTF-8">
            <pattern>${ENCODER_PATTERN}</pattern>
        </encoder>
    </appender>

    <appender name="ASYNC_LOG" class="ch.qos.logback.classic.AsyncAppender">
        <discardingThreshold>0</discardingThreshold>
        <queueSize>1234</queueSize>
        <appender-ref ref="INFO_LOG"/>
    </appender>

    <logger name="asyncLog" level="INFO" additivity="false">
        <appender-ref ref="ASYNC_LOG"/>
    </logger>


    <logger name="errorLog" level="ERROR" additivity="false">
        <appender-ref ref="ERROR_LOG"/>
    </logger>

    <logger name="commonAccess" level="INFO" additivity="false">
        <appender-ref ref="COMMON_ACCESS"/>
    </logger>





    <root level="INFO">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="INFO_LOG"/>
        <appender-ref ref="ERROR_LOG"/>
    </root>

</configuration>
相關文章
相關標籤/搜索