Tutorial on using the SpringBoot series log framework

Based on the previous blog, An introduction to the log framework of the SpringBoot series and its principles After the blog, this blog can help you learn about Springboot by providing a more detailed description of the specific use of the blog framework as a usage manual

@[toc]

1. SpringBoot Log Level

1) Introduction to Log Level

Briefly introduce the log level, sorted from low to high: trace < debug < info < warn < error, eg: If the log level is info, only info level and its high level logs will be printed, so in a project, you can lower the log level by increasing the log level, making fewer logs, or vice versa, if you want to make more logs

2), Default log level

The log levels Springboot supports are TRACE, DEBUG, INFO, WARN, ERROR, FATAL, or OFF, then the default level is info, write a simple test class to verify the problem:

import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringbootLoggerApplicationTests {

	Logger LOG = LoggerFactory.getLogger(this.getClass());

	@Test
	void contextLoads() {
		LOG.trace("this is trace log infomation....");
		LOG.debug("this is debug log infomation....");
		LOG.info("this is info log infomation....");
		LOG.warn("this is warn log infomation....");
		LOG.error("this is error log infomation....");
	}

}

Log Printing:

2019-11-16 18:50:07.513  INFO 19152 --- [           main] c.e.s.s.SpringbootLoggerApplicationTests : this is info log infomation....
2019-11-16 18:50:07.513  WARN 19152 --- [           main] c.e.s.s.SpringbootLoggerApplicationTests : this is warn log infomation....
2019-11-16 18:50:07.513 ERROR 19152 --- [           main] c.e.s.s.SpringbootLoggerApplicationTests : this is error log infomation....

You can see that only info and its high-level logs are printed, but we have not configured anything to indicate that Springboot has been automatically configured and the default log level is info's

3) Configure Log Level

The Springboot log level can set either the root level or the log level below the corresponding package, as shown in the following example:

# root log level is info
logging.level.root=info
# Specify the org.springframework.web package level as debug
logging.level.org.springframework.web=debug
# Specify the org.hibernate package level as error
logging.level.org.hibernate=error
# Specify the uniform debug level below the com.example.springboot.springbootlogger package
logging.level.com.example.springboot.springbootlogger=debug

4) Log grouping settings

Springboot also provides log-level grouping, which is officially said to be "it's often useful to group related loggers together so that they can be configured at the same time."For example, you can often change the logging level for all Tomcat-related loggers, but you can't easily remember the top-level packages."This means that we can define a group by ourselves and then set the level of the entire group by ourselves, mainly because we have difficulty remembering those package names

# Log Grouping Settings
logging.group.tomcat=org.apache.catalina, org.apache.coyote, org.apache.tomcat
# Set the level of the entire group to trace
logging.level.tomcat=TRACE

Springboot already has groups, no groups have to set up themselves |Name| loggers | |--|--| | web | org.springframework.core.codec, org.springframework.http, org.springframework.web, org.springframework.boot.actuate.endpoint.web, org.springframework.boot.web.servlet.ServletContextInitializerBeans| |sql |org.springframework.jdbc.core, org.hibernate.SQL, org.jooq.tools.LoggerListener|

2. SpringBoot Log Formatting

1) Introduction to Default Formatting Principles

As you know from previous studies, Springboot uses logbacks by default for log printing, so you can find default.xml in the jar package of Springboot under the package of the default log format configuration, org.springframework.book.logging.logback, that is, we load the default format if we are not configuring application.properties

2) Default log format

2019-03-05 10:57:51.112  INFO 45469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2019-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2019-03-05 10:57:51.253  INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 1358 ms
2019-03-05 10:57:51.698  INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean        : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-05 10:57:51.702  INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean  : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
  • Date and time: Millisecond precision and easy sorting.
  • Log level: error, warning, information, debugging, or tracing.
  • Process ID.
  • A separator used to distinguish the beginning of an actual log message.
  • Thread name: enclosed in square brackets (console output may be truncated).
  • Logger name: This is usually the source class name (usually abbreviated).
  • Log message.

3) Custom log format

application.perperties can also add their own custom configurations, and this blog uses boot2.2.1

# Define console log print format
logging.pattern.console=%d{yyyy-MM-dd} {%thread} %-5level %logger{50} - %msg%n
# Define archive log file log format
logging.pattern.file=%d{yyyy-MM-dd} {%thread} %-5level %logger{50} - %msg%n

Note that the log format is described as follows:

  • %d: date;
  • %thread: Thread name;
  • %-5level: The log level shows five characters from the left, such as DEBUG;
  • %logger{50}: java class name, for example: com.muses.taoshop.MyTest, where 50 represents the character length;
  • %msg: log content;%n: line break

4) Log color settings

For logs printed in the console, you can also add the necessary colors, which are used to turn on spring.output.ansi.enabled, and the ANSI configuration can refer to the official documentation: https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/api//org/springframework/boot/ansi/AnsiOutput.Enabled.html

In fact, the main attributes are as follows:

  • ALWAYS: Enable ANSI color output.
  • DETECT: Try to detect if ANSI shading is available.
  • NEVER: Disable ANSI color output.

application.properties open

spring.output.ansi.enabled=always

The specific usage is%clr(${param}){color}

logging.pattern.console=%clr(%d{yyyy-MM-dd HH24:mm:ss.SSS}){green} {%thread} %-5level %logger{50} - %msg%n

Springboot supports colors such as:

  • blue
  • cyan
  • faint
  • green
  • magenta
  • red
  • yellow

3. Log file archiving

For log archive files, we can also see from the source that the default configuration is 10M, that is, files will be automatically archived after 10M, we do not need to configure ourselves

You can also add a default configuration to application.properties

# You can specify a directory or not, and create a log file under the project root directory if not specified
logging.file.name=springboot.log
# The window s system creates the springboot/log folder at the disk root of the current project. The default log file is spring.log
#logging.file.path=/springboot/log

Log.file.name=springboot.log, you can specify a directory or not, create a log file under the project root directory if not specified Log.file.path=/springboot/log, the window s system creates the springboot/log folder at the disk root of the current project. The default log file is spring.log

4. Configuration of other logging parameters

For how Springboot configures log parameters, you can refer specifically to the Springboot official website, refer to the logging configuration of the official website, the official website link: https://docs.spring.io/spring-boot/docs/2.2.1.RELEASE/reference/html/spring-boot-features.html#boot-features-logging

5. Turn on log debugging mode

When debugging mode is enabled, a series of core loggers (embedded container, Hibernate, and Spring Boot) are configured to output more information.Enabling debugging mode does not configure the application to use the DEBUG level to log all messages.

$ java -jar myapp.jar --debug

You can also open it in the configuration file library, setting debug to TRUE will do

debug=true

ok, this blog is just about introducing the basic use of log framework, such as custom logback configuration, etc. Please refer to the series of blogs in this column: https://smilenicky.blog.csdn.net/category_9195353.html

Appendix: logging manual: Official SpringBoot Journal Manual example source: Log configuration github code download link

Keywords: Programming SpringBoot Spring Tomcat Apache

Added by busnut on Tue, 21 Apr 2020 20:02:24 +0300