部署为.war时的Spring-Boot Logging配置 [英] Spring-Boot Logging configuration when deployed as .war

查看:196
本文介绍了部署为.war时的Spring-Boot Logging配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的spring-boot应用程序,打包为.war文件,可以部署到外部Tomcat容器.我在类路径(/WEB-INF/classes)中有一个logback.xml(如下),但是,当部署到Tomcat时,日志未写入文件.

I have a simple spring-boot application being packaged as .war file capable of being deployed to an external Tomcat container. I have a logback.xml (below) in the classpath (/WEB-INF/classes), however, when deployed to Tomcat the log is not written to the file.

<?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>

推荐答案

我一直在努力解决同样的问题-单元测试的日志记录工作正常,并运行应用程序"main()"方法,但是在tomcat中它会完全忽略我的配置.

I've been struggling with what might be the same issue - logging works fine from unit tests and running the application 'main()' method, but in tomcat it ignores my configuration completely.

事实证明,在启动时,tomcat设置了一个环境变量'LOGGING_CONFIG'来描述其'logging.properties'文件的位置.可以在tomcat/bin/catalina.sh或(.bat)中看到.

It turns out that on startup, tomcat sets an environment variable 'LOGGING_CONFIG' to describe the location of its 'logging.properties' file. This can be seen in tomcat/bin/catalina.sh or (.bat).

Spring Boot还会在启动时读取环境,并且作为其智能配置处理的一部分,我相信它会将LOGGING_CONFIG转换为'logging.config',这是它用来允许您覆盖其位置的属性.日志记录配置.

Spring Boot also reads the environment on startup, and as part of its smart handling of configuration, I believe it converts LOGGING_CONFIG to 'logging.config', which is a property it uses to allow you to override the location of its' logging configuration.

问题在于,它随后尝试将该值视为文件名并加载(它实际上不是文件名,而是描述文件的Java系统属性),但是失败,并且不初始化其日志记录正确,并继续执行默认的任何Tomcat设置.

The issue is that it then tries to treat that value as a filename and load it (it isn't actually a filename but a Java system property that describes a file), but it fails, and doesn't initialise its logging properly and continues with whatever Tomcat setup by default.

解决方案似乎是要确保首先设置环境变量,例如(您的IDE应该为此提供一个选项-在Intellij中转到运行"->编辑配置"->环境变量"->输入名称)和以下值):

The solution appears to be to ensure that the environment variable is set first, for example something like (your IDE should have an option for this - in Intellij go to Run -> Edit Configurations -> Environment Variables -> enter the name and value from the following):

set logging.config=classpath:/logback.xml

Spring Boot将优先于Tomcat的LOGGING_CONFIG使用它,并将按预期工作.

Spring Boot will use this in preference to LOGGING_CONFIG from Tomcat and will work as expected.

但是,到目前为止,我仍无法从诸如application.properties之类的配置文件中使此工作正常进行,这意味着这是部署到该Tomcat实例的所有应用程序的全局设置.

So far however I have been unable to get this working from within a configuration file like application.properties, which means that this is a global setting for all applications deployed to that Tomcat instance.

这是由于以下事实:环境变量仍然会覆盖本地配置,在任何其他情况下,但这都是您想要的:-(

this would be due to the fact that environment variables override local configuration anyway, which in every other case but this would be what you wanted :-(

我可以确认从Spring Boot 1.1.6开始,此问题已得到解决-作为警告记录下来,它将继续使用现有的(和正确的)记录配置.遗憾的是,您无法在logback.xml文件中禁用警告消息本身.

EDIT 2: I can confirm that as of Spring Boot 1.1.6, this issue has been worked around - it is logged as a warning and it will continue to use the existing (and correct) logging configuration. Sadly you can't disable the warning message itself inside your logback.xml file.

这篇关于部署为.war时的Spring-Boot Logging配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆