Log4j2 is used as the logging framework. There are many others, such as Logback, Log4j, and so on.
First exclude the logback dependency package in Spring Boot
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <! Exclude this default logging component, must have it!! --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency>Copy the code
Then add the log4j2 dependency package:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> <! -- Add this to identify itlog4 j2. Yml file -- > < the dependency > < groupId > com. Fasterxml. Jackson. Dataformat < / groupId > <artifactId>jackson-dataformat-yaml</artifactId> </dependency>Copy the code
Add log4j2. XML or log4j2-spring. XML to resources. Spring Boot automatically loads the file after startup, without specifying the configuration file in application.
I started with configuration files in XML format, and then on a whim I switched to YML files… Post both of them
Log4j2.xml: This configuration is relatively simple
<? xml version="1.0" encoding="utf-8"? > <configuration> <properties> <! -- File output format --> <property name="PATTERN">%d{yyyy-MM-dd HH:mm:ss.SSS} |-%-5level [%thread] %c [%L] -| %msg%n</property>
</properties>
<appenders>
<Console name="CONSOLE" target="system_out">
<PatternLayout pattern="${PATTERN}" />
</Console>
</appenders>
<loggers>
<logger name="com.roncoo.education" level="debug" />
<root level="info">
<appenderref ref="CONSOLE" />
</root>
</loggers>
</configuration>
Copy the code
Log4j2. Yml:
Configuration:
status: warn
Properties: Define global variables
Property: Default configuration (for the development environment). Other environments need to be specified in VM parameters as follows:
Console =warn - dlog.level. XJJ =trace
Console =warn - dlog.level. XJJ =info
- name: log.level.console
value: debug
- name: log.level.xjj
value: info
- name: log.path
value: D:/log
- name: project.name
value: springboot1
Appenders:
Console: Output to the console
name: CONSOLE
target: SYSTEM_OUT
ThresholdFilter:
level: ${sys:log.level.console} # "sys:" means: if this variable value is not specified in the VM parameter, the default global variable value defined in this file is used
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"
RollingFile: # Output to file, over 100MB archive
- name: ROLLING_FILE
ignoreExceptions: false
fileName: ${log.path}/${project.name}.log
filePattern: "${log.path}/ $${date:yyyy-MM}/${project.name}-%d{yyyy-MM-dd}-%i.log.gz"
PatternLayout:
pattern: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"
Policies:
SizeBasedTriggeringPolicy:
size: "100 MB"
DefaultRolloverStrategy:
max: 1000
Loggers:
Root:
level: info
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
Logger: Configure a special Log level for org.spring Framework packages to facilitate debugging
- name: org.springframework
additivity: false
level: ${sys:log.level.xjj}
AppenderRef:
- ref: CONSOLE
- ref: ROLLING_FILE
Copy the code
The Configuration contains INFO and the Root contains debug. The status of the Configuration is the log printing level of log4j2, not the global log level. The leVE of Root is at the global log level.
Under Appenders, the place where the log output is defined. From the above configuration, the output is to the console.
So log4j2 is configured successfully. Restart the project to see what happens
private static Logger logger = LoggerFactory.getLogger(Springboot1Application.class);
public static void main(String[] args) {
SpringApplication.run(Springboot1Application.class, args);
logger.info("Project started successfully! ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~");
}
Copy the code
The springBoot1. log file is also generated in the D:\log directory.
The 2018-08-22 12:01:59. 865 INFO org. Apache. Juli. Logging. DirectJDKLog 180log - Starting ProtocolHandler ["http-nio-8082"] the 2018-08-22 12:01:59. 880 INFO org. Apache. Juli. Logging. DirectJDKLog 180log - Using a shared selector for servlet write/readThe 2018-08-22 12:01:59. 900 INFO org. Springframework. Boot. Web. Embedded. Tomcat. TomcatWebServer 206 start - tomcat started on port(s): 8082 (http) with context path' 'The 2018-08-22 12:01:59. 906 INFO org. Springframework. Boot. StartupInfoLogger 59logStarted - Started Springboot1Application in15.4 seconds (JVM is runningfor17.972) the 2018-08-22 12:01:59. 911 INFO com. 18 main Springboot1Application - project start-up success! ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~Copy the code
Above… Just a simple configuration, the following detailed configuration in depth.