Java Spring boot 不加载 logback-spring.xml
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/50576521/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring boot does not load logback-spring.xml
提问by Tamerlane
I have a sample Spring Boot application that uses Logback for logging.
So I have logback-spring.xml
next to the jar to configure the logging, however it does not work unless I specify it with logging.config
, ex : logging.config=logback-spring.xml
.
我有一个使用 Logback 进行日志记录的示例 Spring Boot 应用程序。所以我logback-spring.xml
在 jar 旁边配置日志记录,但是除非我用logging.config
, ex :指定它,否则它不起作用logging.config=logback-spring.xml
。
I have looked into Spring Boot ignoring logback-spring.xmlwhere it suggests that it might be because there's already a spring.xml
somewhere, but putting breakpoint on org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(LoggingInitializationContext, LogFile)
shows that logFile is empty.
我已经研究过Spring Boot 忽略 logback-spring.xml,它表明它可能是因为已经有一个spring.xml
地方,但是把断点放在上面org.springframework.boot.logging.AbstractLoggingSystem.initializeWithConventions(LoggingInitializationContext, LogFile)
表明 logFile 是空的。
Am I doing something wrong here ?
我在这里做错了吗?
采纳答案by JohanB
By default, Spring will notlook for resources outside the jar file. If you want to use an external logback configuration file, you must pass it's location when starting the jar:
默认情况下,Spring不会在 jar 文件之外寻找资源。如果要使用外部 logback 配置文件,则必须在启动 jar 时传递它的位置:
$ java -jar -Dlogback.configurationFile=/full_path/logback.xml app.jar
Please, do not include the logback.xml into the final Jar file, it will cause multiple logback.xml files in the classpath.
请不要将 logback.xml 包含到最终的 Jar 文件中,这会导致类路径中出现多个 logback.xml 文件。
回答by Shubham Kadlag
There can be two reasons for such behaviour:
这种行为可能有两个原因:
Reason 1:The logback-spring.xml is somehow not in the classpath. You can verify this at runtime by printing System.getProperty("java.class.path")
and checking if the folder containing logback-spring.xml is present in the output.
原因 1:logback-spring.xml 不知何故不在类路径中。您可以在运行时通过打印System.getProperty("java.class.path")
并检查输出中是否存在包含 logback-spring.xml 的文件夹来验证这一点。
Reason 2: If Reason 1 is not the issue, then there is already a file named logback.xml or logback-spring.xml in the classpath and this may be causing conflict. Now here you have to find that file. You can try renaming logback-spring.xml to logback.xml and check the behaviour.
原因 2:如果原因 1 不是问题,那么类路径中已经有一个名为 logback.xml 或 logback-spring.xml 的文件,这可能会导致冲突。现在你必须在这里找到那个文件。您可以尝试将 logback-spring.xml 重命名为 logback.xml 并检查行为。
回答by this_is_om_vm
Just define these lines in your logback-spring.xml
只需在logback-spring.xml 中定义这些行
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<logger name="org.springframework.web" level="DEBUG"/>
</configuration>
回答by Giang Phan
I don't know why it does not working for you. I have created a logback-spring.xmlfile in the resourcefolder and it worked fine.
我不知道为什么它不适合你。我在资源文件夹中创建了一个logback-spring.xml文件,它运行良好。
Following is content of log file:
以下是日志文件的内容:
<?xml version="1.0" encoding="UTF-8"?>
<configuration scan="true">
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<property name="LOGDIR" value="logs"></property>
<property name="APP_NAME" value="spring-boot-sample"></property>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d ${APP_NAME} %-5level [%thread] %logger: %msg%n</Pattern>
</layout>
</appender>
<appender name="ROLLINGFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOGDIR}/${APP_NAME}-log.%d{MM-dd-yyyy}.log</fileNamePattern>
<maxHistory>90</maxHistory>
</rollingPolicy>
<encoder>
<charset>utf-8</charset>
<Pattern>%d ${APP_NAME} %-5level [%thread] %logger: %msg%n</Pattern>
</encoder>
</appender>
<springProfile name="local">
<root level="debug">
<appender-ref ref="CONSOLE"/>
</root>
<logger name="co.jp.oha" additivity="false" level="debug">
<appender-ref ref="ROLLINGFILE"/>
<appender-ref ref="STDOUT"/>
</logger>
</springProfile>
</configuration>
You can try with them. I hope that it will help you.
你可以和他们一起试试。我希望它会帮助你。
回答by Sangam Belose
As per the description of the problem, you are using the externalized version of your log configuration. The file is kept outside the jar. So you have to mention the path as run-time argument as below:
根据问题的描述,您正在使用日志配置的外部化版本。该文件保存在 jar 之外。因此,您必须将路径作为运行时参数提及,如下所示:
-Dlogging.config=file:logback-spring.xml
Or in mention the same property in application.properties as below:
或者在 application.properties 中提及相同的属性,如下所示:
logging.config=file:logback-spring.xml
The reason it pickup the file from resources folder, because it is configured in spring that way. Spring pick up the logback file by below names from classpath.
它从资源文件夹中提取文件的原因,因为它是在 spring 中配置的。Spring 从类路径中通过以下名称获取 logback 文件。
logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy
Please check the relevant docs at spring-boot custom log configuration
请查看spring-boot 自定义日志配置中的相关文档
回答by alove0286
Dockerfile:
Dockerfile:
COPY /rootProjectName/src/main/resources/logback-spring.xml /deployments/
application-dev.properties:
应用程序-dev.properties:
logging.config=classpath:logback-spring.xml
I'm running a docker container and must copy over the resource folder into my deployments folder in my Docker File... but once copied over
this is the logging.config value that works for me (adding the classpath word)
我正在运行一个 docker 容器,并且必须将资源文件夹复制到我的 Docker 文件中的部署文件夹中......但是一旦复制,
这是对我有用的 logging.config 值(添加类路径词)