Configuring Log4j2 for Application Logging

  1. Getting Started

Adding Dependencies

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

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

<!-- Asynchronous logging -->
<dependency>
  <groupId>com.lmax</groupId>
  <artifactId>disruptor</artifactId>
  <version>3.4.2</version>
</dependency>

Place log4j2.xml in the resources directory

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<appenders>
		<!-- Console output format -->
		<console name="Console" target="SYSTEM_OUT">
			<PatternLayout
					pattern="%d{HH:mm:ss} %-5level %class %L %M -- %msg%n" />
		</console>
		<!-- File output configuration -->
		<RollingFile name="DebugAppender"
					 fileName="D:/logs/debug.log"
					 filePattern="D:/logs/$${date:yyyy-MM-dd}/debug-%d{yyyy-MM-dd}-%i.log">
			<Filters>
				<ThresholdFilter level="DEBUG"/>
				<ThresholdFilter level="INFO" onMatch="DENY" onMismatch="NEUTRAL" />
			</Filters>
			<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %class{36} %L %M - %msg%n" />
			<Policies>
				<SizeBasedTriggeringPolicy size="100 MB" />
			</Policies>
			<DefaultRolloverStrategy max="20" />
		</RollingFile>

		<RollingFile name="InfoAppender"
					 fileName="D:/logs/info.log"
					 filePattern="D:/logs/$${date:yyyy-MM-dd}/info-%d{yyyy-MM-dd}-%i.log">
			<Filters>
				<ThresholdFilter level="INFO" />
				<ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL" />
			</Filters>
			<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n" />
			<Policies>
				<SizeBasedTriggeringPolicy size="100 MB" />
			</Policies>
			<DefaultRolloverStrategy max="20" />
		</RollingFile>

		<RollingFile name="WarnAppender"
					 fileName="D:/logs/warn.log"
					 filePattern="D:/logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
			<Filters>
				<ThresholdFilter level="WARN" />
				<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL" />
			</Filters>
			<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<SizeBasedTriggeringPolicy size="100 MB" />
			</Policies>
			<DefaultRolloverStrategy max="20" min="0" />
		</RollingFile>

		<RollingFile name="ErrorAppender"
					 fileName="D:/logs/error.log"
					 filePattern="D:/logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
			<Filters>
				<ThresholdFilter level="ERROR" />
				<ThresholdFilter level="FATAL" onMatch="DENY" onMismatch="NEUTRAL" />
			</Filters>
			<PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n" />
			<Policies>
				<SizeBasedTriggeringPolicy size="100 MB" />
			</Policies>
			<DefaultRolloverStrategy max="20" min="0" />
		</RollingFile>
	</appenders>

	<loggers>
		<root level="INFO">
			<appender-ref ref="Console"/>
			<appender-ref ref="DebugAppender"/>
			<appender-ref ref="InfoAppender"/>
			<appender-ref ref="WarnAppender"/>
			<appender-ref ref="ErrorAppender"/>
		</root>
	</loggers>
</configuration>

Log4j2 is typically used with synchronous logging during development, and switched to asynchronous logging before deployment. This is because restarting the server frequently can cause data loss in asynchronous threads.

  1. Pattern Layout Symbols

Format symbols for PatternLayout:

  • %p or %level: Log priority (DEBUG, INFO, WARN, ERROR, FATAL).
  • %d: Date/time of the log event, default format is ISO8601, can be customized.
  • %r: Milliseconds elapsed since application start.
  • %t: Name of the thread that generated the log event.
  • %class: Full class name where the log was generated.
  • %M: Method name where the log was generated.
  • %F: File name where the log was generated.
  • %L: Line number where the log was generaetd.
  • %m or %msg or %message: The actual log message.
  • %n: Platform-specific newline character.
  • %x: Nested Diagnostic Context (NDC) associated with the current thread.
  • %%: Output a literal '%'.
  • Additional formatting options can be applied using modifiers like %-20 or %.30.
  1. Sending Email on Exceptions
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

spring: mail: prtoocol: smtp host: smtp.163.com port: 465 username: 19937782588@163.compassword: SYRZLRNOCSLFQJKA properties: mail: smtp: auth: true ssl: enable: true



	
		
            
            
                %d %p [%C] [%t] [%l] %m%n
            
        
	

	
		
			
			
			
			
			
		
		
			
		
	


Tags: Log4j2 logging Spring Boot email notifications asynchronous logging

Posted on Mon, 12 Oct 2026 16:44:38 +0000 by lachild