首次配置Log4J遇警告:无可用Appender,需配置控制台+文件日志输出
Fixing the "No appenders could be found for logger" Log4J Warning
Let’s break down what’s going wrong and fix your configuration step by step—this is a super common gotcha when setting up Log4J for the first time!
The Core Issue
Your com.me.kafka logger is configured with DEBUG level and additivity=false, but you haven’t assigned any appenders to it. Since additivity=false disables inheriting appenders from the root logger, this logger has nowhere to send its logs—hence the warning.
Corrected Configuration
Here’s the adjusted config that will route logs from both the root logger and your com.me.kafka package to the console and rolling file:
# Root logger: only FATAL and above go to appenders log4j.rootCategory=FATAL, util_kafka_console, util_kafka_file # com.me.kafka logger: DEBUG and above, uses both appenders, no inheritance from root log4j.logger.com.me.kafka=DEBUG, util_kafka_console, util_kafka_file log4j.additivity.com.me.kafka=false # System properties (ensure these are passed via JVM args, or hardcode the path below) # filename=please_specify_log_location.log # my_kafka.logs.dir=log # Console Appender log4j.appender.util_kafka_console=org.apache.log4j.ConsoleAppender log4j.appender.util_kafka_console.layout=org.apache.log4j.PatternLayout log4j.appender.util_kafka_console.layout.ConversionPattern=[%p] %d{yyyy-MM-dd HH:mm:ss} %x %c [%M] (%L) - %m%n # Rolling File Appender log4j.appender.util_kafka_file=org.apache.log4j.RollingFileAppender log4j.appender.util_kafka_file.layout=org.apache.log4j.PatternLayout log4j.appender.util_kafka_file.layout.ConversionPattern=[%p] %d{yyyy-MM-dd HH:mm:ss} %x %c [%M] (%L) - %m%n # Option 1: Use system properties (ensure JVM args like -Dfilename=kafka.log -Dmy_kafka.logs.dir=./logs are set) log4j.appender.util_kafka_file.File=${my_kafka.logs.dir}/${filename} # Option 2: Hardcode the path if you don't need dynamic variables # log4j.appender.util_kafka_file.File=./logs/kafka_debug.log log4j.appender.util_kafka_file.MaxFileSize=10MB log4j.appender.util_kafka_file.MaxBackupIndex=2
Key Fixes Explained
- Added appenders to
com.me.kafka: By addingutil_kafka_console, util_kafka_fileto the logger definition, you’re telling Log4J exactly where to send logs from this package. - Verified variable resolution: If you’re using the
${my_kafka.logs.dir}and${filename}variables, make sure you pass them as JVM arguments when starting your application, e.g.:
If you don’t need dynamic paths, hardcoding the file path avoids any variable parsing issues.java -Dmy_kafka.logs.dir=./logs -Dfilename=kafka_app.log -jar your-app.jar - Cleaned up whitespace: Removed the extra tab in the conversion pattern (optional, but makes the config cleaner).
Additional Checks
- Ensure Log4J dependencies are correct: Make sure you have the right Log4J 1.x jars in your classpath (since you’re using
org.apache.log4j.*classes). - Confirm config file location: Log4J looks for
log4j.properties(orlog4j.xml) in the root of your classpath by default. If you’re placing it elsewhere, specify the path with the JVM argument-Dlog4j.configuration=file:/path/to/your/log4j.properties.
内容的提问来源于stack exchange,提问作者Havnar




