Spring Boot的baner和log4j在控制台中出现

时间:2016-12-02 11:25:38

标签: spring-boot log4j logback executable-jar

我有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文件输出?

2 个答案:

答案 0 :(得分:1)

根据评论,只留下1个问题。如果您需要禁用横幅广告,可以采用多种方式。您可以通过application.properties文件执行此操作,如:

spring.main.banner-mode=off

或通过application.yml as:

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);
}

答案 1 :(得分:0)

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

默认情况下,Banner会写入控制台,因此我们必须设置:

spring.main.banner-mode=log

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