【问题标题】:Spring Boot's baner and log4j apears in consoleSpring Boot 横幅和 log4j 出现在控制台中
【发布时间】:2016-12-02 11:25:37
【问题描述】:

我有 Spring Boot 应用程序,它在内部使用带有 log4j 日志记录的库。

我在 pom.xml 中添加了(将 log4j 条目附加到我的 logback 日志中):

    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>log4j-over-slf4j</artifactId>
    </dependency>

根据http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html#howto-configure-logback-for-logging-fileonly,我添加了自定义 logback-spring.xml,它应该禁用控制台日志输出:

<configuration>
    <include resource="org/springframework/boot/logging/logback/defaults.xml" />
    <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}/}spring.log}"/>
    <include resource="org/springframework/boot/logging/logback/file-appender.xml" />
    <root level="INFO">
        <appender-ref ref="FILE" />
    </root>
</configuration>

问题出在:

java -jar my-app.jar &

baner.txt 和以下警告仍会写入控制台:

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.4.2.RELEASE)

log4j:WARN No appenders could be found for logger (package.hidden).
log4j:WARN Please initialize the log4j system properly.

Q1:如何完全禁用控制台日志? Q2:如何将 log4j 日志附加到 logback 文件输出?

【问题讨论】:

  • 好的,我从我的库中排除了 log4j,并且没有显示警告。但是baner仍然可见。

标签: spring-boot log4j logback executable-jar


【解决方案1】:

根据评论,只剩下 1 个问题。如果您需要禁用横幅,有多种方法。你可以通过application.properties 文件来做到这一点:

spring.main.banner-mode=off

或者通过application.yml作为:

spring:
    main:
        banner-mode: "off"

甚至在您的来源中:

public static void main(String... args) {
    SpringApplication application = new SpringApplication(SomeSpringConfiguration.class);
    application.setBannerMode(Banner.Mode.OFF);
    application.run(args);
}

【讨论】:

    【解决方案2】:

    log4j WARN 的解决方案是从我的内部库中排除传递依赖。

    Banner默认写入控制台,所以我们要设置:

    spring.main.banner-mode=log
    

    https://github.com/spring-projects/spring-boot/issues/4001

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-01-31
      • 2015-08-02
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多