In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you the "Springboot component Logback how to use", the content is easy to understand, clear, hope to help you solve your doubts, the following let the editor lead you to study and learn "Springboot component Logback how to use" this article.
First, import dependency
General project
Ch.qos.logback logback-core 1.1.11 ch.qos.logback logback-classic 1.1.11
Springboot project
Org.springframework.boot spring-boot-starter-web II. Configuration file parsing
By default, Logback looks for logback-test.xml and logback.xml under the classpath.
Spring Boot will look for logback-test.groovy, logback-test.xml, logback.groovy or logback.xml under classpath. If none can be found, logback-spring.xml under the project path will be loaded.
III. Configuration file structure
IV. The role of each component
Logger: logger, root is a special logger- top-level logger because logger has an inheritance relationship, which will be described later
Appender: configure log file output destination
Encoder: controls the log output format, which is implemented with the help of layout
RollingPolicy: RollingFileAppender has this child element, which specifies the behavior when scrolling occurs
TriggeringPolicy: RollingFileAppender has this child element, which specifies when scrolling occurs, and generally does not need to be configured, because the most popular TimeBasedRollingPolicy implements both rolling policy and triggering policy.
Filter: filter the logs received by appender, and only those logs that meet the Filter criteria are output to the log file
5. Logger components
The logger component has a parent-child hierarchy, and root is the top-most logger. The logger recorder is a named entity. Their names are case-sensitive and follow hierarchical naming rules.
If the name of a logger is followed by a dot, the logger is the ancestor of another logger, and the name of the following point is prefixed to the name of its descendant logger. If there is no ancestor between the recorder and the child recorder, the recorder is called the parent of the child recorder. For example, a logger named "com.foo" is the parent of a logger named "com.foo.Bar". Similarly, "java" is the ancestor recorder of "java.util" and "java.util.Vector". Most developers should be familiar with this naming scheme.
Logger log level inheritance
If a given logger is not assigned a level, it inherits an assigned level from its nearest ancestor. For example, the valid level of a given logger L is equal to the first non-empty level in its hierarchy, starting with L itself, and then looking up in the hierarchy to root logger. To ensure that all loggers eventually inherit levels, root logger always has an assigned level, and the default level for root logger is DEBUG.
Note: a logging request is valid if the level of the logging request is higher than or equal to the valid level of the logger. Log levels are sorted in the following order: TRACE < DEBUG < INFO < WARN < ERROR.
Appender inheritance associated with logger
A logger can configure multiple appender,logger output logs will be output to the current logger bound appender and parents logger (until root logger) bound appender, you can set the additivity attribute to false, then the logger output log will only be output to the current logger bound six, logger attribute
Name: specifies the fully qualified package name
Level: log output level, if not configured, refer to the level inheritance above
Additivity: default is true. Refer to appender inheritance above.
If additivity is true, there will be duplicate output logs, as shown below
% d {HH:mm:ss.SSS} [% thread]%-5level% logger {36} -% msg%n
Then the log under the chapters.configuration package will be output to the console console twice, as shown below
14 main 25 Entering application 36.343 [main].
14 main 25 Entering application 36.343 [main].
14 main 25 Did it again 36.359 [main] Rd-Did it again!
14 main 25 Did it again 36.359 [main] Rd-Did it again!
14 main 25 Exiting application 36.359 [main].
14 main 25 Exiting application 36.359 [main].
7. Appender components
Configure the destination of log output, such as ConsoleAppender, FileAppender, RollingFileAppender
ConsoleAppender
System.out%-4relative [% thread]%-5level% logger {35} -% msg% n
Write logs to the standard output stream. You can configure the target attribute to system.out, or you can specify System.error. The default System.out
FileAppender
The child elements contained are
Append: if true, append to the existing log file, otherwise truncate the existing file. Default is true.
File: specifies the name of the file to which the log is written. If the file does not exist, create a directory that includes the parent. For windows systems to avoid "", the correct example can be specified as c:/temp/test.log or c:\ temp\ test.log
Encoder: output format RollingFileAppender
RollingFileAppender
FileAppender is extended with the ability to scroll update log files; for example, RollingFileAppender can log in to a file called log.txt, and once a condition is met, it can save the current log file and scroll out to a new log file.
The child elements contained are
Append: if true, append to the existing log file, otherwise truncate the existing file. Default is true.
File: specifies the name of the file to which the log is written. If the file does not exist, create a directory that includes the parent. For windows systems to avoid "", the correct example can be specified as c:/temp/test.log or c:\ temp\ test.log
Encoder: format of output
RollingPolicy: specifies the behavior when scrolling occurs in RollingFileAppender
TriggeringPolicy: specifies when the scrolling process occurs in RollingFileAppender
RollingPolicy
TimeBasedRollingPolicy and SizeAndTimeBasedRollingPolicy are commonly used in rollingPolicy.
TimeBasedRollingPolicy
TimeBasedRollingPolicy is the most popular scrolling strategy. It scrolls based on time, which can be monthly or daily, etc. The TimeBasedRollingPolicy is responsible for scrolling and triggering the scrolling. It implements rolling policy as well as triggering policy.
The child elements contained are
FileNamePattern: required; its value should include the file name and the appropriately placed% d conversion specifier. The% d can contain the date and time pattern java.text.SimpleDateFormat class by the specified conversion description. If the date and time mode is omitted, multiple% d can be specified in the default mode yyyy-MM-dd;, but only one major, the rest are marked with 'aux'', and multiple% d allows you to organize the classification of log files, for example, by year and month folder / var/log/%d {yyyy/MM, aux} / myapplication.%d {yyyy-MM-dd} .log
MaxHistory: set the time for archiving files to be saved, and log files to be saved for at least 15 days, because some exceptions occur frequently on a weekly basis.
TotalSizeCap: controls the size of archived files
SizeAndTimeBasedRollingPolicy
Support split by time, and then split by file size
Mylog.txt mylog-%d {yyyy-MM-dd}.% i.txt 100MB 60 20GB% msg%n
"% I" indicates that the log file reaches "maxFileSize" and it will archive the log in an incremental sequence (starting with 0).
Triggering policy
SizeBasedTriggeringPolicy
The triggering policy configures a maxFileSize parameter, and if the log file exceeds this threshold, it informs RollingFileAppender to trigger the log roll. But in general, triggering policy does not have to be configured, and TimeBasedRollingPolicy has been perfectly integrated.
Add: all the above Appender logs are recorded locally, and logback also supports network output logs, local data, e-mails, etc. If necessary, we will study them later. Logback can also use AsyncAppender to log asynchronously.
Encoder
Encoder is responsible for converting events to a byte array and writing the byte array to the output stream. PatternLayoutEncoder is the most commonly used encoder, which formats log events by using PatternLayout. We can configure PatternLayoutEncoder directly instead of PatternLayout, which uses PatternLayout for format output by default.
Layout
Responsible for converting events into strings, we can customize the layout and then refer to it through encoder
PatternLayout
It converts log events into strings, but you can customize strings by adjusting the conversion pattern of PatternLayout.
The transformation pattern of PatternLayout is closely related to the printf () function of the C language.
The commonly used modes are:
% d {pattern}: date
% level log level
% thread thread name
% class fully qualified class name, the class in which the log request is recorded
The method where the method log request record is located
Line number of% line log request record
% n line feeds
% logger {length} outputs the name of the log logger logger, length specifies the length of the output name, and logback intelligently abbreviates without losing semantics.
% msg output messages logged by logger
% d {yyyy-MM-dd HH:mm:ss.SSS,CTT}%-5level [% thread] [% class:%line]% logger {50} -% msg%n
Delimiters between multiple patterns, in most cases, text should contain spaces or other delimited characters ('[',']','-') so that they will not be confused with converted words
Filter
Call the decide method to filter the logs received by appender. Only the logs that meet the Filter conditions are output to the log file. The decide method has three return values, DENY, ACCEPT, and NEUTRAL.
If the returned value is DENY, the log event is deleted immediately without consulting the remaining filters
If the returned value is NEUTRAL neutral, the next filter in the query list, and if no other filter is available for query, the log event will be handled normally
If the return value is ACCEPT, log events are processed immediately, skipping calls to the remaining filters.
LevelFilter
If the event log level is equal to the configured level, the filter accepts or rejects the event log, depending on the onMatch and onMismatch attributes
INFO ACCEPT DENY%-4relative [% thread]%-5level% logger {30} -% msg%n
ThresholdFilter
Filters events below the specified threshold. For events that are equal to or above the threshold, the NEUTRAL is responded when its () method is called. However, events with a level below the threshold will be rejected.
INFO
EvaluatorFilter
EvaluatorFilter is an encapsulated universal filter EventEvaluator. As the name implies, evaluate whether the object meets the given conditions for a given event. When there is a match and a mismatch, the managed returns the value specified by the onMatch or onMismatch property, respectively.
GEventEvaluator implements EventEvaluator and uses arbitrary Groovy language Boolean expressions as the concrete implementation of the evaluation criteria through child elements.
JaninoEventEvaluator implements EventEvaluator, using arbitrary Java language blocks to return Boolean values through child elements as evaluation criteria.
UatorFilter
EvaluatorFilter is an encapsulated universal filter EventEvaluator. As the name implies, evaluate whether the object meets the given conditions for a given event. When there is a match and a mismatch, the managed returns the value specified by the onMatch or onMismatch property, respectively.
GEventEvaluator implements EventEvaluator and uses arbitrary Groovy language Boolean expressions as the concrete implementation of the evaluation criteria through child elements.
JaninoEventEvaluator implements EventEvaluator, using arbitrary Java language blocks to return Boolean values through child elements as evaluation criteria.
EvaluatorFilter can also support regular matching through child elements.
The above is all the content of the article "how to use Springboot component Logback". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.