十五、springboot整合log4j2

前言

日志记录对系统来说必不可少,spring boot中常用的日志组件有log4j、logback、log4j2,其中logback是spring boot默认的,已自带;选用log4j2就可以,其他的不必去关注;

一、pom文件引入依赖

 <dependencies>
        <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-starter-log4j2artifactId>
        dependency>
        
        <dependency>
            <groupId>com.lmaxgroupId>
            <artifactId>disruptorartifactId>
            <version>3.4.2version>
        dependency>
        
    dependencies>

二、增加日志配置文件log4j2-dev.xml








<Configuration status="INFO" monitorInterval="30">
    <Properties>
        
        <Property name="logpath">/Log4j/logs/log/devProperty>
    Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                    pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
        Console>
        <RollingFile name="debug" fileName="${logpath}/debug/erp_debug.log"
                     filePattern="${logpath}/debug/erp_debug_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="info" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/debug" maxDepth="1">
                    <IfFileName glob="erp_debug_*.log"/>
                    <IfLastModified age="15d"/>
                Delete>
            DefaultRolloverStrategy>
        RollingFile>
        <RollingFile name="info" fileName="${logpath}/info/erp_info.log"
                     filePattern="${logpath}/info/erp_info_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/info" maxDepth="1">
                    <IfFileName glob="erp_info_*.log"/>
                    <IfLastModified age="15d"/>
                Delete>
            DefaultRolloverStrategy>
        RollingFile>
        <RollingFile name="warn" fileName="${logpath}/warn/erp_warn.log"
                     filePattern="${logpath}/warn/erp_warn_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/warn" maxDepth="1">
                    <IfFileName glob="erp_warn_*.log"/>
                    <IfLastModified age="15d"/>
                Delete>
            DefaultRolloverStrategy>
        RollingFile>
        <RollingFile name="error" fileName="${logpath}/error/erp_error.log"
                     filePattern="${logpath}/error/erp_error_%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            Filters>
            <PatternLayout pattern="[%d][%-5p][%t] %m (%F:%L)%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy interval="24" modulate="true"/>
                
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/error" maxDepth="1">
                    <IfFileName glob="erp_error_*.log"/>
                    
                    <IfLastModified age="15d"/>
                Delete>
            DefaultRolloverStrategy>
        RollingFile>

    Appenders>
    
    <Loggers>
        <Root level="info">
            <AppenderRef ref="Console"/>
            <AppenderRef ref="debug"/>
            <AppenderRef ref="info"/>
            <AppenderRef ref="warn"/>
            <AppenderRef ref="error"/>
        Root>
    Loggers>
Configuration>

配置文件中增加配置

#################### 7 log4j2  ###################
logging.config=classpath:log4j2-dev.xml
logging.level.org.springframework=INFO

你可能感兴趣的:(springboot)