【问题标题】:Spring boot logging with log4j - how do I ENABLE spring boot log entries?使用 log4j 进行 Spring Boot 日志记录 - 如何启用 Spring Boot 日志条目?
【发布时间】:2015-02-08 14:12:40
【问题描述】:

我在 pom.xml 中配置了 Spring Boot 以使用 log4j 和这些行:

    <!-- use log4j instead of logback (spring boots own logging) -->
    <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-log4j</artifactId>
    </dependency>

我通过像这样配置我的 log4j.xml 来阻止 spring 在调试级别记录到控制台:

<root>
    <priority value="ERROR" />
    <appender-ref ref="consoleAppender" />
</root>

我的问题是我必须将什么放入 log4j.xml 才能查看来自 Spring Boot 的日志消息?例如,这不起作用(除了 log4j.xml):

<category name="org.springframework.web">
    <level value="trace" />
</category>

使用 log4j 让 spring boot 再次登录的正确路径或正确方法是什么?

【问题讨论】:

  • logback 有什么问题?登录很棒! :) 无论您选择什么,请务必使用 Slf4j!

标签: java spring logging log4j spring-boot


【解决方案1】:

我认为是&lt;logger&gt; 而不是&lt;category&gt;。请注意,Spring Boot 还通过在 application.properties 中配置日志级别,让您不必记住这一点。

【讨论】:

  • 所以 Spring Boot 会自动扫描类路径上的文件 application.properties 并设置日志级别,如果它在该文件中找到指令?即使在 Junit 环境中?
  • 当然,它只有在你使用SpringApplication 时才有效(例如在使用@SpringApplicationConfiguration 的单元测试中)。
【解决方案2】:

如果您想要标准 Spring 输出,您应该将错误级别设置为信息级别。

来自 Log4J 快速入门网站https://logging.apache.org/log4j/1.2/manual.html

这条规则是 log4j 的核心。它假设水平是 订购。对于标准级别,我们有 DEBUG

所以root应该看起来像这样

<root>
    <level value="Info" />
    <appender-ref ref="console" />
</root>

【讨论】:

    猜你喜欢
    • 2020-02-21
    • 2017-12-20
    • 2019-02-09
    • 2014-10-30
    • 2017-10-09
    • 1970-01-01
    • 2023-04-04
    • 2014-06-13
    • 2017-12-26
    相关资源
    最近更新 更多