Import jar package
<dependency> <!-- introduce log4j2 rely on --> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency>
Add profile
1. If it is a user-defined file name, you must enter it in applicatio Configuration file name in yml file
logging: config: xxxx.xml level: cn.jay.repository: trace
2. Press the default file name log4j2 spring XML does not need to be configured in the configuration
Profile template
<?xml version="1.0" encoding="UTF-8"?> <!--Configuration hinder status,This is used to set log4j2 The internal information output can not be set when it is set to trace You'll see log4j2 Various internal detailed outputs--> <!--monitorInterval: Log4j It can automatically detect and modify the configuration file and reconfigure itself, and set the interval seconds--> <configuration monitorInterval="5"> <!--Log level and prioritization: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL --> <!--Variable configuration--> <Properties> <!-- Format output:%date Indicates the date,%thread Represents the thread name,%-5level: The level is displayed 5 characters wide from the left %msg: Log messages,%n Is a newline character--> <!-- %logger{36} express Logger The maximum length of the first name is 36 characters --> <property name="LOG_PATTERN" value="%date{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n" /> <!-- Define the path of log storage --> <property name="FILE_PATH" value="Change to your log path" /> <property name="FILE_NAME" value="Replace with your project name" /> </Properties> <appenders> <console name="Console" target="SYSTEM_OUT"> <!--Format of output log--> <PatternLayout pattern="${LOG_PATTERN}"/> <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)--> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> </console> <!--The file will print out all the information, this log The program will be emptied automatically every time it is run, and the append Attribute, suitable for temporary testing--> <File name="Filelog" fileName="${FILE_PATH}/test.log" append="false"> <PatternLayout pattern="${LOG_PATTERN}"/> </File> <!-- This will print out all the information info And below, each time the size exceeds size,Then this size Logs of size are automatically saved by year-The folder created in the month is compressed as an archive--> <RollingFile name="RollingFileInfo" fileName="${FILE_PATH}/info.log" filePattern="${FILE_PATH}/${FILE_NAME}-INFO-%d{yyyy-MM-dd}_%i.log.gz"> <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)--> <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="${LOG_PATTERN}"/> <Policies> <!--interval Property is used to specify how often to scroll. The default is 1 hour--> <TimeBasedTriggeringPolicy interval="1"/> <SizeBasedTriggeringPolicy size="10MB"/> </Policies> <!-- DefaultRolloverStrategy If the property is not set, the default is to start overwriting up to 7 files in the same folder--> <DefaultRolloverStrategy max="15"/> </RollingFile> <!-- This will print out all the information warn And below, each time the size exceeds size,Then this size Logs of size are automatically saved by year-The folder created in the month is compressed as an archive--> <RollingFile name="RollingFileWarn" fileName="${FILE_PATH}/warn.log" filePattern="${FILE_PATH}/${FILE_NAME}-WARN-%d{yyyy-MM-dd}_%i.log.gz"> <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)--> <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="${LOG_PATTERN}"/> <Policies> <!--interval Property is used to specify how often to scroll. The default is 1 hour--> <TimeBasedTriggeringPolicy interval="1"/> <SizeBasedTriggeringPolicy size="10MB"/> </Policies> <!-- DefaultRolloverStrategy If the property is not set, the default is to start overwriting up to 7 files in the same folder--> <DefaultRolloverStrategy max="15"/> </RollingFile> <!-- This will print out all the information error And below, each time the size exceeds size,Then this size Logs of size are automatically saved by year-The folder created in the month is compressed as an archive--> <RollingFile name="RollingFileError" fileName="${FILE_PATH}/error.log" filePattern="${FILE_PATH}/${FILE_NAME}-ERROR-%d{yyyy-MM-dd}_%i.log.gz"> <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)--> <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/> <PatternLayout pattern="${LOG_PATTERN}"/> <Policies> <!--interval Property is used to specify how often to scroll. The default is 1 hour--> <TimeBasedTriggeringPolicy interval="1"/> <SizeBasedTriggeringPolicy size="10MB"/> </Policies> <!-- DefaultRolloverStrategy If the property is not set, the default is to start overwriting up to 7 files in the same folder--> <DefaultRolloverStrategy max="15"/> </RollingFile> </appenders> <!--Logger Node is used to specify the form of log separately, for example, for the log under the specified package class Specify different log levels, etc.--> <!--Then define loggers,Only defined logger And introduced appender,appender Will take effect--> <loggers> <!--Filter out spring and mybatis Some useless DEBUG information--> <logger name="org.mybatis" level="info" additivity="false"> <AppenderRef ref="Console"/> </logger> <!--Monitoring system information--> <!--If additivity Set as false,Zezi Logger Only in their own appender Output in, not in the parent Logger of appender Output in.--> <Logger name="org.springframework" level="info" additivity="false"> <AppenderRef ref="Console"/> </Logger> <root level="info"> <appender-ref ref="Console"/> <appender-ref ref="Filelog"/> <appender-ref ref="RollingFileInfo"/> <appender-ref ref="RollingFileWarn"/> <appender-ref ref="RollingFileError"/> </root> </loggers> </configuration>
Configuration details
- log level
Mechanism: if the level of a log information is greater than or equal to the level of the configuration file, it will be recorded.
- Trace: trace means that the program advances and can write trace output
- debug: debugging. Generally, it is the lowest level, and trace is basically not used.
- info: output important information and use it more
- warn: warning, some messages are not error messages, but you should also give some hints to the programmer.
- Error: error message. Also used a lot.
- Fatal: fatal error.
- output source
- CONSOLE (output to CONSOLE)
- FILE (output to FILE)
- format
- SimpleLayout: displays in a simple form
- HTMLLayout: display in HTML table
- PatternLayout: custom form display
PatternLayout custom log layout:
%d{yyyy-MM-dd HH:mm:ss, SSS} : Log production time,Time to output in milliseconds %-5level : Output log level,-5 Indicates left alignment and fixed output of 5 characters. If it is insufficient, fill 0 on the right %c : logger Name of(%logger) %t : Output current thread name %p : Log output format %m : Log content, i.e logger.info("message") %n : Newline character %C : Java Class name(%F) %L : Line number %M : Method name %l : The number of lines in which the output statement is located, Including class name, method name, file name and number of lines hostName : Local machine name hostAddress : local ip address
- Root node Configuration
There are two properties:
- status
- monitorinterval
There are two child nodes:
- Appenders
- Loggers (indicates that multiple Appender s and loggers can be defined)
status is used to specify the print log level of log4j itself
monitorinterval is used to specify the monitoring interval for log4j automatic reconfiguration. The unit is s and the minimum is 5s
- Appenders node
There are three common sub nodes: Console, RollingFile and File
The Console node is used to define the Appender that is output to the Console
- Name: Specifies the name of the Appender
- target:SYSTEM_OUT or SYSTEM_ERR, generally only set the default: SYSTEM_OUT.
- PatternLayout: output format, not set, default:% m%n
The File node is used to define the Appender of the File exported to the specified location
- Name: Specifies the name of the Appender
- fileName: Specifies the destination file of the output log and the file name with full path
- PatternLayout: output format, not set, default:% m%n
The RollingFile node is used to define that if the specified conditions are exceeded, the old will be automatically deleted and a new Appender will be created
- Name: Specifies the name of the Appender
- fileName: Specifies the destination file of the output log and the file name with full path
- PatternLayout: output format, not set, default:% m%n
- filePattern: Specifies the rules for transferring and renaming files when Rolling occurs
- Policies: Specifies the policy of rolling logs, that is, when to create new log files and output logs
- Timebasedtriggingpolicy: a child node of policies. It is a time-based scrolling policy. The interval attribute is used to specify how often to scroll. The default is 1 hour. modulate=true is used to adjust the time: for example, if it is 3am in the morning and the interval is 4, then the first scroll is 4am, then 8am, 12am Not 7am
- Sizebasedtriggingpolicy: the policies sub node, which is based on the rolling policy of the specified file size. The size attribute is used to define the size of each log file
- DefaultRolloverStrategy: used to specify that when there are at most several log files in the same folder, the oldest one will be deleted and a new one will be created (through the max attribute).
There are two common Loggers nodes: Root and Logger
The Root node is used to specify the Root log of the project. If no Logger is specified separately, the Root log output will be used by default
- Level: log output level. There are 8 levels in total. From low to high, it is the child node of all < trace < debug < info < warn < error < appenderref: Root, which is used to specify the Appender to which the log is output
- The Logger node is used to specify the log form separately, for example, to specify different log levels for class es under the specified package.
- Level: log output level, 8 levels in total, from low to high: all < trace < debug < info < warn < error < fatal < off
- name: used to specify the class to which the Logger applies or the full path of the package where the class is located, inherited from the Root node
- AppenderRef: the child node of Logger, which is used to specify the Appender to which the log is output. If it is not specified, it will inherit from Root by default If it is specified, it will be output in both the specified Appender and the Appender of the Root. At this time, we can set the additivity="false" of the Logger to output only in the customized Appender.