Spring Boot忽略来自外部application.properties

时间:2017-02-27 10:12:12

标签: java spring tomcat logging spring-boot

我在Tomcat上运行war文件中的Spring Boot(版本1.5.2.RELEASE)应用程序。在我的上下文文件(conf/Catalina/localhost/ROOT.xml)中,我已经指定了外部属性文件的路径,如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<Context>
    <Environment name="spring.config.location" value="file:/opt/shared/home/.airlines-data/" type="java.lang.String"/>
</Context>

在路径/opt/shared/home/.airlines-data/内,我有一个application.properties文件,其中包含以下行:

logging.level.root=WARN
logging.level.org.hibernate=ERROR
logging.level.org.springframework.web=WARN
spring.jpa.properties.hibernate.show_sql=false
spring.jpa.show-sql=false
logging.file=/opt/shared/home/.airlines-data/Errors.log

正确读取文件。属性logging.file=/opt/shared/home/.airlines-data/Errors.log也正确加载,如果我将其值更改为其他内容,则一切都按预期工作。不幸的是,Spring Boot忽略了所有的日志级别设置。它记录了INFO级别以上的所有内容。 Hibernate还将所有查询记录到日志文件中。这使得日志文件在短时间内变得非常大。有没有其他方法可以将日志记录级别更改为WARN或ERROR并使用外部配置禁用Hibernate SQL日志记录?我正在使用Spring Boot提供的标准日志记录设置。以下是我的依赖项:

dependencies {
    compile('org.springframework.boot:spring-boot-starter-jdbc')
    compile('org.springframework.boot:spring-boot-starter-jooq')
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.liquibase:liquibase-core')
    compile('org.springframework.boot:spring-boot-starter-security')
    compile('org.springframework.boot:spring-boot-starter-thymeleaf')
    compile('org.springframework.boot:spring-boot-starter-web')
    compile('org.springframework.boot:spring-boot-starter-mail')
    compile("org.springframework:spring-messaging")
    compile('org.apache.commons:commons-lang3:3.4')
    compile('commons-io:commons-io:2.4')
    compile('commons-beanutils:commons-beanutils:1.9.2')
    compile('com.samaxes.filter:cachefilter:2.3.1')
    compile('com.monitorjbl:xlsx-streamer:0.2.12')
    compile('org.apache.commons:commons-csv:1.2')
    compile('org.imgscalr:imgscalr-lib:4.2')
    compile('xerces:xercesImpl:2.11.0')
    compile('com.fasterxml.jackson.datatype:jackson-datatype-hibernate4:2.6.4')
    compile('org.thymeleaf.extras:thymeleaf-extras-springsecurity4:2.1.2.RELEASE')
    compile('org.jodd:jodd-mail:3.7.1')
    providedRuntime('org.springframework.boot:spring-boot-starter-tomcat')
    testCompile('org.springframework.boot:spring-boot-starter-test') 
}

我的Error.log文件填充速度非常快,并且包含许多与此类似的行:

2017-02-17 18:45:13.338 DEBUG 5168 --- [localhost-startStop-1] o.s.boot.SpringApplication               : Loading source class pl.eb2b.AirlinesApplication,class org.springframework.boot.context.web.ErrorPageFilter
2017-02-17 18:45:13.851 DEBUG 5168 --- [localhost-startStop-1]
2017-02-17 18:45:13.851 DEBUG 5168 --- [localhost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'file:/opt/shared/home/.airlines-data/application.properties'
2017-02-17 18:45:13.852 DEBUG 5168 --- [localhost-startStop-1] o.s.b.c.c.ConfigFileApplicationListener  : Loaded config file 'classpath:/application.properties'
2017-02-18 00:21:34.997 DEBUG 9026 --- [http-nio-8080-exec-8] o.s.b.c.web.OrderedRequestContextFilter  : Bound request context to thread: org.apache.catalina.connector.RequestFacade@14ab2c1f
2017-02-18 00:21:35.000 DEBUG 9026 --- [http-nio-8080-exec-8] o.s.b.c.web.OrderedRequestContextFilter  : Cleared thread-bound request context: org.apache.catalina.connector.RequestFacade@14ab2c1f
2017-02-20 13:15:09.526 DEBUG 11701 --- [http-nio-8080-exec-4] org.hibernate.SQL                        : select count(*) as y0_ from cars_car_contract this_

我也尝试将logback-spring.xml文件放在我的类路径中:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <root level="WARN">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
    <logger name="org.hibernate" level="ERROR" additivity="false">
        <appender-ref ref="CONSOLE"/>
        <appender-ref ref="FILE"/>
    </logger>
</configuration>

它在我的本地计算机上运行但在服务器上被忽略。

更新2017-04-07:

我在catalina.out日志中找到了一些其他信息,其中包含打印到控制台的所有内容(可能会有帮助):

07-Apr-2017 06:12:48.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_BASE:         /opt/repo/versions/8.5.3
07-Apr-2017 06:12:48.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log CATALINA_HOME:         /opt/repo/versions/8.5.3
07-Apr-2017 06:12:48.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.config.file=/opt/repo/versions/8.5.3/conf/logging.properties
07-Apr-2017 06:12:48.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager
07-Apr-2017 06:12:48.736 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DReceiverIp=
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DMagicPort=
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djvm=jelastic
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.awt.headless=true
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.net.preferIPv4Stack=true
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -DjvmRid=
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xms32M
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xmn30M
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xmx512M
07-Apr-2017 06:12:48.737 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xminf0.1
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Xmaxf0.3
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -XX:+UseG1GC
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -XX:+UseCompressedOops
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -XX:MinHeapFreeRatio=15
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -XX:MaxHeapFreeRatio=30
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djdk.tls.ephemeralDHKeySize=2048
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.base=/opt/repo/versions/8.5.3
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Dcatalina.home=/opt/repo/versions/8.5.3
07-Apr-2017 06:12:48.738 INFO [main] org.apache.catalina.startup.VersionLoggerListener.log Command line argument: -Djava.io.tmpdir=/opt/repo/versions/8.5.3/temp

由于我的服务器是基于Jelastic的云服务器,因此使用这些参数自动配置。不知道它是否有事可做,但param -Djava.util.logging.config.file=/opt/repo/versions/8.5.3/conf/logging.properties看起来与记录有关。以下是此文件的内容:

handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler

1catalina.org.apache.juli.AsyncFileHandler.level = FINE
1catalina.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.AsyncFileHandler.prefix = catalina.

2localhost.org.apache.juli.AsyncFileHandler.level = FINE
2localhost.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.AsyncFileHandler.prefix = localhost.

3manager.org.apache.juli.AsyncFileHandler.level = FINE
3manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.AsyncFileHandler.prefix = manager.

4host-manager.org.apache.juli.AsyncFileHandler.level = FINE
4host-manager.org.apache.juli.AsyncFileHandler.directory = ${catalina.base}/logs
4host-manager.org.apache.juli.AsyncFileHandler.prefix = host-manager.

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = org.apache.juli.OneLineFormatter


org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = WARNING
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = 2localhost.org.apache.juli.AsyncFileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = 3manager.org.apache.juli.AsyncFileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/host-manager].handlers = 4host-manager.org.apache.juli.AsyncFileHandler

1 个答案:

答案 0 :(得分:1)

我们能够找到的一些文章(12)说出以下内容:

  

Spring Boot中的默认日志记录支持

     

Spring Boot参考文档说:

     

默认情况下,如果您使用'Starter POM',将使用Logback   日志记录。还包括适当的Logback路由以确保这一点   使用Java Util Logging,Commons Logging,Log4J的依赖库   或者SLF4J都能正常工作。

因此,您可以在application.properties文件中调整SpringBoot的日志记录级别,最有可能通过以下路径:

cat /opt/tomcat/webapps/ROOT/WEB-INF/classes/application.properties
logging.level.*=OFF
logging.level.org.springframework.web=WARN
logging.level.org.springframework.boot=OFF
logging.level.org.hibernate.SQL=OFF

更改完成后,DEBUG消息应从catalina.out

中消失