spring boot—集成log4j2日志框架


市场上的日志框架

  1)日志门面最常用的是slf4j

  2)日志实现最常用的是logbacklog4j2, 当然log4j也还有人使用,只是log4j比较老旧了

  3)log4jlogbackslf4j都是出自同一个人之手,他首先写了log4j,后来发现log4j有性能问题,就又写了logback . . .
cd4356
  4)日志门面slf4j不是一个真正的日志实现,它是抽象层的api框架,它允许你在后台使用任意一个日志实现框架,实现系统与日志实现框架的解耦


spring boot日志框架关系

  1)spring boot底层使用slf4j + logback框架来实现日志记录。

  2)slf4j是日志抽象层api框架,logback是日志实现层框架。也就会说我们是面向slf4j来编程的,具体靠logback来实现。

面向slf4j编程

  1)slf4j官方文档

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class HelloWorld {
  public static void main(String[] args) {
  	//获取记录器
    Logger logger = LoggerFactory.getLogger(HelloWorld.class);
    //通过记录器记录日志信息
    logger.info("Hello World");
  }
}

具体靠logback来实现

  1)logback的具体实现就不需要我们来操心了,当然我们可以修改一些默认配置。

#日志记录
logging:
  #日志级别
  level:
    #可以调整不同目录的日志级别(注意:必须指定包,否则报错)
    com.cd.example.one: trace
    com.cd.example.two: debug
    com.cd.example.three: info
  #日志文件
  file:
    #日志文件完整路径(含文件名)
    name: D:\idea\logs\example-loggin.log
    #日志文件根路径(spring boot默认使用spring.log作为日志文件)
    #path: D:\idea\log
    #文件大小(默认10MB)
    max-size: 10MB
    #文件保存周期(默认7天,七天之后就会删除)
    max-history: 7
  #日志输出格式
  pattern:
    #在控制台输出的日志格式
    console: -%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n
    #在文件输出的日志格式
    file: '%d{yyyy-MM-dd hh:mm:ss} -- %-5level -- [%thread] -- %logger{50} --- %msg %n'

移除默认日志框架

  1)spring boot默认日志框架都依赖于spring-boot-starter-logging(具体在pom.xml文件中按crl + shift + alt + u查看依赖树)。
cd4356
   2)移除默认日志框架的方法:

      1)打开依赖树,选中spring-boot-starter-logging,快捷键shift + delete移除。

      2)spring-boot-starter-web依赖中使用<exclusion>移除默认日志框架

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-web</artifactId>
	<!--移除默认日志框架-->
	<exclusions>
		<exclusion>
			<artifactId>spring-boot-starter-logging</artifactId>
			<groupId>org.springframework.boot</groupId>
		</exclusion>
	</exclusions>
</dependency>

切换为log4j2日志框架

   1)添加spring-boot-starter-log4j2依赖

<!--log4j2日志框架-->
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

   2)spring boot官方文档的提示,spring boot集成log4j2框架,那么日志配置文件只能是log4j2.xmllog4j2-spring.xml

   3)spring boot官方建议使用log4j2-spring.xml作为log4j2框架的日志配置文件,因为带-spring后缀的配置文件可以使用spring boot提供的一些高级功能,如profile多环境日志输出功能

log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration >
    <Appenders>
        <!--当前为dev环境时,该配置生效-->
        <springProfile name="dev">
            <!--控制台输出日志信息-->
            <Console name="Console" target="SYSTEM_OUT">
                <!--日志格式-->
                <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
            </Console>
        </springProfile>

        <!--日志文件记录日志信息-->
        <RollingFile name="log_file"
                     fileName="D:\idea\logs\example.log"
                     filePattern="D:\idea\logs\example-%d{yyyy-MM-dd}-%i.log">
            <!--文件滚动策略-->
            <Policies>
                <!--基于时间的滚动策略,默认是1小时,我这里设置24小时滚动一次-->
                <TimeBasedTriggeringPolicy interval="24"/>
                <!--基于大小的滚动策略-->
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
			<!--日志格式-->
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
        </RollingFile>
    </Appenders>
    
    <Loggers>
    	<!--只有在Root中引入appender,appender才会生效-->
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="log_file"/>
        </Root>
    </Loggers>
</configuration>



  1) 部分开发者在记录日志文件时,喜欢将不同级别的日志信息分开独自记录:比如debug日志信息单独记录到一个日志文件中,info日志信息记录到一个日志文件中,warn日志信息记录到一个日志文件中,error日志信息记录到一个日志文件中,那么就可以使用临界值过滤器 <ThresholdFilter/> 来实现

  2) 下面配置的意思是:一个临界值过滤器接受debug级别以上的日志、另一个临界值过滤器拒绝info级别以上的日志。相当于只记录debug级别的信息

<!--接受debug级别以上的日志-->
<ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
<!--拒绝info级别以上的日志-->
<ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>

log4j2-spring.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration >
    <!--参数配置-->
    <Properties>
        <!--日志文件根路径-->
        <property name="log_path" value="D:\idea\logs"/>
    </Properties>

    <!--日志输出目标-->
    <Appenders>
        <!--当前为dev环境时,该配置生效-->
        <springProfile name="dev">
            <!--控制台输出日志信息-->
            <Console name="Console" target="SYSTEM_OUT">
                <!--日志格式-->
                <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
            </Console>
        </springProfile>

        <!--日志文件输出debug级别日志信息-->
        <RollingFile name="debug_file"
                     fileName="${log_path}\debug\example_debug.log"
                     filePattern="${log_path}\debug\example_debug-%d{yyyy-MM-dd}-%i.log">
            <!--文件滚动策略-->
            <Policies>
                <!--基于时间的滚动策略,默认是1小时,我这里设置24小时滚动一次-->
                <TimeBasedTriggeringPolicy interval="24"/>
                <!--基于大小的滚动策略-->
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
			<!--日志格式-->
			<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
            <!--临界值过滤器-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
        </RollingFile>

        <!--日志文件输出日志信息-->
        <RollingFile name="info_file"
                     fileName="${log_path}\info\example_info.log"
                     filePattern="${log_path}\info\example_info-%d{yyyy-MM-dd}-%i.log">
            <!--文件滚动策略-->
            <Policies>
                <!--基于时间的滚动策略,默认是1小时,我这里设置24小时滚动一次-->
                <TimeBasedTriggeringPolicy interval="24"/>
                <!--基于大小的滚动策略-->
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <!--日志格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
            <!--临界值过滤器-->
            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            <ThresholdFilter level="warn" onMatch="DNEY" onMismatch="NEUTRAL"/>
        </RollingFile>

        <!--日志文件输出日志信息-->
        <RollingFile name="warn_file"
                     fileName="${log_path}\warn\example_warn.log"
                     filePattern="${log_path}\warn\example_warn-%d{yyyy-MM-dd}-%i.log">
            <!--文件滚动策略-->
            <Policies>
                <!--基于时间的滚动策略,默认是1小时,我这里设置24小时滚动一次-->
                <TimeBasedTriggeringPolicy interval="24"/>
                <!--基于大小的滚动策略-->
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <!--日志格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
            <!--临界值过滤器-->
            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
        </RollingFile>

        <!--日志文件输出日志信息-->
        <RollingFile name="error_file"
                     fileName="${log_path}\error\example_error.log"
                     filePattern="${log_path}\error\example_error-%d{yyyy-MM-dd}-%i.log">
            <!--文件滚动策略-->
            <Policies>
                <!--基于时间的滚动策略,默认是1小时,我这里设置24小时滚动一次-->
                <TimeBasedTriggeringPolicy interval="24"/>
                <!--基于大小的滚动策略-->
                <SizeBasedTriggeringPolicy size="10MB"/>
            </Policies>
            <!--日志格式-->
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} -- %-5level -- [%thread] -- %logger{50} ---> %msg %n"/>
            <!--临界值过滤器-->
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
        </RollingFile>
    </Appenders>

    <Loggers>
        <!--只有在Root中引入appender,appender才会生效-->
        <Root level="info">
            <AppenderRef ref="Console" />
            <AppenderRef ref="debug_file"/>
            <AppenderRef ref="info_file"/>
            <AppenderRef ref="warn_file"/>
            <AppenderRef ref="error_file"/>
        </Root>
    </Loggers>
</configuration>
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章