The way of springboot for beginners, integrating log4j2 logs

0. Preface

Log recording is essential to the system. The commonly used log components in spring boot include log4j, logback and log4j2. Logback is the default of spring boot and has been brought by itself. You can choose log4j2, and others don't need to pay attention;

1, Integrated log4j2 log component

1. Remove the default logback log framework from pom dependency:

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

        <!-- 7-1 Remove the built-in log dependency-->
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

2. Add dependency

        <!-- 7-2,Integrate log4j2  -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!-- Add extra disruptor Dependency, for solving log4j2 The log version is lower, and an error is reported-->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.4.2</version>
        </dependency>

3. Add log configuration file log4j2-dev.xml

<?xml version="1.0" encoding="UTF-8"?>
<!--set up log4j2 Self of log Level is warn-->
<!--Log level and priority sorting: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration hinder status,This is used to set log4j2 The internal information output can not be set,
    When set to trace When you see log4j2 Various internal detailed output-->
<!--monitorInterval: Log4j It can automatically detect and modify the configuration file and reconfigure itself, and set the interval in seconds-->


<Configuration status="INFO" monitorInterval="30">
    <Properties>
        <!--  Output path  -->
        <Property name="logpath">/Log4j/logs/log/dev</Property>
    </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"/>
                <!--   Maximum 50 per file M -->
                <SizeBasedTriggeringPolicy size="50 MB"/>\
            </Policies>
            <DefaultRolloverStrategy max="30">
                <Delete basePath="${logpath}/error" maxDepth="1">
                    <IfFileName glob="erp_error_*.log"/>
                    <!-- Set the maximum save time to 15 days-->
                    <IfLastModified age="15d"/>
                </Delete>
            </DefaultRolloverStrategy>
        </RollingFile>

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

Add configuration to profile:

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

 

4. Use

private static final Logger logger = LoggerFactory.getLogger(UserController.class);

    logger.info("Unknown exception! As a result of:info");
    logger.error("Unknown exception! As a result of:error");
    logger.warn("Unknown exception! As a result of:warn");

Easy to use, over~

In microservices, there are many services, which are distributed in various servers. Generally, we do not use them like this. We usually use ELK to collect and analyze logs. We will talk about it later



Keywords: Java Spring log4j xml encoding

Added by PerfecTiion on Thu, 19 Dec 2019 20:47:41 +0200