这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)
如何使用application.properties设置日志级别? 日志文件的位置,等等?
这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)
如何使用application.properties设置日志级别? 日志文件的位置,等等?
当前回答
设置根日志级别的正确方法是使用logging.level.root属性。参见文档,自最初提出这个问题以来,文档已经更新。
例子:
logging.level.root=WARN
其他回答
记录:官方文档,如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.
in spring boot project we can write logging.level.root=WARN but here problem is, we have to restart again even we added devtools dependency, in property file if we are modified any value will not autodetectable, for this limitation i came to know the solution i,e we can add actuator in pom.xml and pass the logger level as below shown in postman client in url bar http://localhost:8080/loggers/ROOT or http://localhost:8080/loggers/com.mycompany and in the body you can pass the json format like below
{
"configuredLevel": "WARN"
}
如果你想设置更多的细节,请添加一个日志配置文件名“logback.xml”或“logback-spring.xml”。
在你的应用中。属性文件,输入如下:
logging.config: classpath:logback-spring.xml
在loback-spring.xml中,像这样输入:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="ROOT_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>INFO</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>sys.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/system.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>500MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
</pattern>
</encoder>
</appender>
<appender name="BUSINESS_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>TRACE</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<file>business.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${LOG_DIR}/${SYSTEM_NAME}/business.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>500MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<encoder>
<pattern>%-20(%d{yyy-MM-dd HH:mm:ss.SSS} [%X{requestId}]) %-5level - %logger{80} - %msg%n
</pattern>
</encoder>
</appender>
<logger name="{project-package-name}" level="TRACE">
<appender-ref ref="BUSINESS_APPENDER" />
</logger>
<root level="INFO">
<appender-ref ref="ROOT_APPENDER" />
</root>
</configuration>
你可以使用你的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驱动器几个小时的操作;))。
确保Dave Syer技巧得到一些喜爱,因为向应用程序添加debug=true。属性确实会启用调试日志记录。