Spring Boot log processing are you still using Logback?

▶ Log4j2 performance

▶ Spring Boot dependency and configuration

Maven dependence

<!-- web -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

        <!-- Journal Log4j2 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

        <!-- Log4j2 Asynchronous Support -->
        <dependency>
            <groupId>com.lmax</groupId>
            <artifactId>disruptor</artifactId>
            <version>3.3.6</version>
        </dependency>

XML configuration resources/log4j2.xml

  • Mixed sync/async
  • Color log
  • Classification output to different files
  • Automatically compress and archive log files
<?xml version="1.0" encoding="UTF-8"?>
<!-- Configuration Hinder status,This is used to set log4j2 The internal information output of itself can not be set when it is set to trace At that time,
     You will see log4j2 Various internal detailed output. Can be set to OFF(Close) or Error(Output error messages only). 
     30s Refresh this configuration
-->
<configuration status="WARN" monitorInterval="30">

    <!-- Log file directory, compressed file directory, log format configuration -->
    <properties>
        <Property name="fileName">/Users/admin/Code/log</Property>
        <Property name="fileGz">/Users/admin/Code/log/7z</Property>
        <Property name="PID">????</Property>
        <Property name="LOG_PATTERN">%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx</Property>
    </properties>

    <Appenders>
        <!-- Configuration of output console log -->
        <Console name="console" target="SYSTEM_OUT">
            <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)-->
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>
            <!-- Format of output log -->
            <PatternLayout pattern="${LOG_PATTERN}"/>
        </Console>

        <!-- Print out all information, each time the size exceeds size,Then this size Size logs are automatically saved by year-The folder created in the month is compressed as an archive -->
        <RollingRandomAccessFile name="infoFile" fileName="${fileName}/web-info.log" immediateFlush="false"
                                    filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-info.gz">
            <PatternLayout pattern="${LOG_PATTERN}"/>

            <Policies>
                <SizeBasedTriggeringPolicy size="20 MB"/>
            </Policies>

            <Filters>
                <!-- Record only info and warn Level information -->
                <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/>
                <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>

            <!-- Specifies the maximum number of compressed packets per day. The default number is 7, which exceeds the previous -->
            <DefaultRolloverStrategy max="50"/>
        </RollingRandomAccessFile>

        <!-- Store all error information -->
        <RollingRandomAccessFile name="errorFile" fileName="${fileName}/web-error.log" immediateFlush="false"
                                    filePattern="${fileGz}/$${date:yyyy-MM}/%d{yyyy-MM-dd}-%i.web-error.gz">
            <PatternLayout pattern="${LOG_PATTERN}"/>

            <Policies>
                <SizeBasedTriggeringPolicy size="50 MB"/>
            </Policies>

            <Filters>
                <!-- Record only error Level information -->
                <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
            </Filters>

            <!-- Specifies the maximum number of compressed packets per day. The default number is 7, which exceeds the previous -->
            <DefaultRolloverStrategy max="50"/>
        </RollingRandomAccessFile>
    </Appenders>

    <!-- Mixed sync/async -->
    <Loggers>
        <Root level="debug" includeLocation="true">
            <AppenderRef ref="console"/>
            <AppenderRef ref="infoFile"/>
            <AppenderRef ref="errorFile"/>
        </Root>

        <AsyncRoot level="debug" includeLocation="true">
            <AppenderRef ref="console"/>
            <AppenderRef ref="infoFile"/>
            <AppenderRef ref="errorFile"/>
        </AsyncRoot>
    </Loggers>

</configuration>

The final effect is as follows:

Copyright belongs to the author. Please contact the author for reprint or content cooperation

Here's the question. Are you currently using Logback or Log4j2 in your project?

[behind the double 11 Carnival] how does the micro service registration center carry tens of millions of visits to large-scale systems?

Spring Boot new generation monitoring you should play like this

Spring Boot exception handling

Spring Boot configuration - configuration information encryption

Reject black box application - visual monitoring of Spring Boot application

There are three sources of concurrent bugs. Please open your eyes to see them

This article is based on the platform of blog one article multiple sending OpenWrite Release!

Keywords: Java Spring xml Apache log4j

Added by SoundreameR on Wed, 06 Nov 2019 16:26:20 +0200