Spring Boot learning notes (2) integrate log4j2

1. Log4j2 profile

Good log output can quickly locate problems when they occur. So let’s integrate log4j first.

We use log4j2, which is basically the same as log4j. The big difference is that log4j2 no longer supports properties configuration files

2. Add log4j2 dependencies

Add dependencies to POM files:

1 <! Logging out of the parent project Log4j2 --> 2<dependency> 3 <groupId>org.springframework.boot</groupId> 4 <artifactId>spring-boot-starter</artifactId> 5 <exclusions> 6 <exclusion> 7 <groupId>org.springframework.boot</groupId> 8 <artifactId>spring-boot-starter-logging</artifactId> 9 </exclusion> 10 </exclusions> 11</dependency> 12<! Log4j2 <dependency> 14 <groupId>org.springframework.boot</groupId> 15 <artifactId>spring-boot-starter-log4j2</artifactId> 16</dependency> 17 18<dependency> 19 <groupId>org.slf4j</groupId> 20 <artifactId> log4J-over-slf4j </artifactId> 21 <version>1.7.24</version> 22 < Exclusions > 23 < Exclusion > 24 <artifactId>slf4j-api</artifactId> 25 <groupId>org.slf4j</groupId> 26 </exclusion> 27 </exclusions> 28</dependency>Copy the code

3. Add the log4j2 configuration file

1 <? The XML version = "1.0" encoding = "utf-8"? 2 > <! -- 3 status: This parameter is used to set the internal output of log4j2. Do not set this parameter. 4 If this parameter is set to trace, detailed output of log4j2 will be displayed. Log4j is able to automatically detect changes to configuration files and reconfiguration itself, with 6 set intervals of seconds. Re-read the Configuration file every 600 seconds 7--> 8<Configuration status="OFF" monitorInterval="600"> 9 10 <! -- Log level: TRACE < DEBUG < INFO < WARN < ERROR < FATAL- > 11 <! If set to WARN, messages below WARN will not be printed --> 12 <Properties> 13 <! > 14 <Property name="LOG_HOME">D:\sysLogs\learning > 15 <Property name="FILE_NAME">learningSpringBoot</property> 16 </Properties> 17 18 <Appenders> 19 <! 20 <Console name="Console" target="SYSTEM_OUT"> 21 <! -- The console outputs only messages of level and above (onMatch), --> 23 <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/> 24 <! -- Log output format --> 25 <! --%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n--> 26 27 <PatternLayout pattern="|%d{yyyy-MM-dd HH:mm:ss.SSS}|%5p|%5t|%4c:%L|%m%n"/> 28 </Console> 29 30 <RollingRandomAccessFile name="LEARNING" fileName="${LOG_HOME}/${FILE_NAME}.log" 31 filePattern="${LOG_HOME}/backup/${FILE_NAME}_%d{yyyy-MM-dd}_%i.log"> 32 <! --%d{yyyy-MM-dd 'at' HH:mm:ss z} %-5level %class{36} %L %M - %msg%xEx%n--> 33 <PatternLayout pattern="|%d{yyyy-MM-dd HH:mm:ss.SSS}|%5p|%5t|%4c:%L|%m%n"/> 34 <Policies> 35 <TimeBasedTriggeringPolicy interval="1"/> 36 <SizeBasedTriggeringPolicy size="20MB"/> 37 </Policies> 38 <DefaultRolloverStrategy max="10"/> 39 </RollingRandomAccessFile> 40 </Appenders> 41 42 <! After defining logger and introducing an appender, Loggers> 44 <Logger name="com.zdran.springboot" level="INFO" additivity="true"> 45 <AppenderRef ref="LEARNING"/> 46 </Logger> 47 <Logger name="org.springframework" level="INFO" additivity="true"> 48 <AppenderRef ref="LEARNING"/> 49 </Logger> 50 <Logger name="org.apache" level="INFO" additivity="true"> 51 <AppenderRef ref="LEARNING"/> 52 </Logger> 53 <Root level="INFO"> 54 <Appender-Ref ref="Console"/> 55 </Root> 56 </Loggers> 57 58</Configuration>Copy the code

Such a detailed note does not need to explain it…

D:\sysLogs\learning There is another setting, we can configure a placeholder in the XML, and then specify the log directory in the form of the number of arguments when the command line starts. It can be configured as follows:

1<Property name="LOG_HOME">${sys:log4j.path}/learning</Property> 2 3 Start the command Java -jar xxx.jar -log4j.path=D:\sysLogs\learningCopy the code

If you use this configuration it will be reported at startup

1ERROR StatusLogger Cannot access RandomAccessFile Java.io.IOException: The file name, directory name, or volume label method is incorrect. 2 java.io.IOException: The file name, directory name, or volume label is incorrect.Copy the code

This error is caused by placeholders that are not valid paths and can be configured as follows:










-Dlog4j.path=D:\sysLogs









Link to this article: zdran.com/20180629.ht…