Default logging configuration for SpringBoot

Right out of the box, SpringBoot configures the Logging framework for you by default to use Commons Logging, but the default configuration also provides support for common Logging such as Java Util Logging, Log4J, Log4J2 and Logback. Each Logger can be configured to output log content using either a console or a file.

Console output

The five log levels are ERROR, WARN, INFO, DEBUG, and TRACE. The log levels are higher than those of the preceding levels. For example, WARN, INFO, and DEBUG messages will not be output if the level is set to ERROR. Log output to the console at the ERROR, WARN, and INFO levels is configured by default in SpringBoot. There is no FATAL level in Logback, which is treated as ERROR.

We can switch to DEBUG level in two ways:

  • Add the –debug flag after running the command, such as $Java -jar myapp.jar –debug
  • inapplication.propertiesSet debug=true to true. Core Loggers (including embedded containers, Hibernate, and Spring) will output more, but your own application logs will not output debug.

Colorful output

SpringBoot supports color logging since version 1.4.0. If your terminal supports ANSI, setting color output will make the log more readable. Through the application. The properties set in the spring. The output. The ANSI. Enabled parameters to support.

  • NEVER: Disable anSI-colored output (default)
  • DETECT: Will check if the terminal supports ANSI, if so use color output (recommended)
  • ALWAYS: Always use anSI-colored output. If the terminal does not support this format, interference may occur. Do not use this format

The output file

The default SpringBoot configuration is only printed to the console and is not recorded in a file, but is usually recorded in a file when used in production environments.

To increase file output, you need to configure the logging.file or logging.path property in application.properties.

  • logging.file: Sets the file, which can be an absolute path or a relative path. Such as: logging. The file = my. Log
  • logging.path: If you set the directory, the spring.log file will be created in this directory and log content will be written, for example, logging.path=/var/log

Log files are truncated at 10Mb and new log files are generated. The default levels are ERROR, WARN, and INFO

Level control

In SpringBoot, you only need to configure the logging level control in application.properties.

Configuration format: logging.level.*= level

  • logging.level: Log level control prefix, * is the package name or Logger name
  • LEVEL: Options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF

For example:

  • Logging.level.com.controller=DEBUG: com. The controller all the class in order to packageDEBUGLevel of output
  • Logging.level. root=WARN: root logs are loggedWARNLevel of output

Custom log configuration

If you don’t want to use the default configuration, just add your own log configuration file. Since the logging service is typically initialized before the ApplicationContext is created, it does not have to be controlled through Spring’s configuration file. Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.

Depending on the logging system, you can organize the configuration file names as follows to get them loaded correctly:

  • Logback: logback-spring. XML, logback-spring.groovy, logback. XML, logback.groovy
  • Properties, log4j-spring. XML, log4j.properties, log4j.xml
  • Log4j2: log4j2-spring.xml , log4j2.xml
  • JDK (Java Util Logging) : logging.properties

SpringBoot officially recommends using -spring filenames for your log configuration (e.g., using logback-spring. XML instead of logback.xml).

Customize the output format

In SpringBoot, you can control the output format by setting the following parameters in application.properties:

  • logging.pattern.console: Define output styles to the console (JDK Logger not supported)
  • logging.pattern.file: Define output styles to files (JDK Logger not supported)

You can also define these formats directly in the log configuration file, rather than in the application configuration file.

Common Logback configuration file template

<?xml version="1.0" encoding="UTF-8"? >
<configuration>
	<! Do not use relative path in LogBack configuration -->
    <property name="LOG_HOME" value="d:/logs"/>

    <! -- Color log dependent render class -->
    <conversionRule conversionWord="clr" converterClass="org.springframework.boot.logging.logback.ColorConverter"/>
    <conversionRule conversionWord="wex"
                    converterClass="org.springframework.boot.logging.logback.WhitespaceThrowableProxyConverter"/>
    <conversionRule conversionWord="wEx"
                    converterClass="org.springframework.boot.logging.logback.ExtendedWhitespaceThrowableProxyConverter"/>
    <! -- Color log format -->
    <property name="CONSOLE_LOG_PATTERN"
              value="${CONSOLE_LOG_PATTERN:-%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- CLR (-) {}) magenta} % {abbreviation} % CLR ([% 15.15 t]) {abbreviation} % CLR (% 40.40 logger {39}) CLR (:) {abbreviation} {cyan} % %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}}"/>
    <! -- Console output Settings -->
	<appender name="console" class="ch.qos.logback.core.ConsoleAppender">
		<encoder>
			<! Format output: %d indicates the date, %thread indicates the thread name, %-5level indicates the level of 5 characters from the left. % MSG indicates the log message, %n indicates the newline character.
			<! --<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>-->
			<pattern>${CONSOLE_LOG_PATTERN}</pattern>
			<charset>utf8</charset>
		</encoder>
	</appender>

    <! Generate log files per day -->
    <appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <! -- Log file output file name -->
            <fileNamePattern>${LOG_HOME}/mixedSys.%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>

    <! -- Log Logger (package. Class) -->
	<logger name="org.springframework.web.servlet.mvc.method.annotation.ExceptionHandlerExceptionResolver" level="DEBUG" >
		<appender-ref ref="console" />
		<appender-ref ref="file" />
	</logger>

    <logger name="org.springframework.boot" level="DEBUG"/>
    <! Hibernate SQL
    <! -- <logger name="org.hibernate.type.descriptor.sql.BasicBinder" level="TRACE" /> <logger name="org.hibernate.type.descriptor.sql.BasicExtractor" level="DEBUG" /> <logger name="org.hibernate.SQL" level="DEBUG" /> <logger name="org.hibernate.engine.QueryParameters" level="DEBUG" /> <logger name="org.hibernate.engine.query.HQLQueryPlan" level="DEBUG" /> -->

    <! -- Log output level -->
    <root level="INFO"> <! -- Debug messages will be filtered -->  
        <appender-ref ref="console" />   
        <appender-ref ref="file" />   
    </root> 
    
	<! -- Log asynchronous to database -->    
    <! - < appender name = "DB" class = "ch. Qos. Logback. Classic. DB. DBAppender" > asynchronous to the database log < connectionSource Class = "ch. Qos. Logback. Core. The DriverManagerConnectionSource" < > connection pool dataSource class="com.mchange.v2.c3p0.ComboPooledDataSource"> <driverClass>com.mysql.jdbc.Driver</driverClass> < url > JDBC: mysql: / / 127.0.0.1:3306 / databaseName < / url > < user > root < / user > < password > root < / password > < / a dataSource > </connectionSource> </appender> -->
	
</configuration>
Copy the code