Spring Boot uses Commons Logging for all internal Logging, but the implementation of the underlying Logging is open. In the Spring Boot ecosystem, there are automated configuration components for common Logging frameworks such as Java Util Logging, Log4J2, and Logback. Each Logger can be configured to output log content to the console or to a file. By default, Logback is used for log management when using various Starter.

How to keep a Journal

There are many ways to write logs in the introduction, here will not be a variety of ways to do a list, just talk about DD the most used way!

First, at the code level, we don’t have to decide whether to use the default Logback or Log4j, but just Slf4j.

Why not write code using Logback or Log4j? This is the benefit of using Slf4j. Why Slf4j? Simple Logging Facade for Java is not a Logging solution, but a Logging framework like Logback or Log4j. Slf4j is a typical application of the facade pattern among the 23 design patterns. With Slf4j as a facade abstraction, we can only rely on this abstract logging operation after we write the code, and the concrete implementation will be delegated to the concrete implementation when the Slf4j facade is invoked.

For example, here is a simple logging example:

@Slf4j
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);

        log.error("Hello World");
        log.warn("Hello World");
        log.info("Hello World");
        log.debug("Hello World");
        log.trace("Hello World"); }}Copy the code

Note: Here we introduce Lombok in pom.xml, then use the @slf4J declaration to introduce Slf4j’s log logging object, which can then be easily used for logging. How this log is written to the console or to a file depends on the specific logging framework introduced in the Spring Boot project, which is Logback by default.

Log elements

Starting any Spring Boot project, we can see many logs on the console, such as the following log:

There are seven elements in the log output as follows:

  1. Time and date: accurate to the millisecond
  2. Log level: ERROR, WARN, INFO, DEBUG or TRACE
  3. The process ID
  4. The separator:---Identifies the start of the actual log
  5. Thread name: enclosed in square brackets (may truncate console output)
  6. Logger name: The name of the class that normally uses the source code
  7. Log contents

Log output

In a Spring Boot application, logs are output to the console by default. The default output log levels are ERROR, WARN, and INFO.

The 2021-12-28 17:37:25. 65136-578 the INFO [main] com. Didispace. Chapter81. Application: Started Application in 2.695 seconds (JVM running for 3.957) 2021-12-28 17:37:25.579 ERROR 65136 - [main] com.didispace.chapter81.Application : Hello World 2021-12-28 17:37:25. 579 WARN 65136 - [the main] com. Didispace. Chapter81. Application: Hello World 2021-12-28 17:37:25. 579 INFO 65136 - [the main] com. Didispace. Chapter81. Application: Hello WorldCopy the code

Enabling DEBUG Logs

We can switch to DEBUG level in two ways:

$Java -jar myapp.jar –debug

Second: set debug=true in the application. Properties configuration file

The DEBUG log enabled here only affects core Loggers. The embedded container, Hibernate, Spring, and other framework logs will output more content, but your own application logs will not be output at DEBUG level. The Debug level Hello World we wrote ourselves has no output.

The log configuration

The following describes some common log configurations to help you better manage log content.

Colorful output

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 to support parameters, the parameter has three options:

  • NEVER: Disables anSI-colored output
  • DETECT: Checks whether the terminal supports ANSI, if so color output is used (default)
  • ALWAYS: ALWAYS use anSI-colored. If the TERMINAL does not support this parameter, interference information will be generated. Therefore, do not use this parameter

Note: The default is NEVER when Spring Boot 1.x and DETECT after 2.x, so if you look at our screenshot above, the default is already colored. So if you are a Spring Boot 2.x user, there is basically no need to change this.

The output file

The Spring Boot default 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 several parameters in the configuration file application.properties, such as this:

logging.file.name=run.log
logging.file.path=./
Copy the code
  • logging.file.name: Set the file name.
  • logging.file.path: Sets the file path

Note: this is different from the 1.x version, where the parameters are logging.file and logging.path respectively.

File the scroll

Keeping logs in one file is obviously not a good idea, and any logging framework has a rolling configuration for log files for this purpose. Since the default configuration is used in this article, it is the Logback configuration. Specifically, there are several:

  • logging.logback.rollingpolicy.file-name-pattern: File name mode for creating log files.
  • logging.logback.rollingpolicy.clean-history-on-start: Indicates whether the log archive is cleared when the application starts. The default value is false
  • logging.logback.rollingpolicy.max-history: Indicates the maximum number of archive log files to be retained. The default value is 7
  • logging.logback.rollingpolicy.max-file-size: maximum size of a log file before archiving. The default size is 10MB
  • logging.logback.rollingpolicy.total-size-cap: Indicates the maximum capacity of log files before deletion. The default value is 0B

Level control

If you want to do some simple output level control for individual loggers, you can do it by configuring it 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.didispace=DEBUG:com.didispaceAll classes in the package are output at DEBUG level
  • logging.level.root=WARN: Root logs are output at the WARN level

After this configuration, you can execute the above program and the original debug Hello World will be printed successfully.

Custom log configuration

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
  • Log4j2:log4j2-spring.xml.log4j2.xml
  • JDK (Java Util Logging) :logging.properties

Spring Boot is recommended-springFile name as your log configuration (as usedlogback-spring.xmlRather thanlogback.xml)

Customize the output format

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

  • Logging.pattern. console: Define styles for output to the console (JDK Logger not supported)
  • Logging.pattern.file: Defines styles for output to files (JDK Logger is not supported)

Well, that’s all for today’s learning! If you have any difficulty in learning? You can join our super high quality Spring technology exchange group, participate in the exchange and discussion, better learning and progress! Summary page “Spring Boot basic tutorial” can click direct! , welcome to collect and forward support!

Code sample

For the complete project of this article, see the chapter8-1 project in the 2.x directory of the warehouse below:

  • Github:github.com/dyc87112/Sp…
  • Gitee:gitee.com/didispace/S…

If you found this article good, welcomeStarSupport, your attention is my motivation!

Welcome to pay attention to my public number: program ape DD. Learn cutting-edge industry news, share in-depth technical know-how, and obtain high-quality learning resources at the first time