-Dlogback.configurationFile=logback.xml 在运行 Spring-Boot 时被忽略
Posted
技术标签:
【中文标题】-Dlogback.configurationFile=logback.xml 在运行 Spring-Boot 时被忽略【英文标题】:-Dlogback.configurationFile=logback.xml ignored when running Spring-Boot 【发布时间】:2014-03-10 12:09:20 【问题描述】:我们有一个 spring-boot 1.0.RC1 应用程序,配置了 logback 用于日志记录,并在 src/test/resources 中有一个 logback.xml 文件。当我们从 maven 运行 spring-boot 时,似乎可以看到并尊重 logback 文件。 (这是我们在 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 ReconfigureOnChangeFilterinvocationCounter=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 ReconfigureOnChangeFilterinvocationCounter=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>%dHH:mm:ss.SSS [%thread] %-5level %logger40 - %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 中),但是当我们不这样做时t 捆绑,然后尽管 logback 尽了最大努力,但它被忽略了。
至于解决这个问题,我们很难过。我们猜测在我们的 spring-boot jar 中的某个地方以某种方式优先,或者关闭 logback,或者介入并告诉 logback 忽略刚刚被告知的内容并使用另一个默认值。
或者我们是愚蠢的,但看不到它。感谢所有帮助。
【问题讨论】:
你能发布你的 Spring Boot 依赖项吗?你添加了执行器吗? 【参考方案1】:默认策略假定如果您在类路径中没有logback.xml
(或其他标准文件名之一),那么您必须对默认值感到满意(有关详细信息,请参阅LogbackLoggingSystem
)。 Spring Boot 试图统一常用日志系统的外部配置开关,所以它在logging.config
中查找(它不知道logback.configurationFile
)。您可以改用它(即logging.config=file:./src/test/resources/logback.xml
),或者确保您的配置文件位于类路径中。
【讨论】:
@Dave :我刚从 Google 来到这里:系统属性logback.configurationFile
对我有用,但不适用于 logging.config
(logback v1.1.2)
current docs: ...您可以使用“logging.config”属性设置配置文件的位置
java -Dlogging.config=path_to_logback.xml [...]
为我工作
@TommyBlackbird 谢谢!您的方法也对我有用,应该是标记为已接受答案的***答案【参考方案2】:
我知道这篇文章有点陈旧,但根据 Dave Syer 的回答,将以下内容添加到 application.properties
是一种快速解决方法:
logging.config=$logback.configurationFile
【讨论】:
【参考方案3】:根据Spring Boot documentation,你可以这样运行:
java -jar app.jar --logging.config=./src/test/resources/logback.xml
另外,Spring Boot 尝试统一不同日志启动的属性名称。结果JVM参数Dlogback.configurationFileis not used for Spring Boot。
【讨论】:
【参考方案4】:如果 logback xml 在资源文件夹下,您可以在 VM 选项中添加以下内容:
java -jar App.jar -Dlogging.config=classpath:logback-local.xml
【讨论】:
【参考方案5】:在 application.properties 文件中添加 logback 文件路径。示例测试文件夹包含 app.jar 和 config 文件夹,如下所示 测试 |- app.jar |- 配置 |--- 应用程序.properties |--- logback.xml 然后添加 logging.config:config\\logback.xml。那就试试吧
【讨论】:
【参考方案6】:假设您在 config 目录中有 spring logback 配置,下面的命令将通过覆盖应用程序内的现有文件(如果有)来添加外部文件:
java -jar -Dlogging.config=./config/logback-spring.xml app.jar --spring.config.location=./config/application.properties
【讨论】:
这行得通。你也可以使用 classpath:/path_to_config.xml 表示法。【参考方案7】:不太对。 logback.configurationFile
不由 Spring 管理(即 env var 替换左右),但 logback 仍会选择它。至少这是我对文档的理解和我的实践经验。
【讨论】:
以上是关于-Dlogback.configurationFile=logback.xml 在运行 Spring-Boot 时被忽略的主要内容,如果未能解决你的问题,请参考以下文章