▶ Log4j2 performance

  • Logging.apache.org/log4j/2.x/p…

▶ Spring Boot Dependency and Configuration

Maven rely on

<! -- 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> <! <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <! Disruptor </artifactId> <version> disruptor</artifactId> <version>3.3.6</version> </dependency>Copy the code

The XML configuration resources/log4j2. XML

  • Mix the sync/async
  • Color log
  • Classify output to different files
  • Automatically compress and archive log files
<? The XML version = "1.0" encoding = "utf-8"? > <! -- Status after Configuration. This is used to set the internal output of log4j2. You do not need to set this. It can be set to OFF(OFF) or Error(output only Error messages). 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 {-} {abbreviation} % % CLR {[% 15.15 t]} {abbreviation} % CLR {% - 40.40 - c {1}} {cyan} % CLR {that} {abbreviation} % m % n % xwEx < / Property > < / properties > <Appenders> <! <Console name=" Console "target="SYSTEM_OUT"> <! -- The console outputs only messages of level and above (onMatch), --> <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/> <! PatternLayout pattern="${LOG_PATTERN}"/> </Console> <! -- Print out all the information, every time the size exceeds the size, the size of the log will be automatically saved in the folder created by year to month and 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> <! -- Log only INFO and WARN levels --> <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL"/> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <! DefaultRolloverStrategy Max ="50"/> </RollingRandomAccessFile> <! --> <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> <! --> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> </Filters> <! 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>Copy the code

The final effect is as follows:

© copyright belongs to the author, reprint or content cooperation please contact the author

Now, are you currently using Logback or Log4j2 in your project?

● How does the micro-service registry carry tens of millions of visits of large systems?

Here’s how you should play it

● Spring Boot exception processing

● Spring Boot Configuration – Configuration information encryption

● Reject black box applications – Visual monitoring of Spring Boot applications

● There are three sources of concurrency bugs, so keep your eyes open

This article is published by OpenWrite!