这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)
如何使用application.properties设置日志级别? 日志文件的位置,等等?
这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)
如何使用application.properties设置日志级别? 日志文件的位置,等等?
当前回答
如果使用eclipse IDE,并且您的项目是maven,请记住清理并构建项目以反映更改。
其他回答
如果您想使用不同的日志框架(例如log4j),我发现最简单的方法是禁用spring引导自己的日志记录并实现自己的日志记录。这样我就可以在一个文件log4j.xml(在我的例子中)中配置每个日志级别。
要实现这一点,你只需要将这些行添加到你的pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>
您可能已经有了第一个依赖项,只需要另外两个依赖项。请注意,这个例子只包含log4j。 以上就是全部内容,现在您就可以在log4j配置文件中为引导配置日志了!
假设应用程序的包名为com.company.myproject。然后,您可以在应用程序中为项目中的类设置日志级别,如下所示。属性文件
loging.level.com.company.myproject = DEBUG
loging.level.org.springframework.web = DEBUG和loging.level.org.hibernate = DEBUG将仅为Spring框架web和Hibernate的类设置日志级别。
用于设置日志文件位置使用
伐木。文件= /home/ubuntu/myproject
你可以使用你的application.properties来做到这一点。
logging.level。=ERROR ->设置根日志级别为ERROR ... logging.level。=DEBUG ->设置根日志级别为DEBUG log .file=${java.io.tmpdir}/myapp.log ->设置日志的绝对路径为TMPDIR/myapp.log 一个正常的默认应用程序集。关于使用概要文件进行日志记录的属性如下: application.properties:
spring.application.name=<your app name here>
logging.level.=ERROR
logging.file=${java.io.tmpdir}/${spring.application.name}.log
application-dev.properties:
logging.level.=DEBUG
logging.file=
当你在你最喜欢的IDE中开发时,你只需要添加一个-Dspring.profiles。active=dev作为应用程序运行/调试配置的VM参数。
这将使您在开发期间只在生产和调试日志中记录错误,而不将输出写入日志文件。这将提高开发期间的性能(并节省SSD驱动器几个小时的操作;))。
记录:官方文档,如Spring Boot v1.2.0。RELEASE和Spring v4.1.3 RELEASE:
If the only change you need to make to logging is to set the levels of various loggers then you can do that in application.properties using the "logging.level" prefix, e.g. logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR You can also set the location of a file to log to (in addition to the console) using "logging.file". To configure the more fine-grained settings of a logging system you need to use the native configuration format supported by the LoggingSystem in question. By default Spring Boot picks up the native configuration from its default location for the system (e.g. classpath:logback.xml for Logback), but you can set the location of the config file using the "logging.config" property.
现有的答案很好。我只是想与您分享一个新的spring引导功能,允许对日志进行分组并在整个组上设置日志级别。
来自文件的例子:
创建日志记录组
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
为组设置日志级别
logging.level.tomcat=TRACE
这是一个很好的功能,它带来了更多的灵活性。