Details on common logback configurations (2) appenderappender: appender is a sub-node of configuration and a component used to write logs. Appender has two necessary attributes: name and class. Name specifies the appender name and class specifies the fully qualified appender name. 1. leleappender: adds logs to the console and has the following subnodes:
Details on common logback configurations (2) appender: appender is a sub-node of configuration and a component used to write logs. Appender has two necessary attributes: name and class. Name specifies the appender name and class specifies the fully qualified appender name. 1. leleappender: adds logs to the console and has the following subnodes:
Common logback configurations (2)
:
Yes Is the component responsible for writing logs.
There are two necessary attributes: name and class. Name specifies the appender name and class specifies the fully qualified appender name.
1. leleappender:
Add logs to the console with the following subnodes:
: Format the log. (The specific parameters will be explained later)
: String System. outOr System. err, Default System. out;
For example:
Xml Code
- <Configuration>
-
- <AppenderName = "STDOUT" class = "ch. qos. logback. core. leleappender">
- <Encoder>
- <Pattern>%-4 relative [% thread] %-5 level % logger {35}-% msg % n Pattern>
- Encoder>
- Appender>
-
- <RootLevel = "DEBUG">
- <Appender-refRef = "STDOUT"/>
- Root>
- Configuration>
2. FileAppender:
Add logs to a file with the following subnodes:
: The name of the file to be written. It can be a relative directory or an absolute directory. If the parent directory does not exist, it will be automatically created with no default value.
: If it is true, the log is appended to the end of the file. If it is false, the existing file is cleared. The default value is true.
: Format record events. (The specific parameters will be explained later)
: If it is true, logs will be securely written to the file, even if other FileAppender is writing to this file, the efficiency is low. The default value is false.
For example:
Xml Code
- <Configuration>
-
- <AppenderName = "FILE" class = "ch. qos. logback. core. FileAppender">
- <File>TestFile. log File>
- <Append>True Append>
- <Encoder>
- <Pattern>%-4 relative [% thread] %-5 level % logger {35}-% msg % n Pattern>
- Encoder>
- Appender>
-
- <RootLevel = "DEBUG">
- <Appender-refRef = "FILE"/>
- Root>
- Configuration>
3. RollingFileAppender:
Scroll the record file, first record the log to the specified file, when a condition is met, the log is recorded to other files. The following sub-nodes are available:
: The name of the file to be written. It can be a relative directory or an absolute directory. If the parent directory does not exist, it will be automatically created with no default value.
: If it is true, the log is appended to the end of the file. If it is false, the existing file is cleared. The default value is true.
: Format record events. (The specific parameters will be explained later)
: When rolling occurs, it is decided RollingFileAppenderFile movement and rename.
>: NoticeRollingFileAppenderActivate scroll.
: If it is true, FixedWindowRollingPolicy is not supported. TimeBasedRollingPolicy is supported, but there are two restrictions: 1 does not support file compression, 2 does not support setting file attributes, and must be left blank.
RollingPolicy:
TimeBasedRollingPolicy:The most commonly used rolling policy, which is used to create a rolling policy based on time. It is responsible for both rolling and rolling. The following sub-nodes are available:
:
Required node, including the file name and "% d" conversion character. "% d" can containThe time format specified by java. text. SimpleDateFormat, for example, % d {yyyy-MM }. If % d is used directly, the default format is yyyy-MM-dd.
RollingFileAppenderYou can set file to specify different locations for the active file and archive file. The current log is always recorded in the file (active file ), the name of the active file will not change. If file is not set, the name of the active file will beFileNamePatternValue, which is changed every other time. "/" Or "\" is used as the directory separator.
:
An optional node that controls the maximum number of archive files that are retained. If the maximum number is exceeded, the old files are deleted. For example, if you set to scroll every month, and If the value is 6, only the files in the last six months are saved and the old files are deleted. Note that deleting old files is the same as deleting directories created for archiving.
FixedWindowRollingPolicy:Rename the file rolling policy based on the fixed window algorithm. The following sub-nodes are available:
: Minimum window index value
: Maximum window index value. When the specified window is too large, the window is automatically set to 12.
:
It must contain "% I". For example, if the minimum and maximum values are 1 and 2 respectively and the naming mode is mylog % I. log, the archive files mylog1.log and mylog2.log will be generated. You can also specify a compressed file, for example, mylogpolici.log.gz or no logpolici.log.zip
TriggeringPolicy:
SizeBasedTriggeringPolicy:View the size of the current active file. If the size exceeds the specified size, the system will notify youRollingFileAppenderTrigger the current active file rolling. Only one node:
: The size of the active file. The default value is 10 MB.
For example, a log file is generated every day and saved for 30 days.
Java code
-
- "FILE"Class= "Ch. qos. logback. core. rolling. RollingFileAppender">
-
- Class = "Ch. qos. logback. core. rolling. TimeBasedRollingPolicy">
- LogFile. % d {yyyy-MM-dd}. log
- 30
-
-
-
- %-4 relative [% thread] %-5 level % logger {35}-% msg % n
-
-
-
-
- "FILE"/>
-
-
For example, a log file is generated in fixed window mode. When the file is larger than 20 mb, a new log file is generated. The window size is 1 to 3. When three archive files are saved, the earliest logs are overwritten.
Xml Code
- <Configuration>
- <AppenderName = "FILE" class = "ch. qos. logback. core. rolling. RollingFileAppender">
- <File>Test. log File>
-
- <RollingPolicyClass = "ch. qos. logback. core. rolling. FixedWindowRollingPolicy">
- <FileNamePattern>Tests.polici.log.zip FileNamePattern>
- <MinIndex>1 MinIndex>
- <MaxIndex>3 MaxIndex>
- RollingPolicy>
-
- <TriggeringPolicyClass = "ch. qos. logback. core. rolling. SizeBasedTriggeringPolicy">
- <MaxFileSize>5 MB MaxFileSize>
- TriggeringPolicy>
- <Encoder>
- <Pattern>%-4 relative [% thread] %-5 level % logger {35}-% msg % n Pattern>
- Encoder>
- Appender>
-
- <RootLevel = "DEBUG">
- <Appender-refRef = "FILE"/>
- Root>
- Configuration>
4. In addition, SocketAppender, SMTPAppender, DBAppender, SyslogAppender, and SiftingAppender are not commonly used. These are not described here. You can refer to the official documentation. Of course, you can write your own Appender.
:
One is to convert the log information into a byte array and the other is to write the byte array to the output stream.
CurrentlyPatternLayoutEncoderIs the only useful and defaultEncoder, There is Node, used to set the log input format. Use "%" and "conversion character". To output "%", use "\" to escape "\ %.
For example:
Xml Code
- <Encoder>
- <Pattern>%-4 relative [% thread] %-5 level % logger {35}-% msg % n Pattern>
- Encoder
Description of the Conversion characters:
Conversion character |
Function |
C{Length} Lo{Length} Logger{Length} |
The logger name of the output log, which can have an integer parameter. The function is to shorten the logger name. If it is set to 0, it indicates that only the string following the vertices symbol on the rightmost of the logger is entered.
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} |
The full qualified name of the caller who output the execution record request. The parameters are the same as those above. Avoid using it unless the execution speed does not cause any problems. |
ContextName Cn |
Name of the output context. |
D{Pattern} Date{Pattern} |
Output log printing, mode syntax andjava.text.SimpleDateFormat Compatible.
Conversion Pattern |
Result |
% D |
14:06:49, 812 |
% Date |
14:06:49, 812 |
% Date {ISO8601} |
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 |
Name of the java source file that outputs the execution record request. Avoid using it unless the execution speed does not cause any problems. |
Caller {depth} caller {depth, evaluator-1,... evaluator-n} |
The location information of the caller who outputs the generated log. The integer option indicates the depth of the output information. For example,% Caller {2}Output: 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}Output: 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 |
The row number of the execution log request. Avoid using it unless the execution speed does not cause any problems. |
M/msg/message |
Outputs Information provided by the application. |
M/method |
Method Name of the output log request. Avoid using it unless the execution speed does not cause any problems. |
N |
The branch Letter "\ n" or "\ r \ n" of the output platform ". |
P/le/level |
Output log level. |
R/relative |
The time from program startup to log creation, in milliseconds. |
T/thread |
Name of the thread that outputs logs. |
Replace (P) {R, t} |
PIs the log content,RIs a regular expressionPMediumRTo replaceT. For example, "% replace (% msg) {'\ s ',''}" |
Format modifier, used together with the conversion OPERATOR:
The optional format modifier is located between "%" and the conversion operator.
The First Optional modifier isLeft alignedSign, which is the minus sign "-", followed by optionalMinimum WidthModifier, expressed in decimal number. If the character is smaller than the minimum width, it is left-filled or right-filled. The default value is left-filled (that is, right-aligned), and the padding is space. If the character is greater than the minimum width, the character will never be truncated.Max widthModifier. the symbol is the dot "." followed by a decimal number. If the character is greater than the maximum width, It is truncated from the front. Add the minus sign "-" after the dot sign "." and add a number to remove it from the tail.
For example, %-4 relative indicates that the output is left aligned from program startup to log creation time and the minimum width is 4.