Spring Boot中Log4j2无法解析系统属性user.home问题咨询
${user.home} Not Working in Log4j2 YAML Config for Spring Boot Hey there, let's troubleshoot why your ${user.home} system property isn't resolving correctly in your Log4j2 YAML configuration. I've dealt with similar headaches before, so here are the most likely fixes to get your logs generating in the right place:
1. Use Explicit System Property Lookup Syntax
Log4j2 has specific lookup syntax for system properties, and relying on plain ${user.home} can sometimes be ambiguous in YAML files (since YAML has its own variable handling). Instead, explicitly tell Log4j2 to fetch the system property with ${sys:user.home}.
Modify your config like this:
Configuration: name: Default Appenders: RollingFile: - name: RollingFile_Appender fileName: "${sys:user.home}/deepscan.log" filePattern: "${sys:user.home}/deepscan.log.%i.gz" PatternLayout: pattern: "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n" # Add your Policies configuration here (e.g., SizeBasedTriggeringPolicy) Policies: SizeBasedTriggeringPolicy: size: 10MB
Notice I also updated filePattern to use the same path—otherwise your rolled log files might end up in the wrong directory instead of alongside the main log.
2. Verify Log4j2 Dependency Setup
Make sure you've properly excluded Spring Boot's default Logback logging and added the Log4j2 starter. If Logback is still in the classpath, it might interfere with Log4j2's configuration loading.
Maven Setup:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</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>
Gradle Setup:
dependencies { implementation('org.springframework.boot:spring-boot-starter') { exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging' } implementation 'org.springframework.boot:spring-boot-starter-log4j2' }
3. Validate YAML Syntax
YAML is super picky about indentation and quotes. Ensure your configuration has consistent spacing (no mixing tabs and spaces) and that paths with variables are wrapped in double quotes (like we did in the first step). This prevents YAML from misinterpreting the ${...} syntax as its own internal variables.
4. Test with a Manual System Property
If you're still stuck, test by explicitly setting user.home as a JVM argument when starting your Spring Boot app. For example:
java -Duser.home=/your/test/path -jar your-app.jar
If the log file shows up in /your/test/path, that confirms Log4j2 is able to resolve the property—meaning the issue was likely syntax-related in your YAML config.
内容的提问来源于stack exchange,提问作者conquester

