log4j2.properties配置异步adminLogger失效问题排查求助
It looks like your current configuration only sets up a rolling file appender, but doesn’t wrap it in an AsyncAppender—that’s why the async mode isn’t kicking in for your adminLogger. Here’s how to fix this step by step:
1. Add the AsyncAppender Wrapper
You need an AsyncAppender layer to handle asynchronous logging, which references your existing AdminAppender. Add this to your log4j2.properties:
# Async appender wrapping AdminAppender appender.adminAsync.type = AsyncAppender appender.adminAsync.name = AdminAsyncAppender appender.adminAsync.appenderRefs = adminRef appender.adminAsync.appenderRef.adminRef.ref = AdminAppender # Optional: Tune async behavior (adjust values as needed) appender.adminAsync.blocking = false appender.adminAsync.queueSize = 1024
2. Point adminLogger to the AsyncAppender
Update your logger configuration to use the async appender instead of the direct rolling file appender. If you haven’t defined the adminLogger yet, add this:
logger.adminLogger.name = com.yourpackage.admin logger.adminLogger.level = INFO logger.adminLogger.additivity = false logger.adminLogger.appenderRefs = adminAsyncRef logger.adminLogger.appenderRef.adminAsyncRef.ref = AdminAsyncAppender
Replace com.yourpackage.admin with the actual package/class name you want to log with this logger.
3. Verify Required Dependencies
Log4j2 uses the LMAX Disruptor library for high-performance async logging by default. Make sure you have this dependency in your project:
For Maven:
<dependency> <groupId>com.lmax</groupId> <artifactId>disruptor</artifactId> <version>3.4.4</version> <!-- Use the latest compatible version --> </dependency>
For Gradle:
implementation 'com.lmax:disruptor:3.4.4'
Full Working Example
Putting it all together, your log4j2.properties should look like this:
# Admin Rolling File Appender appender.adminAppender.type = RollingFile appender.adminAppender.name = AdminAppender appender.adminAppender.fileName = ${adminLogFile} appender.adminAppender.filePattern = ${logPath}/admin-Backup-%i.log appender.adminAppender.layout.type = PatternLayout appender.adminAppender.layout.pattern = %d{yyyy.MM.dd HH.mm.ss.SSS} %-5p %c{1} [%t]: %m%n # Async Appender for Admin appender.adminAsync.type = AsyncAppender appender.adminAsync.name = AdminAsyncAppender appender.adminAsync.appenderRefs = adminRef appender.adminAsync.appenderRef.adminRef.ref = AdminAppender appender.adminAsync.blocking = false appender.adminAsync.queueSize = 1024 # Admin Logger Configuration logger.adminLogger.name = com.yourpackage.admin logger.adminLogger.level = INFO logger.adminLogger.additivity = false logger.adminLogger.appenderRefs = adminAsyncRef logger.adminLogger.appenderRef.adminAsyncRef.ref = AdminAsyncAppender # Root Logger (adjust as needed for your application) rootLogger.level = INFO rootLogger.appenderRefs = stdout rootLogger.appenderRef.stdout.ref = ConsoleAppender
Common Pitfalls to Check
- Missing appenderRef in AsyncAppender: If you don’t link the async appender to your rolling file appender, no logs will be written.
- Disruptor dependency missing: Without this, Log4j2 may fall back to synchronous logging or throw runtime errors.
- Incorrect logger name: Ensure the logger name matches the package/class you’re trying to log from.
After making these changes, restart your application. You can verify async mode by checking thread names in the logs—async logs should show a thread like AsyncAppender-AdminAsyncAppender instead of your application’s main threads.
内容的提问来源于stack exchange,提问作者Mallikarjun gunnam

