这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)
如何使用application.properties设置日志级别? 日志文件的位置,等等?
这个问题很简单,但我找不到资料。 (可能我对Java框架的知识严重缺乏)
如何使用application.properties设置日志级别? 日志文件的位置,等等?
当前回答
在我当前的配置中,我在应用程序中定义了它。Yaml是这样的:
logging:
level:
ROOT: TRACE
我正在使用spring-boot:2.2.0.RELEASE。您可以像这样定义任何应该具有TRACE级别的包。
其他回答
记录:官方文档,如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 Boot v1.2.0开始。释放,应用程序中的设置。属性或应用程序。你可以。请参见参考指南的日志级别部分。
logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR
对于早期版本的Spring Boot,您不能这样做。为此,您只需使用日志框架的正常配置(log4j、logback)。将适当的配置文件(log4j.xml或logback.xml)添加到src/main/resources目录并根据自己的喜好进行配置。
当从命令行启动应用程序时,可以通过指定——debug来启用调试日志记录。
Spring Boot还为logback提供了一个很好的起点来配置一些默认值,着色等。base.xml文件可以简单地包含在你的logback.xml文件中。(这也是Spring Boot中默认的logback.xml的建议。
<include resource="org/springframework/boot/logging/logback/base.xml"/>
如果您使用Spring Boot,那么您可以直接在应用程序中添加以下属性。属性文件设置日志级别, 自定义日志记录模式并将日志存储在外部文件中。
这些是不同的日志级别及其从最小值<<最大值的顺序。
关闭<<致命<<错误<<警告<< info << debug << trace << all
# To set logs level as per your need.
logging.level.org.springframework = debug
logging.level.tech.hardik = trace
# To store logs to external file
# Here use strictly forward "/" slash for both Windows, Linux or any other os, otherwise, its won't work.
logging.file=D:/spring_app_log_file.log
# To customize logging pattern.
logging.pattern.file= "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
请通过这个链接来更生动地定制您的日志。
https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
如果你想设置更多的细节,请添加一个日志配置文件名“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>
在Springboot 2中,你可以像这样用环境变量设置根日志级别:
logging.level.root=DEBUG
或者你可以像这样为包设置特定的日志记录:
logging.level.my.package.name=TRACE