Spring Boot integrates Log4j2 logs and pressure tests performance

Posted by etrooper on Wed, 15 Apr 2020 20:14:49 +0200

Performance test of 1/ Log4j2

It is not difficult to see from the figure that the performance is the best when the number of online processes is between 2 and 16, and synchronous and asynchronous logger s are used to print logs.

2/ target

  • Mixed sync/async
  • Color log
  • Classification output to different files
  • Automatically compress and archive log files

3/ implementation

0x01 Maven depends on pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
	<modelVersion>4.0.0</modelVersion>

	<groupId>org.spring</groupId>
	<artifactId>springboot</artifactId>
	<version>0.0.1-SNAPSHOT</version>
	<packaging>jar</packaging>

	<name>springboot</name>
	<description>Demo Log4j2 for Spring Boot</description>

	<parent>
		<groupId>org.springframework.boot</groupId>
		<artifactId>spring-boot-starter-parent</artifactId>
		<version>1.5.4.RELEASE</version>
	</parent>

	<properties>
		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
		<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
		<java.version>1.8</java.version>
	</properties>

	<dependencies>

		<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>

        <!-- Code simplification -->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <version>1.16.16</version>
        </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>

	</dependencies>

	<build>
		<plugins>
			<plugin>
				<groupId>org.springframework.boot</groupId>
				<artifactId>spring-boot-maven-plugin</artifactId>
			</plugin>
		</plugins>
	</build>

</project>

0x02 configure Log4j2, add log4j2.xml in the resources file directory, and it will be automatically configured

<?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>

0x03 add Application startup class

@SpringBootApplication
@EnableScheduling
public class Application {

	public static void main(String[] args) {
		SpringApplication.run(Application.class, args);
	}

}

0x04 add Job class for test

@Component
@Log4j2
public class LogJob {

    /**
     * 2 Once per second
     */
    @Scheduled(fixedRate = 2 * 1000)
    public void logging(){
        Date now = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        log.info(simpleDateFormat.format(now));
        log.debug("-------DEBUG---------");
        log.error(now.getTime());
    }

}

0x05 approximate file directory structure

4 / reference documents

Welfare at the end of article

Java materials link: https://pan.baidu.com/s/1pUCCPstPnlGDCljtBVUsXQ Password: b2xc
More information: in 2020, select Alibaba Java, architecture, microservices, etc., plus v Mei qwerdd111

Reprint, please keep the original address, thank you~

Topics: Java Spring Maven xml