当前位置: 首页 > 知识库问答 >
问题:

运行Spring-Boot时忽略-dlogback.configurationfile=logback.xml

强烨
2023-03-14

我们有一个Spring-Boot1.0.rc1应用程序,为日志配置了logback,并在src/test/resources中有一个logback.xml文件。当我们从maven运行spring-boot时,日志文件似乎会被看到和尊重。(这是我们在spring-boot徽标之后所需的日志格式)

D:\Users\myName\My Projects\Work\my-co\my-app\src\my-app\my-app-camel>mvn  clean spring-boot:run -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener -Dlogback.configurationFile=src/test/resources/logback.xml

<snip/>

[INFO] --- spring-boot-maven-plugin:0.5.0.M7:run (default-cli) @ my-app-camel ---
06:41:56,011 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [src/test/resources/logback.xml] at [file:/D:/Users/myName/My%20Projects/Work/my-co/my-app/src/my-app/my-app-camel/src/test/resources/logback.xml]
06:41:56,150 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
06:41:56,170 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 5 seconds
06:41:56,171 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[D:\Users\myName\My Projects\Work\my-co\my-app\src\my-app\my-app-camel\src\test\resources\logback.xml]] every 5 seconds.
06:41:56,172 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
06:41:56,223 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
06:41:56,233 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
06:41:56,276 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
06:41:56,389 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [me.moocar.logbackgelf.GelfAppender]
06:41:56,398 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [GELF]
06:41:56,567 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.my-co.my-app.] to DEBUG
06:41:56,568 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.my-co.my-app.] to false
06:41:56,569 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[com.my-co.my-app.]
06:41:56,571 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.] to DEBUG
06:41:56,572 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.] to false
06:41:56,572 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[org.]
06:41:56,573 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to INFO
06:41:56,574 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
06:41:56,574 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
06:41:56,578 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@4ffdcca5 - Registering current configuration as safe fallback point

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

06:41:57.436 [com.my-co.my-app.Application.main()] INFO  com.my-co.my-app.Application - Starting Application on MY-LAPTOP-HOSTNAME with PID 7812 (started by myName)
06:41:57.952 [com.my-co.my-app.Application.main()] INFO  o.s.b.f.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [META-INF/spring/camel-server.xml]

<etc./>

但是,当我们删除一个溜进src/java/resources的流氓logback.xml文件,并在其他所有内容未变的情况下重新运行时(确保我们也删除目标中的文件):

D:\Users\myName\My Projects\Work\my-co\my-app\src\my-app\my-app-camel>mvn  clean spring-boot:run -Dlogback.statusListenerClass=ch.qos.logback.core.status.OnConsoleStatusListener -Dlogback.configurationFile=src/test/resources/logback.xml

<snip/>

[INFO]
[INFO] <<< spring-boot-maven-plugin:1.0.0.RC1:run (default-cli) @ my-app-camel <<<
[INFO]
[INFO] --- spring-boot-maven-plugin:1.0.0.RC1:run (default-cli) @ my-app-camel ---
06:53:59,598 |-INFO in ch.qos.logback.classic.LoggerContext[default] - Found resource [src/test/resources/logback.xml] at [file:/D:/Users/myName/My%20Projects/Work/my-co/my-app/src/my-app/my-app-camel/src/test/resources/logback.xml]
06:53:59,736 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - debug attribute not set
06:53:59,755 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Setting ReconfigureOnChangeFilter scanning period to 5 seconds
06:53:59,756 |-INFO in ReconfigureOnChangeFilter{invocationCounter=0} - Will scan for changes in [[D:\Users\myName\My Projects\Work\my-co\my-app\src\my-app\my-app-camel\src\test\resources\logback.xml]] every 5 seconds.
06:53:59,757 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - Adding ReconfigureOnChangeFilter as a turbo filter
06:53:59,806 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [ch.qos.logback.core.ConsoleAppender]
06:53:59,817 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [STDOUT]
06:53:59,858 |-INFO in ch.qos.logback.core.joran.action.NestedComplexPropertyIA - Assuming default type [ch.qos.logback.classic.encoder.PatternLayoutEncoder] for [encoder] property
06:53:59,971 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - About to instantiate appender of type [me.moocar.logbackgelf.GelfAppender]
06:53:59,980 |-INFO in ch.qos.logback.core.joran.action.AppenderAction - Naming appender as [GELF]
06:54:00,144 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [com.my-co.my-app.] to DEBUG
06:54:00,144 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [com.my-co.my-app.] to false
06:54:00,145 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[com.my-co.my-app.]
06:54:00,148 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting level of logger [org.] to DEBUG
06:54:00,148 |-INFO in ch.qos.logback.classic.joran.action.LoggerAction - Setting additivity of logger [org.] to false
06:54:00,149 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[org.]
06:54:00,150 |-INFO in ch.qos.logback.classic.joran.action.RootLoggerAction - Setting level of ROOT logger to DEBUG
06:54:00,150 |-INFO in ch.qos.logback.core.joran.action.AppenderRefAction - Attaching appender named [STDOUT] to Logger[ROOT]
06:54:00,151 |-INFO in ch.qos.logback.classic.joran.action.ConfigurationAction - End of configuration.
06:54:00,154 |-INFO in ch.qos.logback.classic.joran.JoranConfigurator@54c1f582 - Registering current configuration as safe fallback point

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

2014-02-12 06:54:01.023  INFO 5240 --- [lication.main()] com.my-co.my-app.Application          : Starting Application on MY-LAPTOP-HOSTNAME with PID 5240 (started by myName)
2014-02-12 06:54:01.564  INFO 5240 --- [lication.main()] o.s.b.f.xml.XmlBeanDefinitionReader      : Loading XML bean definitions from class path resource [META-INF/spring/camel-server.xml]

<etc./>

您将注意到spring-boot启动后的日志格式与第一次输出不同。但是,logback(在Spring-Boot启动之前)报告它在src/test/resources中看到了logback.xml,就像它第一次看到的那样。

为了感兴趣,我们的logback.xml(在src/java/resources和src/test/resources中都有)如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true" scanPeriod="5 seconds">

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{40} - %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="GELF" class="me.moocar.logbackgelf.GelfAppender">
        <facility>logback-gelf-test</facility>
        <graylog2ServerHost>XXX.XXX.XXX.XXX</graylog2ServerHost>
        <graylog2ServerPort>NNNNN</graylog2ServerPort>
        <useLoggerName>true</useLoggerName>
        <useThreadName>true</useThreadName>
        <useMarker>true</useMarker>
        <graylog2ServerVersion>0.9.6</graylog2ServerVersion>
        <chunkThreshold>1000</chunkThreshold>
        <messagePattern>%m%rEx</messagePattern>
        <shortMessagePattern>%.-100(%m%rEx)</shortMessagePattern>
        <additionalField>ipAddress:_ip_address</additionalField>
        <additionalField>requestId:_request_id</additionalField>
        <staticAdditionalField>_node_name:www013</staticAdditionalField>
        <includeFullMDC>true</includeFullMDC>
    </appender>

    <logger name="com.my-co.my-app." level="DEBUG" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>

    <logger name="org." level="DEBUG" additivity="false">
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <!--<appender-ref ref="GELF" />-->
    </root>

</configuration>

总之,当logback.xml与spring-boot应用绑定时(尽管我可能错了),它会被看到并使用(即在src/java/resources中),但当我们不绑定时,尽管logback尽了最大努力,它却被忽略了。

至于解决这个问题,我们被难住了。我们猜测,在spring-boot jar中的某个地方,有某种东西以某种方式占据优先地位,或者关闭logback,或者介入并告诉logback忽略它刚才被告知的内容并使用另一个默认值。

或者我们是哑巴,但看不见。感谢所有的帮助。

共有1个答案

茹正祥
2023-03-14

默认策略假设,如果类路径中没有logback.xml(或其他标准文件名之一),那么您必须对默认值满意(有关详细信息,请参阅logbackloggingsystem)。Spring Boot尝试统一公共日志系统的外部配置开关,因此它在logging.config中查看(它不知道logback.configurationfile)。您可以改用它(即logging.config=file:./src/test/resources/logback.xml),或者确保您的配置文件位于类路径上。

 类似资料:
  • 我有2个Spring Boot(1.4.1-发行版)控制台应用程序使用了Logback。这两个配置文件或多或少是相同的,都位于my/src/main/resources文件夹中,名为logback-spring.xml。 这两个项目都在其pom.xml中包含了maven依赖项spring-boot-starter-logging,并获取1.1.7版本的日志。 两个POM中定义的Spring Boo

  • 问题内容: 我必须使用大量未明确指定serialVersionUID的已编译Java类。由于它们的UID是由编译器任意生成的,因此即使实际的类定义匹配,许多需要序列化和反序列化的类最终也会导致异常。(当然,这是所有预期的行为。) 对我来说,返回并修复所有第3方代码是不切实际的。 因此,我的问题是:是否有任何方法可以使Java运行时 忽略 serialVersionUID中的差异,并且仅在结构上存在

  • 我在Spring Boot(1.5.7.release)中添加了带有JWT身份验证的Spring Security性(5.0.0.release),但是CORS似乎不起作用。我添加了这里描述的CORS配置。我还尝试将@CrossOrigin添加到控制器中,但似乎没有改变任何东西。 和Spring Security调试日志: 下面是无效CORS请求的日志:

  • 为什么它们没有被应用?

  • 我已经编写了许多通过RESTAPI调用进行通信的服务。这些服务可以配置为使用HTTP或HTTPS。任何给定的客户端都具有定义到服务器的连接的安全配置。“默认”配置属性由应用程序中的值设置。yml在这一点上效果很好。 然而,我逐渐意识到,这在更现实的情况下并不适用。问题是,我试图设置特定的参数,例如启动客户端时的服务器主机/端口,而我设置的值被忽略。 例如: 服务A(客户端)将出于某种目的与服务B(

  • 我正在迁移一个活动的Spring Web应用到Spring Boot(1.4.2)。 这些bean是在XML中定义的,因为它正在加载@重要资源。 我正在启动的4个bean是同一个对象BasicDataSource的实例。 为了告诉Spring要加载哪个,我为每个都设置了一个ID,并使用@Qualifer将正确的bean绑定到变量。 但Spring似乎忽略了我的@Qualifier,并抛出“没有可用