Spring Framework application.properties与logback.xml [英] Spring Framework application.properties vs logback.xml

查看:229
本文介绍了Spring Framework application.properties与logback.xml的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将Spring与Spring Boot一起使用.最近,在尝试使用EhCache时,我尝试为EhCache启用日志记录.使用以下命令在application.properties中设置日志级别:

I am using Spring with Spring boot. Recently while trying my hands on EhCache, I tried enabling logging for EhCache. Setting log-level in application.properties with :

logging.level.org.springframework.cache: DEBUG

没有效果.因此,我遇到了一种使用"logback"启用日志记录的方法.现在,我需要将日志记录配置放入文件logback.xml中.

It had no effect. So I came across a method to enable logging using 'logback'. Now I need to put logging configuration into file logback.xml.

我的问题是在Spring中如何处理配置? logback.xml是否具有高于application.properties的优先级? 有没有一种方法只能使用一种配置?是application.properties还是logback.xml? 具有两种配置的意义何在?

My question is how configuration are handled in Spring? Do logback.xml is given priority over application.properties? Is there a method to use only one configuration? Either application.properties or logback.xml? Whats the point of having two configurations?

后来我发现,要启用EhCache日志记录,我需要在application.properties中添加以下行:

Later I found out, to enable EhCache logging, I need to add this line in my application.properties:

logging.level.net.sf.ehcache: DEBUG

推荐答案

Logback会在类路径中查找logback_test.xml文件,如果找不到第一个,则查找logback.xml.

Logback will look in the classpath for the files logback_test.xml, then logback.xml if it can't find the first.

假设您正在使用Maven,则属性文件中不需要任何内容​​.通常在src/main/resources中使用logback.xml,在src/test/resources中使用logback_test.xml.

Assuming you're using maven you do not need anything in your properties file. Usually logback.xml in src/main/resources and logback_test.xml in src/test/resources.

您将需要实现logback和api,通常在pom中,您需要这样做:

You will need an implementation of logback and a api, usually in your pom you need this:

<!-- API -->
<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-api</artifactId>
    <version>1.7.22</version>
</dependency>
<!-- Implementation -->
<dependency>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    <version>1.1.8</version>
</dependency>

Ehcache将使用slf4j,因此将提取您的登录配置.

Ehcache will use slf4j so your logback config will be picked up.

请参见 http://logback.qos.ch/manual/configuration.html用于登录配置.

这篇关于Spring Framework application.properties与logback.xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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