logback Common configuration details (ii.) <appender>
<appender>:
<appender> is a child node of <configuration> and is the component responsible for writing logs.
<appender> has two required attributes name and class. Name Specifies the Appender name, and class specifies the fully qualified name of the Appender.
1.ConsoleAppender:
Add the log to the console with the following child nodes:
<ENCODER>: Formats the log. (Detailed parameters will be explained later)
<target>: string system.out or system.err , default system.out ;
For example:
XML code
- <configuration>
- <appender name= "STDOUT" class= "Ch.qos.logback.core.ConsoleAppender" >
- <encoder>
- <pattern>%-4relative [%thread]%-5level%logger{35}-%msg%n</pattern>
- </encoder>
- </appender>
- <root level= "DEBUG" >
- <appender-ref ref= "STDOUT"/>
- </root>
- </configuration>
2.FileAppender:
Add the log to the file with the following child nodes:
<file>: The file name to be written, can be relative to the directory, or it can be an absolute directory, if the parent directory does not exist automatically created, there is no default value.
<append>: If true, the log is appended to the end of the file and, if False, empties the existing file, which is true by default.
<ENCODER>: Formats the logged event. (Detailed parameters will be explained later)
<prudent>: If true, the log will be safely written to the file, even if other fileappender are writing to this file, the efficiency is low, and the default is False.
For example:
XML code
- <configuration>
- <appender name= "FILE" class= "Ch.qos.logback.core.FileAppender" >
- <file>testFile.log</file>
- <append>true</append>
- <encoder>
- <pattern>%-4relative [%thread]%-5level%logger{35}-%msg%n</pattern>
- </encoder>
- </appender>
- <root level= "DEBUG" >
- <appender-ref ref= "FILE"/>
- </root>
- </configuration>
3.RollingFileAppender:
Scrolls through the log file, logging to the specified file, and logging to other files when a certain condition is met. Has the following child nodes:
<file>: The file name to be written, can be relative to the directory, or it can be an absolute directory, if the parent directory does not exist automatically created, there is no default value.
<append>: If true, the log is appended to the end of the file and, if False, empties the existing file, which is true by default.
<ENCODER>: Formats the logged event. (Detailed parameters will be explained later)
<rollingpolicy>: When scrolling occurs, determines the behavior of Rollingfileappender , involving file movement and renaming.
<triggeringpolicy : tell Rollingfileappender to activate scrolling.
<prudent>: When True, Fixedwindowrollingpolicy is not supported. Support Timebasedrollingpolicy, but there are two restrictions, 1 does not support or allow file compression, 2 cannot set the file property, must be left blank.
Rollingpolicy:
Timebasedrollingpolicy: The most commonly used scrolling strategy, which is based on time to develop a scrolling strategy that is both responsible for scrolling and for starting scrolling. Has the following child nodes:
<Filenamepattern>:
Necessary nodes, including the file name and the "%d" translator, "%d" can contain a java.text.SimpleDateFormat指定的时间格式,如:%d{yyyy-MM}。如果直接使用 %d,默认格式是 yyyy-MM-dd。 rollingfileappender file byte point is optional, by setting file, you can specify a different location for the active file and the archive file, The current log is always logged to file (active file), the name of the active file does not change, and if file is not set, the name of the active file is changed every time, based on the value of Filenamepattern. "/" or "\" will be treated as a directory delimiter.
<Maxhistory>:
An optional node that controls the maximum number of archived files that are retained, and deletes old files if they exceed the number. Assuming that the settings are scrolled every month, and <maxhistory> is 6, only the last 6 months of files are saved, and the old files are deleted. Note that deleting old files is the ones that are created to be archived and also deleted.
Fixedwindowrollingpolicy: renames a file's scrolling policy based on the fixed window algorithm. Has the following child nodes:
<minindex>: Window index Minimum value
<maxindex>: Window index maximum, when the user-specified window is too large, the window is automatically set to 12.
<Filenamepattern;:
Must contain "%i" for example, assuming that the minimum and maximum values are 1 and 2 respectively, and that the named mode is Mylog%i.log, the archive file Mylog1.log and Mylog2.log are generated. You can also specify file compression options, for example, mylog%i.log.gz or no log%i.log.zip
Triggeringpolicy:
Sizebasedtriggeringpolicy: to see the size of the currently active file, if exceeding the specified size tells Rollingfileappender to trigger the current active file scroll. Only one node :
<maxfilesize>: This is the size of the active file, and the default value is 10MB.
For example: Generate a log file every day and save 30 days of log files.
Java code
- <configuration>
- <appender name= "FILE" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
- <rollingpolicy class= "Ch.qos.logback.core.rolling.TimeBasedRollingPolicy" >
- <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>
- <maxHistory>30</maxHistory>
- </rollingPolicy>
- <encoder>
- <pattern>%-4relative [%thread]%-5level%logger{35}-%msg%n</pattern>
- </encoder>
- </appender>
- <root level= "DEBUG" >
- <appender-ref ref= "FILE"/>
- </root>
- </configuration>
For example: Generate a log file in fixed window mode and generate a new log file when the file is larger than 20MB. The window size is 1 to 3, and when 3 archive files are saved, the oldest log is overwritten.
XML code
- <configuration>
- <appender name= "FILE" class= "Ch.qos.logback.core.rolling.RollingFileAppender" >
- <file>test.log</file>
- <rollingpolicy class= "Ch.qos.logback.core.rolling.FixedWindowRollingPolicy" >
- <fileNamePattern>tests.%i.log.zip</fileNamePattern>
- <minIndex>1</minIndex>
- <maxIndex>3</maxIndex>
- </rollingPolicy>
- <triggeringpolicy class= "Ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy" >
- <maxFileSize>5MB</maxFileSize>
- </triggeringPolicy>
- <encoder>
- <pattern>%-4relative [%thread]%-5level%logger{35}-%msg%n</pattern>
- </encoder>
- </appender>
- <root level= "DEBUG" >
- <appender-ref ref= "FILE"/>
- </root>
- </configuration>
4. There are also Socketappender, Smtpappender, Dbappender, Syslogappender, Siftingappender, not commonly used, these are not explained here, you can refer to the official documents. Of course, you can write your own appender.
<encoder>:
Responsible for two things, one is to convert the log information into a byte array, and the second is to write the byte array to the output stream.
Currently Patternlayoutencoder is the only useful and default encoder , with a <pattern> node that sets the input format for the log. Using the "%" plus "convert" method, if you want to output "%", you must use "\" to escape "\%".
For example:
XML code
- <encoder>
- <pattern>%-4relative [%thread]%-5level%logger{35}-%msg%n</pattern>
- </encoder
<pattern> inside the translator description:
| Conversion character |
function |
c {length } Lo {length } Logger {length } |
The logger name of the output log, which can have an shaping parameter that shortens the logger name and is set to 0 to enter only the string after the rightmost point symbol of logger.
| Conversion specifier |
Logger name |
Result |
| %logger |
MainPackage.sub.sample.Bar |
MainPackage.sub.sample.Bar |
| %LOGGER{0} |
MainPackage.sub.sample.Bar |
Bar |
| %LOGGER{5} |
MainPackage.sub.sample.Bar |
M.s.s.bar |
| %LOGGER{10} |
MainPackage.sub.sample.Bar |
M.s.s.bar |
| %LOGGER{15} |
MainPackage.sub.sample.Bar |
M.s.sample.bar |
| %LOGGER{16} |
MainPackage.sub.sample.Bar |
M.sub.sample.bar |
| %LOGGER{26} |
MainPackage.sub.sample.Bar |
MainPackage.sub.sample.Bar |
|
C {length } class {length } |
Outputs the fully qualified name of the caller who executed the record request. The parameters are the same as above. Try to avoid using it unless the speed of execution does not cause any problems. |
Contextname cn |
The output context name. |
d {pattern } Date {pattern } |
Print logs for output logs, pattern syntax and java.text.SimpleDateFormat compatibility.
| Conversion Pattern |
Result |
| %d |
2006-10-20 14:06:49,812 |
| %date |
2006-10-20 14:06:49,812 |
| %date{iso8601} |
2006-10-20 14:06:49,812 |
| %date{hh:mm:ss. SSS} |
14:06:49.812 |
| %DATE{DD MMM yyyy; HH:mm:ss. SSS} |
Oct. 2006;14:06:49.812 |
|
| F/file |
Output the Java source file name of the execution record request. Try to avoid using it unless the speed of execution does not cause any problems. |
| Caller{depth}caller{depth, Evaluator-1, ... evaluator-n} |
Outputs the location information of the caller of the generated log, and the integer option indicates the depth of the output information.
For example, the %caller{2} output is:
0 [main] debug-logging statement caller+0 at MainPackage.sub.sample.Bar.sampleMethodName (bar.java:22) Caller+1 at mainPackage.sub.sample.Bar.createLoggingRequest (bar.java:17)
For example, the %caller{3} output is:
[main] debug-logging statement caller+0 at MainPackage.sub.sample.Bar.sampleMethodName (bar.java:22) Caller+1 at mainPackage.sub.sample.Bar.createLoggingRequest (bar.java:17) caller+2 at MainPackage.ConfigTester.main (configtester.java:38) |
| L/line |
The line number of the output execution log request. Try to avoid using it unless the speed of execution does not cause any problems. |
| M/msg/message |
Outputs the information provided by the application. |
| M/method |
The name of the method that outputs the execution log request. Try to avoid using it unless the speed of execution does not cause any problems. |
| N |
The output platform first closes the line break "\ n" or "\ r \ n". |
| P/le/level |
The output log level. |
| R/relative |
The time, in milliseconds, that the output was initiated from the program to the creation of the log record |
| T/thread |
Output the name of the thread that generated the log. |
| Replace (p ) {R, T} |
p is the log content,R is the regular expression, and the content of R in p is replaced with T .
For example, "%replace (%msg) {' \s ', '}" |
Format modifiers, which are used in conjunction with the conversion character:
The optional format modifier is located between the "%" and the conversion character.
The first optional modifier is the left-aligned flag, which is a minus sign "-", followed by the optional minimum width modifier, in decimal notation. If the character is less than the minimum width, the left or right padding is left padding (that is, right-aligned) and the padding is a space. If the character is greater than the minimum width, the character is never truncated. The maximum width modifier, the symbol is the dot number "." followed by a decimal number. If the character is greater than the maximum width, it is truncated from the front. Dot symbol "." followed by a minus sign "-" in addition to the number, indicating truncation from the tail.
For example,%-4relative indicates that the output is left-aligned and has a minimum width of 4 from the start of the program to the time the log record was created.
Logback General configuration (b) <appender>