Set spring-boot logging and spring-bootlogging
By default, spring-boot uses logback to record logger and spring-boot packages.org.springframework.boot.logging.logback
There are some configuration files under the path, which will be used by defaultbase.xml
, Which outputs logs to the console and files. Its content is:
<included> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="FILE" /> </root></included>
If logging is set for the output to a file. this value is used as the file name if logging is set. the path uses logging. path/spring. log is used as the file name. If none of them are set, it is put into the temporary file.
So the question is, if you want to write a file, will it not be too large?
At this time, you can seebase.xml
Insideinclude
Noworg/springframework/boot/logging/logback/file-appender.xml
Let's take a look.org/springframework/boot/logging/logback/file-appender.xml
What is in it?
<included> <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <fileNamePattern>${LOG_FILE}.%i</fileNamePattern> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender></included>
Yes, the default configuration containsSizeBasedTriggeringPolicy
To indicate the file size. When each file reaches 10 MB, it is re-opened and named${LOG_FILE}.%i
In this way, the log file is too large.
Some people may want to ask, these are all default configurations. If I don't want to use the default configuration, what should I do if I want to cut logs every day?
You can createlogback-spring.xml
And provide the desired configuration. Here, a simple configuration is provided. The log file can be cut by day.
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <property name="LOG_FILE" value="${LOG_FILE:-${LOG_PATH:-${LOG_TEMP:-${java.io.tmpdir:-/tmp}}}/spring.log}"/> <include resource="org/springframework/boot/logging/logback/console-appender.xml" /> <appender name="TIME_FILE" class="ch.qos.logback.core.rolling.RollingFileAppender"> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <file>${LOG_FILE}</file> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern> <maxHistory>365</maxHistory> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <root level="INFO"> <appender-ref ref="CONSOLE" /> <appender-ref ref="TIME_FILE" /> </root></configuration>
Above%d{yyyy-MM-dd}
Indicates that the name of the named file is suffixed with a date after the original name,<maxFileSize>100MB</maxFileSize>
The size of each file is 100 MB. In this way, log files are cut by date and saved.
Therefore, if you want to customize the log format, you can write it by yourself.logback-spring.xml
This file is defined by yourself.
Finally, in the configuration fileappication.yaml
Set the file name and log level.
spring: application: name: spring-boot-logginglogging: file: ./logs/spring-boot-logging.log level: com.dragon.study.spring.boot: DEBUG
If you wantlogback-spring.xml
Customize some configuration files, which can be used
<springProperty scope="context" name="SYSLOG_HOST" source="logging.syslog.host" defaultValue="syslog.hostname" /> <springProperty scope="context" name="SYSLOG_FACILITY" source="logging.syslog.facility" defaultValue="LOCAL7"/> <appender name="SYSLOG" class="ch.qos.logback.classic.net.SyslogAppender"> <syslogHost>${SYSLOG_HOST}</syslogHost> <facility>${SYSLOG_FACILITY}</facility> </appender>
Thenappication.yaml
Add to the configuration file
logging.syslog.facility: syslog.change.hostnamelogging.syslog.facility: LOCAL1
Configure to replace the default value
PS: If you encounter a pitfall, you just want to turn off the logs under a certain package.
logging: level: com.dragon.study.spring.boot: OFF
Because OFF is the key word of yaml, writing above does not work.
So if you want to disable the logs under a certain package, the correct method is to OFF and put a single quotation mark on it.
logging: file: ./logs/spring-boot-logging.log level: com.dragon.study.spring.boot: 'OFF'
Finally, if you think that what you are talking about can help you and want to learn more in detail, you are welcome to join group 632109190 for discussion and study.