Detailed explanation of common configuration of logback (2) <appender>

Detailed explanation of common configuration of logback (2) <appender>





<appender>:

<appender> is the 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 sub-nodes:

<encoder>: Format the log. (Specific parameters will be explained later)

<target>: String System.out or System.err, default System.out;

for example:

Xml code Collection code
1.<configuration> 
2. 
3. <appender name="STDOUT" class= "ch.qos.logback.core.ConsoleAppender"> 
4. <encoder> 
5. <pattern>%-4relative [%thread] %-5level %logger{35} - %msg %n</pattern> 
6. </ encoder> 
7. </appender> 
8. 

10. <appender-ref ref="STDOUT" /> 
11. </root> 
12. </configuration> 



2.FileAppender:

add the log to a file, with the following subnodes:

<file>: the name of the file to be written , which can be a relative directory or an absolute directory. If the parent directory does not exist, it will be automatically created, and there is no default value.

<append>: If true, the log is appended to the end of the file, if false, the existing file is emptied, the default is true.

<encoder>: Formats logging events. (The specific parameters will be explained later)

<prudent>: If it is true, the log will be safely written to the file, even if other FileAppenders are writing to this file, which is inefficient, and the default is false.

For example:

Xml code Collection code
1. <configuration> 
2. 
3. <appender name="FILE" class="ch.qos.logback.core.FileAppender"> 
4. <file>testFile.log</file> 
5. <append>true</append> 
6. <encoder> 
7. < 
8. </encoder> 
9. </appender> 
10.         
11. <root level="DEBUG"> 
12. <appender-ref ref="FILE" /> 
13. </root> 
14. </configuration> 




3 .RollingFileAppender:

Rolling record files, first record the log to the specified file, and when a certain condition is met, record the log to other files. It has the following subnodes:

<file>: The name of the file to be written, which can be a relative directory or an absolute directory. If the upper-level directory does not exist, it will be automatically created, and there is no default value.

<append>: If true, the log is appended to the end of the file, if false, the existing file is emptied, the default is true.

<encoder>: Formats logging events. (Specific parameters will be explained later)

<rollingPolicy>: Determines the behavior of RollingFileAppender when scrolling occurs, involving file movement and renaming.

<triggeringPolicy>: Tells the RollingFileAppender to properly activate scrolling.

<prudent> : When true, FixedWindowRollingPolicy is not supported. TimeBasedRollingPolicy is supported, but there are two restrictions, 1 does not support and does not allow file compression, 2 cannot set the file attribute and must be left blank.



rollingPolicy:



TimeBasedRollingPolicy: The most commonly used rolling policy, it formulates a rolling policy according to time, which is responsible for both rolling and starting rolling. It has the following sub-nodes:

<fileNamePattern>:

Required node, including file name and "%d" converter, "%d" can include a time format specified by java.text.SimpleDateFormat, such as: %d{yyyy-MM}. If you use %d directly, the default format is yyyy-MM-dd. The file byte point of RollingFileAppender is optional. By setting file, you can specify different locations for active files and archived files. The current log is always recorded to the file specified by file (active file), and the name of the active file will not change; if If file is not set, the name of the active file will be changed every so often according to the value of fileNamePattern. "/" or "\" are used as directory separators.



<maxHistory>:

An optional node, which controls the maximum number of archived files to be retained. If the number is exceeded, old files will be deleted. Assuming that the setting is rolled every month, and <maxHistory> is 6, only the files of the last 6 months are saved, and the old files before are deleted. Note that when deleting old files, those directories created for archiving are also deleted.





FixedWindowRollingPolicy: The rolling policy for renaming files according to the fixed window algorithm. It has the following child nodes:

<minIndex>: the minimum value of the window index

<maxIndex>: the maximum value of the window index, when the window specified by the user is too large, the window will be automatically set to 12.

<fileNamePattern>:

Must contain "%i" For example, assuming the minimum and maximum values ​​are 1 and 2, respectively, and the naming pattern is mylog%i.log, the archive files mylog1.log and mylog2.log will be generated. You can also specify file compression options, for example, mylog%i.log.gz or no log%i.log.zip




triggeringPolicy:



SizeBasedTriggeringPolicy: View the size of the currently active file, if it exceeds the specified size it will tell RollingFileAppender to trigger the currently active file to roll. There is only one node:

<maxFileSize>: This is the size of the active file, the default is 10MB.







For example: generate a log file every day and save the log file for 30 days.



Java code Collection code
1. <configuration>  
2. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
3.     
4. <rollingPolicy class="ch.qos.logback.core" .rolling.TimeBasedRollingPolicy">  
5. <fileNamePattern>logFile.%d{yyyy-MM-dd}.log</fileNamePattern>  
6. <maxHistory>30</maxHistory>   
7. <  

9. <encoder>  
10. <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>  
11. </encoder>  
12. </appender>   
13.  
14. <root level="DEBUG">  
15. <appender-ref ref="FILE" />  
16. </root>  
17.</configuration> 



  For example: generate log files in fixed window mode, when the file is larger than 20MB, generate new log file. The window size is 1 to 3, and when 3 archives are saved, the oldest log will be overwritten.

Xml code Collection code
1. <configuration>  
2. <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">  
3. <file>test.log</file>  
4.  
5. <rollingPolicy class="ch.qos.logback.core.rolling.  

7.      <minIndex>1</minIndex>  
8.      <maxIndex>3</maxIndex>  
9.    </rollingPolicy>  
10.  
11.    <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy">  
12.      <maxFileSize>5MB</maxFileSize>  
13.    </triggeringPolicy>  
14.    <encoder>  
15.      <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>  
16.    </encoder>  
17.  </appender>  
18.          
19.  <root level="DEBUG">  
20.    <appender-ref ref="FILE" />  
21.  </root>  
22.</configuration> 



4. In addition, there are SocketAppender, SMTPAppender, DBAppender, SyslogAppender, SiftingAppender, which are not commonly used. These are not explained here. You can refer to the official documentation. 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 other is to write the byte array to the output stream.

Currently PatternLayoutEncoder is the only useful and default encoder, with a <pattern> node to set the input format of the log. Use "%" plus "converter" method, if you want to output "%", you must use "\" to escape "\%".

For example:

Xml code Collection code
1.<encoder>  
2. <pattern>%-4relative [%thread] %-5level %logger{35} - %msg%n</pattern>  
3.</encoder 



in <pattern> Converter description: The



converter function is
c {length }
lo {length }
logger {length }
The logger name of the output log, there can be an integer parameter, the function is to shorten the logger name, setting it to 0 means only input the logger after the rightmost dot symbol. string. Conversion specifier Logger name Result
%logger mainPackage.sub.sample.Bar mainPackage.sub.
%logger{0} mainPackage.sub.sample.Bar Bar
%logger{5} mainPackage.sub.sample.Bar mssBar
%logger{10} mainPackage.sub.sample.Bar mssBar
%logger{15} mainPackage.sub.sample. Bar mssample.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 }
Output the fully qualified name of the caller that performed the logging request. The parameters are the same as above. Try to avoid it unless the speed of execution doesn't cause any problems.
contextName
cn
outputs the context name.
d {pattern }
date {pattern }
prints the log of the output log. The pattern syntax is compatible with java.text.SimpleDateFormat. 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} 20 oct. 2006 ;14:06:49.812

F/file Output the java source filename where the logging request was performed. Try to avoid it unless the speed of execution doesn't cause any problems.
caller{depth}caller{depth, evaluator-1, ... evaluator-n} Output the location information of the caller that generated the log, the integer option indicates the depth of the output information.
For example, %caller{2} outputs:

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, %caller{3} outputs:

16 [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 Output the line number where the log request was executed. Try to avoid it unless the speed of execution doesn't cause any problems.
m/msg/message Outputs information provided by the application.

M / method Outputs the name of the method that executes the log request. Try to avoid it unless the speed of execution doesn't cause any problems.
n Output platform-first line break "\n" or "\r\n".
p / le / level Output log level.
r/relative Output the time in milliseconds from program startup to the creation of the log record
t/thread Output the name of the thread that produced the log.
replace(p ){r, t} p is the log content, r is a regular expression, replace the content of p that matches r with t.

For example, the "%replace(%msg){'\s', ''}"






format modifier, used in conjunction with the switch:

an optional format modifier is between "%" and the switch.

The first optional modifier is the left-justification flag, represented by the minus sign "-"; followed by the optional minimum-width modifier, expressed as a decimal number. If the character is smaller than the minimum width, it will be left or right padding, the default is left padding (that is, right-aligned), and the padding character is a space. Characters are never truncated if they are larger than the minimum width. Maximum width modifier, the symbol is a period "." followed by a decimal number. Truncate from the front if the character is larger than the maximum width. A minus sign "-" is added after the dot symbol "." to add a number, which means truncation from the tail.





For example: %-4relative means that the output is left-aligned from the time the program is started to the time the log record is created with a minimum width of 4

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326686602&siteId=291194637