springBoot整合log4j2

文章目录

      • 什么是log4j2
        • springBoot依赖的引入
      • 接下来是log4j2的示例配置
        • 首先在application.yml制定采用哪个配置文件
        • 在resources目录下新建log4j2.xml文件

什么是log4j2

Apache Log4j 2是对Log4j的升级,它比其前身Log4j 1.x提供了重大改进,并提供了Logback中可用的许多改进,同时修复了Logback架构中的一些问题。是目前最优秀的Java日志框架,没有之一。

springBoot依赖的引入


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-log4j2artifactId>
        dependency>

网上好多说要这样排除掉springBoot自带的logging依赖(其实还会有问题 这样很可能排除不干净 log4j的日志文件就不会生成 也没有报错信息 很难排查)


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starter-webartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

应该采用下面这种方式


        <dependency>
            <groupId>org.springframework.bootgroupId>
            <artifactId>spring-boot-starterartifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.bootgroupId>
                    <artifactId>spring-boot-starter-loggingartifactId>
                exclusion>
            exclusions>
        dependency>

接下来是log4j2的示例配置

首先在application.yml制定采用哪个配置文件

logging:
  config:
    classpath: log4j2.xml

在resources目录下新建log4j2.xml文件

这里就不讲解啦 其实大部分跟之前的logback差不多不懂的自行百度吧





<configuration status="WARN" monitorInterval="300">
    
    <appenders>
        
        <console name="Console" target="SYSTEM_OUT">
            
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        console>
        
        <File name="log" fileName="exam/exam.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        File>
        
        <RollingFile name="RollingFileInfo" fileName="exam/logs/info.log"
                     filePattern="exam/logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
			<Filters>
                
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                
                <ThresholdFilter level="info" onMatch="NEUTRAL" onMismatch="DENY"/>
            Filters>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>
        RollingFile>
        <RollingFile name="RollingFileWarn" fileName="exam/logs/warn.log"
                     filePattern="exam/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
			<Filters>
                
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                
                <ThresholdFilter level="warn" onMatch="NEUTRAL" onMismatch="DENY"/>
            Filters>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>
            
            <DefaultRolloverStrategy max="20"/>
        RollingFile>
        <RollingFile name="RollingFileError" fileName="exam/logs/error.log"
                     filePattern="exam/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            Policies>
        RollingFile>
    appenders>
    
    <loggers>
        <root level="all">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
        root>
    loggers>
configuration>

你可能感兴趣的:(springBoot,spring,boot,java,spring)