Spring-boot default uses Logback to record Logger,spring-boot package inside the org.springframework.boot.logging.logback
path there are some configuration files, which are used by default base.xml
, which output 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>
For output to a file, if you set Logging.file to use this value as the file name, if you set Logging.path to use Logging.path/spring.log as the file name, if you do not have the settings to put in the temporary file.
So the question is, if you want to write a file, will a file not be written very large?
You can look inside this base.xml
time include
.org/springframework/boot/logging/logback/file-appender.xml
Let's see org/springframework/boot/logging/logback/file-appender.xml
what's inside.
<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, there is a default configuration SizeBasedTriggeringPolicy
to indicate the size of the file, each file to 10MB will be re-opened a file, and the name of the previous names ${LOG_FILE}.%i
, so that the log file is too large to solve the problem.
Someone may want to ask, these are the default configuration, if I do not want to use the default configuration, I want to cut the log by day, how should I set it?
Can create a file under the Resources directory logback-spring.xml
, and give the desired configuration, here to a simple configuration Yes log files can be in days to cut log files
<?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.RollingFileA Ppender "> <encoder> <pattern>${FILE_LOG_PATTERN}</pattern> </encoder> <file>${LOG_FILE}</file> <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPo Licy "> <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.%i</fileNamePattern> <maxhistor y>365</maxhistory> <timebasedfilenamingandtriggeringpolicy class= "Ch.qos.logback.core.rolling.SizeA NDTIMEBASEDFNATP "> <maxfilesize>100MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <root level= "INFO" > <appender-ref ref= "CONSOLE"/> <appender-ref ref= " Time_file "/> </root></configuration>
%d{yyyy-MM-dd}
the above indicates that the named file name is prefixed with the original name followed by the date, <maxFileSize>100MB</maxFileSize>
indicating that each file size is 100MB. The log file is then cut and saved with the date.
So if you want to customize the format of the log, you can write logback-spring.xml
the file yourself to define it yourself.
Finally appication.yaml
, set the file name and log level on the configuration file.
spring: application: name: spring-boot-logginglogging: file: ./logs/spring-boot-logging.log level: com.dragon.study.spring.boot: DEBUG
If you need to logback-spring.xml
customize some configuration files, you can use the
<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>
Then appication.yaml
, in the configuration file, add the
logging.syslog.facility: syslog.change.hostnamelogging.syslog.facility: LOCAL1
Configure to override default values
PS: Encountered a pit, just want to turn off a packet under the log, not directly
logging: level: com.dragon.study.spring.boot: OFF
Because off is the keyword of yaml, it doesn't have any effect on it.
So if you want to turn off the log in a package, the correct way is to put a single quote on the off
logging: file: ./logs/spring-boot-logging.log level: com.dragon.study.spring.boot: ‘OFF‘
Finally, if you feel that what you are talking about can help you, and want to do more in-depth study, Welcome to Dabigatran 632109190 for discussion and study.
Set the logging of Spring-boot