SoFunction
Updated on 2025-03-03

Guide to log management and tuning in SpringBoot projects

Preface

During Spring Boot development, log management is one of the important skills that developers must master. Reasonable log configuration can not only help developers track the application execution process and locate problems, but also improve the application maintainability. However, in actual development, log configuration often encounters many problems, such as log framework conflicts, log format mismatch, redundant log output, etc. These problems will cause developers to fall into chaos during debugging. This article will explore in detail the common problems, solutions, and best practices of log management in Spring Boot projects.

1. Overview of Spring Boot Log Framework

1.1 Logging frameworks supported by Spring Boot

Spring Boot is built-in by defaultlogbackLog framework, supportslf4jAs an abstract log interface. Common logging frameworks include:

  • Logback: The log framework used by Spring Boot is default, and the configuration file format isor
  • Log4jandLog4j2: Traditional logging framework, configuration file format isor
  • Java Util Logging (JUL): The logging framework that comes with JDK is simple to configure but not powerful enough.
  • Commons Logging: An early abstract logging framework, currently rarely used.

1.2 Spring Boot default log configuration

Spring Boot is used by defaultlogbackImplemented as a log, it will automatically includespring-boot-starter-loggingDependence. When started, it will automatically load theorLog configuration in   and output formatted log information in the project.

By default, Spring Boot uses the following log format:

2024-10-08 17:53:00.073  INFO 20091 --- [   scheduling-1]                  : Important configuration information

The meanings of each part are as follows:

  • Date and time2024-10-08 17:53:00.073): Displays the time of occurrence of the log.
  • Log LevelINFO): Indicates the severity of the log, it can beTRACEDEBUGINFOWARNERROR
  • Process ID20091): Displays the ID of the currently running process.
  • Thread namescheduling-1): Indicates which thread the log is output.
  • Log Name): The class name or package name that represents the source of the log.
  • Log contentImportant configuration information): The specific output information of the log.

2. Log framework conflict issues

2.1 Problem Description

In Spring Boot projects, multiple log frameworks may be introduced simultaneously (e.g.log4jandlogback), which causes log configuration file conflicts or multiple log frameworks to output logs simultaneously. Typical conflict phenomena are as follows:

  • Introducinglog4j2orlog4jWhen dependency, an error message similar to the following will appear:

Unknown object "property" of type . is ignored
  • IntroducelogbackWhen  , if there are other log implementations (such aslog4j2), it may causemvnAn error occurred while building, or the runtime log format was messy.

2.2 Solution

To resolve log framework conflicts, developers need to select appropriate log implementations based on actual needs and remove unnecessary log dependencies. Here are a few common solutions:

Solution 1: Ensure uselogbackAs the only logging framework

  1. Remove the project fromlog4jorlog4j2Related dependencies

    examineWhether it containslog4j-corelog4j-apiOr otherlog4jRelated dependencies, if there are, remove these dependencies and make sure to use onlylogbackAs a logging framework.

<dependency>
    <groupId>.log4j</groupId>
    <artifactId>log4j-core</artifactId>
</dependency>
<dependency>
    <groupId>.log4j</groupId>
    <artifactId>log4j-api</artifactId>
</dependency>

IntroducelogbackDependence(If not introduced):

&lt;dependency&gt;
    &lt;groupId&gt;&lt;/groupId&gt;
    &lt;artifactId&gt;logback-classic&lt;/artifactId&gt;
    &lt;version&gt;1.2.3&lt;/version&gt; &lt;!-- Please use the appropriate version --&gt;
&lt;/dependency&gt;
  • Check whether the item is availableorConfiguration file
    Delete or replace with, make sure the project is only usedlogbackConfiguration.

Solution 2: Uselog4j2Replacementlogback

If you want to uselog4j2As a log implementation, you can configure it according to the following steps:

  • RemovelogbackDependence

    existExcludedlogbackRelated dependencies to ensure that the project does not includelogback-classic

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
        <exclusion>
            <groupId></groupId>
            <artifactId>logback-classic</artifactId>
        </exclusion>
    </exclusions>
</dependency>

Introducelog4j2Dependence

<dependency>
    <groupId>.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.14.1</version>
</dependency>
<dependency>
    <groupId>.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.14.1</version>
</dependency>

CreateConfiguration file

existsrc/main/resourcesCreated in, the configuration content is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

2.3 Check for dependency conflicts

To avoid log dependency conflicts, you can check the log dependencies actually loaded in your project using the following command:

mvn dependency:tree | grep log

This command will output all containinglogDependency package, check whether there are incompatible dependencies (such aslog4jandlogbackexist at the same time).

3. Control log output level

During project development and deployment, controlling the log output level can effectively reduce unnecessary log information and improve debugging efficiency.

3.1 Adjust log level

Spring Boot supports it inorConfigure log level. For example:

# Set the global log level to INFO=INFO

# Set log levels for specific packages=DEBUG
=TRACE

3.2 Control SQL statement logs

SQL statement logs can cause a lot of redundant output when using MyBatis or JPA, and can be optimized by adjusting the levels of SQL-related logs:

# Close SQL logs for MyBatis execution=ERROR
=ERROR

3.3 Using custom log configuration files

Can be passedSpecify a custom log configuration file:

=classpath:

Ensure that the file path is correct and that the configuration file content complies with the corresponding log implementation (logbackorlog4j2)Format.

4. Summary

Log management is a very important part of Spring Boot project development. By rationally configuring the log framework, controlling the log output level, and handling dependency conflicts, developers can be more efficient when debugging and maintaining projects. Hopefully this guide will help you better understand and manage the log configuration of Spring Boot projects, thus creating a more

Robust and efficient applications.

The above is the detailed content of the log management and tuning guide in SpringBoot project. For more information about SpringBoot log management and tuning, please follow my other related articles!