Springboot Configuration Logback
The log configuration of the project is a more common case, the previous contact and use of spring combined with XML, the introduction of a few dependencies, and then write a logback.xml
configuration file, then what can be done in springboot?
I. Configuration Instructions
In the resource directory, create a new log file: logback-spring.xml
the following
<?xmlVersion= "1.0" encoding= "UTF-8"?><configuration> <!--%m output information,%p log level,%t thread name,%d date, full name of the%c class,%i index "incrementing from number 0",,,--> <!--Appender is the child node of the configuration and is the component responsible for writing the log. - <!--consoleappender: Output logs to console -- <appendername="STDOUT"class="Ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d [%t]%-5level%logger{36}.%m\ (%file:%line\)-%msg%n</pattern> <!--console also use UTF-8, do not use GBK, otherwise garbled Chinese-- <charset>UTF-8</charset> </encoder> </appender> <appendername="Erroralarm"class="Com.git.hui.story.common.alarm.ServiceAlarm"> <!--If you just want the Error level of the log, then you need to filter, the default is the info level of,thresholdfilter--> <filterclass="Ch.qos.logback.classic.filter.ThresholdFilter"> <level>ERROR</level> </filter> </appender> <!--Rollingfileappender: Scrolls log files to the specified file, logging to other files when a certain condition is met -- <!--The following approximate meaning is: 1. Save the log by date, date changed, rename the previous day's log file name to xxx% date% index, and the new log is still Demo.log <!--2. If the date does not change, but the current log file size exceeds 1KB, the current log is split rename- <appendername="Story"class="Ch.qos.logback.core.rolling.RollingFileAppender"> <!--If you just want the Error level of the log, then you need to filter, the default is the info level of,thresholdfilter--> <filterclass="Ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <File>Logs/story.log</File> <!--Rollingpolicy: Determines the behavior of rollingfileappender when scrolling occurs, involving file movement and renaming. - <!--Timebasedrollingpolicy: The most commonly used scrolling strategy, it is based on time to develop a rolling strategy that is both responsible for scrolling and also responsible for starting scrolling -- <rollingpolicyclass="Ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!--The name of the active file is changed once every time, based on the value of Filenamepattern. <!--file name: Log/demo.2018-06-23.0.log - <fileNamePattern>Logs/arch/story.%d.%i.log</fileNamePattern> <!--Each log file is generated, the log file is stored for 3 days --- <maxHistory>3</maxHistory> <timebasedfilenamingandtriggeringpolicyclass="Ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <!--maxfilesize: This is the size of the active file, the default value is 10MB, the test can be changed to 1KB viewing effect -- <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> <encoder> <!--pattern node to set the input format for the log- <pattern>%d%p (%file:%line\)-%m%n</pattern> <!--log encoding: Set character sets here--- <charset>UTF-8</charset> </encoder> </appender> <!--A package in the specified project, logging level when there is a log action behavior-- <!--levels are "high to Low": FATAL > ERROR > WARN > INFO > DEBUG > TRACE-- <!--Additivity=false is no longer passed to other logger--> after the match is indicated <loggername="Com.git.hui"level="DEBUG"additivity="false"> <appender-refref="STDOUT"/> <appender-refref="Story"/> <appender-refref="Erroralarm"/> </logger> <loggername="Com.github.hui"level="DEBUG"additivity="false"> <appender-refref="STDOUT"/> <appender-refref="Story"/> <appender-refref="Erroralarm"/> </logger> <!--console output log level - <rootlevel="INFO"> <appender-refref="STDOUT"/> </root></configuration>
Above is a basic log output configuration with additional notes:
- Can the configuration file name be a different
appender
filtering log levels in tags
logger
under one label there are multipleappender-ref
- Custom Appender Implementation Classes
II. Expansion 1. Profile Name
The profile name defaults to logback-spring.xml
, what if I want to change it mylog.xml
?
The main is application.yml
to modify the parameters specified in the configuration file
logging: config: classpath:mylog.xml
2. Logger label
Logger tag can be connected to a number of <appender-ref>
, that is, the log of the hit, will be used appender
to print again, such as above, to a log, in the console will output, in the log file will be output, if it is the error log, will trigger ServiceAlarm
the logic
So there's a problem that we might want different
<appender name="errorLog" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!--如果只是想要 Error 级别的日志,那么需要过滤一下,默认是 info 级别的,ThresholdFilter--> <filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>WARN</level> </filter></appender>
3. Custom Appender Implementation Class
In the previous configuration file, a custom alarm was used, AlarmService
mainly when the error log was received, the corresponding simple implementation is as follows
/*** Alarm* Created by @author Yihui in 16:43 18/6/23. */ Public classServicealarmextendsappenderbase<iloggingevent> {Private Static Final LongINTERVAL =Ten* +* -;Private LongLastalarmtime =0;@Override protected void Append(Iloggingevent iloggingevent) {if(Canalarm()) {Doalarm(Iloggingevent.Getformattedmessage()); } }Private Boolean Canalarm() {Longnow = System.Currenttimemillis();if(Now-lastalarmtime >= INTERVAL) {lastalarmtime = now;return true; }Else{return false; } }Private void Doalarm(String content) {Try{Emailwrapper.SendMail("Abnormal Alarm","[email protected]", content); }Catch(Exception e) {e.Printstacktrace(); } }}
4. Other
For detailed parameters in the Logback configuration file, you can refer to the blog post:
- Logback Concise User's Manual
Iii. other 1. A grey and grey blog:https://liuyueyi.github.io/hexblog
A gray and gray personal blog, recording all the study and work in the blog, welcome everyone to visit
2. Disclaimer
The letter is not as good as, has been on the content, purely opinion, because of limited personal ability, inevitably there are omissions and errors, such as the detection of bugs or better suggestions, welcome criticism, please feel grateful
- Weibo address: small Gray Gray Blog
- QQ: A grey/grey/3302797840
3. Scan for attention
180623-springboot logback configuration file