问题描述
我有一个简单的弹簧启动应用程序,该应用程序构建到JAR文件.我在src/main/resources/log4j.xml中有一个log4j.xml文件,看起来像这样(log4j docs的基本示例文件):
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <!-- Pattern to output the caller's file name and line number --> <param name="ConversionPattern" value="%5p [%t] (%F:%L) - %m%n"/> </layout> </appender> <appender name="R" class="org.apache.log4j.RollingFileAppender"> <param name="file" value="/tmp/logs/sample.log"/> <param name="MaxFileSize" value="100KB"/> <!-- Keep one backup file --> <param name="MaxBackupIndex" value="1"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p %t %c - %m%n"/> </layout> </appender> <root> <priority value="debug"/> <appender-ref ref="stdout"/> <appender-ref ref="R"/> </root> </log4j:configuration>
记录仅转到控制台(/tmp/logs/sample.log永远不会创建),因为它被忽略了log4j.xml文件.
文件显示在JAR的根部,我认为这是正确的.我还需要做些什么才能获得此记录配置?
如果有任何区别,则该项目正在使用Gradle,而不是Maven.
推荐答案
这取决于您如何设置类路径. log4j是否在类路径上绑定到SLF4J(如果您只使用Vanilla Spring-boot-starter)?这是Spring-Boot-Starter-Log4J和A 样本显示如何使用它(在Maven中,但Gradle具有相同的功能).
如果我是你,我会只使用登录.
n.b. Spring Boot 1.4不支持Log4J(仅LOG4J2).不过,在同一个地方有一个样本.
其他推荐答案
如果我们使用Spring-boot,同时也尝试使用Log4J明确使用日志记录,则内置日志记录中的Spring-boot具有更高的优先级,因此永远不会读取Log4J配置.如上所述,在这种情况下,最好的解决方案是从下面的两个弹簧启动依赖项中排除日志记录.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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-test</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>
问题描述
I have a simple Spring Boot application that builds to a jar file. I have a log4j.xml file in src/main/resources/log4j.xml that looks like this (basic sample file from the log4j docs):
<?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> <layout class="org.apache.log4j.PatternLayout"> <!-- Pattern to output the caller's file name and line number --> <param name="ConversionPattern" value="%5p [%t] (%F:%L) - %m%n"/> </layout> </appender> <appender name="R" class="org.apache.log4j.RollingFileAppender"> <param name="file" value="/tmp/logs/sample.log"/> <param name="MaxFileSize" value="100KB"/> <!-- Keep one backup file --> <param name="MaxBackupIndex" value="1"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="%p %t %c - %m%n"/> </layout> </appender> <root> <priority value="debug"/> <appender-ref ref="stdout"/> <appender-ref ref="R"/> </root> </log4j:configuration>
Logging only goes to the console though (/tmp/logs/sample.log never gets created), as it the log4j.xml file is being ignored.
The file shows up in the root of the jar, which I assume is correct. What else do I need to do to have this logging configuration picked up?
If it makes any difference, the project is using Gradle, not Maven.
推荐答案
It depends how you set up your classpath. Is the log4j binding to slf4j on your classpath (it won't be if you just use the vanilla spring-boot-starter)? The is a spring-boot-starter-log4j and a sample showing how to use it (in Maven, but Gradle has the same features).
If I were you I'd just use logback.
N.B. Spring Boot 1.4 does not support log4j (only log4j2). There's a sample for that in the same place though.
其他推荐答案
If we use Spring-boot and at the same time also trying to use logging explicitly using log4j, the spring-boot in built logging takes higher precedence and hence log4j configuration is never read. As stated above, the best solution in this case is to exclude logging from the below two spring-boot dependencies.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</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-test</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>