Spring Boots baner 和 log4j 出现在控制台中

Spring Boot's baner and log4j apears in console

我有 Spring 引导应用程序,它在内部使用带有 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 WARN 的解决方案是从我的内部库中排除传递依赖项。

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

spring.main.banner-mode=log

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

根据评论,仅剩 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);
}