全链路追踪—接入log4j2日志组件

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/shenzhen_zsw/article/details/89702555

全链路追踪—接入log4j2日志组件

Log4j2的接入

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.springframework.boot</groupId>
          <artifactId>spring-boot-starter-log4j2</artifactId>
       </dependency>
       
       	<dependency>
	      <groupId>com.lmax</groupId>
	      <artifactId>disruptor</artifactId>
	      <version>3.3.6</version>
	    </dependency>

 

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration status="debug">
    <appenders>
        <!--这个输出控制台的配置-->
        <Console name="STDOUT" >
             <!--控制台只输出level及以上级别的信息(onMatch),其他的直接拒绝(onMismatch)-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
            <!-- 输出日志的格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8"/>
        </Console>

        <RollingRandomAccessFile name="FILE-INFO" fileName="logs/house-info.log" filePattern="logs/house-info.%d{yyyy-MM-dd-HH}.log">
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
            <TimeBasedTriggeringPolicy  interval="1" />
            <DefaultRolloverStrategy max="1"  >
               <Delete basePath="logs" maxDepth="2">
                    <IfFileName glob="*house-info.*.log" />
                    <IfLastModified age="1h" />
               </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>


        <RollingRandomAccessFile name="FILE-DEBUG" fileName="logs/house-debug.log"  filePattern="logs/house-debug.%d{yyyy-MM-dd-HH}.log">
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS}  [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId},%X{X-B3-ParentSpanId}] - %msg%n" charset="UTF-8" />
            <TimeBasedTriggeringPolicy  interval="1" />
            <DefaultRolloverStrategy max="1"  >
               <Delete basePath="logs" maxDepth="2">
                    <IfFileName glob="*house-debug.*.log" />
                    <IfLastModified age="1h" />
               </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="FILE-WARN" fileName="logs/house-warn.log" filePattern="logs/house-warn.%d{yyyy-MM-dd-HH}.log">
            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
            <TimeBasedTriggeringPolicy  interval="1" />
             <DefaultRolloverStrategy max="1"  >
               <Delete basePath="logs" maxDepth="2">
                    <IfFileName glob="*house-warn.*.log" />
                    <IfLastModified age="1h" />
               </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>

        <RollingRandomAccessFile name="FILE-ERROR" fileName="logs/house-error.log"
            filePattern="logs/house-error.%d{yyyy-MM-dd-HH}.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY" />
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%logger{50}:%L] [%X{X-B3-TraceId},%X{X-B3-SpanId}] - %msg%n" charset="UTF-8" />
            <TimeBasedTriggeringPolicy  interval="1" />
            <DefaultRolloverStrategy max="1"  >
               <Delete basePath="logs" maxDepth="2">
                    <IfFileName glob="*house-error.*.log" />
                    <IfLastModified age="1h" />
               </Delete>
            </DefaultRolloverStrategy>
        </RollingRandomAccessFile>
    </appenders>

    <loggers>
	    <AsyncLogger name="springfox" level="OFF"></AsyncLogger>

        <AsyncRoot level="INFO" includeLocation="true" >
            <AppenderRef ref="FILE-INFO" />
            <AppenderRef ref="FILE-WARN" />
            <AppenderRef ref="FILE-ERROR" />
            <AppenderRef ref="STDOUT" />
        </AsyncRoot>
    </loggers>
</configuration>

log4j2.component.properties

AsyncLogger.RingBufferSize=10000
AsyncLoggerConfig.RingBufferSize=10000
log4j2.AsyncQueueFullPolicy=Discard
log4j2.DiscardThreshold=DEBUG

Log4j2的优点

Logback VS Log4j2

猜你喜欢

转载自blog.csdn.net/shenzhen_zsw/article/details/89702555