“This is the 12th day of my participation in the August Text Challenge.
Take a look at the Spring Boot 2.x properties configuration file for details
Modify based on the above code
Logging is essentially the domain of the JDK
Logging is an important tool for debugging and analysis. Logback, Log4j2, and so on can be used as log providers.
JDK Logging
The Java standard library has a built-in logging package, java.util.logging.
Seven log levels are defined. The default level is INFO, ranging from critical to minor:
- SEVERE
- WARNING
- INFO
- CONFIG
- FINE
- FINER
- FINEST
Cons: Hard to use!
-
The Logging system reads and initializes the configuration file at JVM startup and cannot modify the configuration once the main() method is run
-
Configuration changes to the JVM starts passing parameters – Djava. Util. Logging. Config. The file = < config file – – the name >.
The default configuration file is lib/logging.properties in the JRE or conf/logging.properties in the JDK
-
If YOU want to generate a file, Need to modify the configuration files of configuration items to handlers = Java. Util. Logging. The FileHandler or handlers = Java. Util. Logging. FileHandler, java.util.logging.ConsoleHandler
Commons Logging
Commons Logging is a Logging module created by Apache that allows you to mount different Logging systems and specify which Logging system to mount through a configuration file. Commons Logging provides standard API interfaces that are implemented by Logging system providers.
Commons Logging automatically searches for and uses Log4j by default, and if Log4j is not found, JDK Logging is used. As a result, the Commons Logging module is widely used because of its automatic search mechanism and concise API interface.
Commons Logging defines six Logging levels: the default level is INFO.
- FATAL
- ERROR
- WARNING
- INFO
- DEBUG
- TRACE
There are only two steps to logging:
-
Get an instance of the Log class from LogFactory
-
Log using the Log instance method
public class Main {
public static void main(String[] args) {
Log log = LogFactory.getLog(Main.class);
log.info("start...");
log.warn("end."); }}Copy the code
What is the SLF4J
-
Not satisfied with the Commons Logging API, someone tried SLF4J
-
Not satisfied with Log4j’s performance, someone created Logback
Spring Boot uses Log4j2
Logging via Commons Logging requires only configuration and no code changes are required to use Log4j.
Log4j’s architecture is roughly as follows:
log.info("User signed in."); │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ├ ─ ─ > │ Appender │ ─ ─ ─ > │ Filter │ ─ ─ ─ > │ Layout │ ─ ─ ─ > │ Console │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ├ ─ ─ > │ Appender │ ─ ─ ─ > │ Filter │ ─ ─ ─ > │ Layout │ ─ ─ ─ > │ File │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ └ ─ ─ > │ Appender │ ─ ─ ─ > │ Filter │ ─ ─ ─ > │ Layout │ ─ ─ ─ > │ Socket │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code
When we output a log using Log4j, Log4j automatically outputs the same log to different destinations through different appenders. Such as:
- Console: Output to the screen
- File: Output to a file
- Socket: Output to a remote computer over the network
Pom. XML is introduced into log4j2
Since Spring Boot 2.x uses SLF4J as the logging API by default, you need to exclude the default automatic logging configuration and introduce Spring-boot-starter-log4j2
<! --spring log4j-->
<! -- > https://docs.spring.io/spring-boot/docs/1.5.19.RELEASE/reference/htmlsingle/#howto-configure-log4j-for-logging--
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<! -- Remove the default logging configuration of spring-boot-starter -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
Copy the code
Defining a configuration file
Create the log4j2-spring. XML configuration file in the resources root directory as follows:
<! --monitorInterval: Log4j2 automatically detects modified configuration files and reloads configurations, sets the number of seconds between monitorInterval
<Configuration monitorInterval="5">
<! -- Log levels and priorities: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL-->
<Properties>
<! Define log format -->
<Property name="log.pattern">%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{%-5level}[%thread] %style{%logger{36}}{cyan} : %msg%n</Property>
<! -- Define filename variables -->
<! -- Define log store path, do not configure relative path -->
<property name="FILE_PATH" value="logs/" />
<property name="FILE_NAME" value="log" />
</Properties>
<! Define Appender, the destination -->
<Appenders>
<! -- Define output to console -->
<Console name="console" target="SYSTEM_OUT">
<! -- Log format references log.pattern defined above -->
<PatternLayout pattern="${log.pattern}" disableAnsi="false" noConsoleNoAnsi="false"/>
<! -- The console only outputs messages of level and above (onMatch). The rest will be rejected (onMismatch) -->
<ThresholdFilter level="DEBUG" onMatch="ACCEPT" onMismatch="DENY"/>
</Console>
<! Every time the size of the log exceeds size, the size of the log will be automatically saved in the folder created by year and month and compressed, as an archive.
<RollingFile name="err" bufferedIO="true" fileName="${FILE_PATH}/error.log" filePattern="${FILE_PATH}/${FILE_NAME}-error-%d{yyyy-MM-dd}_%i.log.gz">
<PatternLayout pattern="${log.pattern}" disableAnsi="false" noConsoleNoAnsi="false"/>
<! -- Only output messages of level or higher (onMatch), reject others (onMismatch) -->
<ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>
<Policies>
<! --interval specifies how often to scroll. Default is 1 hour-->
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<! -- Automatically cut logs according to file size -->
<SizeBasedTriggeringPolicy size="20MB" />
</Policies>
<! -- Keep the last 20 copies -->
<DefaultRolloverStrategy max="20" />
</RollingFile>
</Appenders>
<Loggers>
<! -- Monitor system information -->
<! If additivity is set to false, the child Logger outputs only in its own appender and not in the parent Logger's appender. -->
<Logger name="org.springframework.web" level="info" additivity="false">
<AppenderRef ref="console"/>
</Logger>
<! -- Log Configuration -->
<Root level="info">
<! -- For info level logs, output to console -->
<AppenderRef ref="console" level="DEBUG" />
<! -- For error-level logs, output to err (RollingFile) -->
<AppenderRef ref="err" level="error" />
</Root>
</Loggers>
</Configuration>
Copy the code
The core of this is the configuration of spring logging:
<Logger name="org.springframework.web" level="info" additivity="false">
<AppenderRef ref="console"/>
</Logger>
Copy the code
The org.springFramework. web package outputs logs to the console at the info level.
Use the log
Using the Lombok plugin and annotating @log4j2 to log directly to log.xx internally, isn’t it super easy? The following is an example:
import lombok.extern.log4j.Log4j2;
@Log4j2
@Component
public class ShutDownHook
{
public void onApplicationClosed(a)
{
log.info("Background - System shut down"); . }}Copy the code
Reference Documents: Liao Xuefeng et al
Source code: github.com/langyastudi…