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


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





        <!-- Code simplification -->

        <!-- Journal Log4j2 -->

        <!-- Log4j2 Asynchronous Support -->




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

        <!-- 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}"/>

        <!-- 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"
            <PatternLayout pattern="${LOG_PATTERN}"/>

                <SizeBasedTriggeringPolicy size="20 MB"/>

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

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

        <!-- Store all error information -->
        <RollingRandomAccessFile name="errorFile" fileName="${fileName}/web-error.log" immediateFlush="false"
            <PatternLayout pattern="${LOG_PATTERN}"/>

                <SizeBasedTriggeringPolicy size="50 MB"/>

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

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

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

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


0x03 add Application startup class

public class Application {

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


0x04 add Job class for test

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");


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