Spring Boot Logging framework

Spring Boot supports Java Util Logging,Log4j2,Lockback as the Logging framework, and if you use the Starters launcher, Spring Boot will use Logback as the default Logging framework. Regardless of which logging framework you use, Spring Boot supports configuration to output logs to the console or to a file.

The spring-boot-starter launcher contains the spring-boot-starter-logging launcher and integrates slF4J log abstraction and Logback logging framework.

Property Configuration Log

Spring Boot supports attribute configuration logging parameters, which are not very flexible and will not be detailed.

Reference configuration:

# LOGGING logging.config= # Location of the logging configuration file. For instance `classpath:logback.xml` for Logback  logging.exception-conversion-word=%wEx # Conversion word used when logging exceptions. logging.file= # Log file name. For instance `myapp.log` logging.level.*= # Log levels severity mapping. For instance `logging.level.org.springframework=DEBUG` logging.path= # Location of the log file. For instance `/var/log` logging.pattern.console= # Appender pattern for output to the console. Only supported with the default logback setup. logging.pattern.file= # Appender pattern for output to the file. Only supported with the default logback setup. logging.pattern.level= # Appender pattern for log level (default %5p). Only supported with the default logback setup. logging.register-shutdown-hook=false # Register a shutdown hook for the logging system when it is initialized.Copy the code

Such as:

logging.level.root=DEBUG
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
Copy the code

Customize log files

Based on different logging frameworks, the file name of the default loaded log configuration file is stored in the root directory of the resource. Other directories and file names cannot be loaded.

Logging System Customization
Logback logback-spring.xml, logback-spring.groovy, logback.xml or logback.groovy
Log4j2 log4j2-spring.xml or log4j2.xml
JDK (Java Util Logging) logging.properties

Since the Logback framework comes with you by default, Logback is also the best logging framework. Go to the resources directory and create logback-spring. XML.

<? The XML version = "1.0" encoding = "utf-8"? > <configuration debug="false"> <springProperty scope="context" name="APP_NAME" source="spring.application.name"/> <springProperty scope="context" name="APP_PORT" source="server.port"/> <springProperty scope="context" name="DEFAULT_APP_PORT" source="spring.application.port"/> <property name="OS_NAME" value="${os.name}"/> <if condition='property("OS_NAME").contains("Windows")'> <then> <property name="LOG_PATH" value="${LOG_PATH:-E:/logs}" /> </then> <else> <property name="LOG_PATH" value="${LOG_PATH:-/log}" /> </else> </if> <property name="LOG_DIR" value="${APP_NAME:-system}" /> <property name="APP_PORT" value="${APP_PORT:-${DEFAULT_APP_PORT:-0}}" /> <if condition='! property("APP_PORT").equals("0")'> <then> <property name="LOG_DIR" value="${LOG_DIR}-${APP_PORT}" /> </then> </if> <! - the console output - > < appender name = "STDOUT" class = "ch. Qos. Logback. Core. ConsoleAppender" > < encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! -- Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level of 5 characters from the left, % MSG: Log messages, SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> </encoder> </appender> <! Generated according to the daily log FILE - > < appender name = "FILE" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > < filter class="ch.qos.logback.classic.filter.ThresholdFilter"> <level>INFO</level> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! ${LOG_PATH}/${LOG_DIR}/info.log.%d{YYYY-MM-DD}. Log </FileNamePattern> <! -- Log file retention days --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: -> <pattern>%d{YYYY-MM-DD HH: MM :ss.SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> </ coder> <! -- the largest size in the log file -- > < triggeringPolicy class = "ch. Qos. Logback. Core. Rolling. SizeBasedTriggeringPolicy" > <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <! Generated according to the daily log FILE error level - > < appender name = "FILE - error" class = "ch. Qos. Logback. Core. Rolling. RollingFileAppender" > < filter class="ch.qos.logback.classic.filter.LevelFilter"> <level>ERROR</level> <onMatch>ACCEPT</onMatch> <onMismatch>DENY</onMismatch> </filter> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <! ${LOG_PATH}/${LOG_DIR}/error.log.%d{YYYY-MM-DD}. Log </FileNamePattern> <! -- Log file retention days --> <MaxHistory>30</MaxHistory> </rollingPolicy> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level from the left 5 character width % MSG: -> <pattern>%d{YYYY-MM-DD HH: MM :ss.SSS} [%thread] %-5level % Logger {50} - % MSG %n</pattern> </ coder> <! -- the largest size in the log file -- > < triggeringPolicy class = "ch. Qos. Logback. Core. Rolling. SizeBasedTriggeringPolicy" > <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> <! --myibatis log configure --> <logger name="com.apache.ibatis" level="TRACE" /> <logger name="java.sql.Connection" level="DEBUG" /> <logger name="java.sql.Statement" level="DEBUG" /> <logger name="java.sql.PreparedStatement" level="DEBUG" /> <! -- Log output level --> <root level="INFO"> <appender-ref ref="STDOUT" /> <appender-ref ref="FILE" /> <appender-ref ref="FILE-ERROR" /> </root> </configuration>Copy the code

It is highly recommended to use logback-spring. XML as the file name because logback.xml is loaded too early.

The log is initialized before the ApplicationContext is created, so the configuration loaded by @propertysources cannot be read. System Environment variables, Spring Environment and application,bootstrap configuration files can be read.

Read system environment properties:

<property name="LOG_PATH" value="${LOG_PATH:-E:/logs}" />
Copy the code

Read a property in the current application Environment:

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host"
        defaultValue="localhost"/>
Copy the code

Spring Boot also supports loading configurations under different profiles through Spring Profiles.

<springProfile name="staging"> <! -- configuration to be enabled when the "staging" profile is active --> </springProfile> <springProfile name="dev, staging"> <! -- configuration to be enabled when the "dev" or "staging" profiles are active --> </springProfile> <springProfile name="! production"> <! -- configuration to be enabled when the "production" profile is not active --> </springProfile>Copy the code

Recommended reading

Dry goods: 2TB architect four-stage video tutorial

Interview: the most complete Java multithreaded interview questions and answers

Interview: the most comprehensive ali advanced Java interview questions in history

Interview: The most complete Spring interview questions in history

Tutorial: The most complete Spring Boot complete video tutorial

Books: 15 must-read books for advanced Java architects

Tools: Recommended an online creation flow chart, mind mapping software

Share Java dry goods, high concurrency programming, hot technology tutorials, microservices and distributed technology, architecture design, blockchain technology, artificial intelligence, big data, Java interview questions, and cutting-edge hot news.