SoFunction
Updated on 2025-03-05

LogBack log configuration and multi-environment combat in SpringBoot

introduction

Logging is an indispensable part of modern software development. It not only helps us understand the running status of the application, but also provides important debugging information when problems arise. Spring Boot provides support for a variety of logging frameworks, with Logback being a very popular choice because it is simple, efficient and powerful. This article will introduce how to configure Logback in Spring Boot project and implement log configuration in different environments.

1. Introduction to Logback

Logback is a log framework created by Ceki Gülcü, which is the successor to log4j and aims to solve problems in log4j. Logback is divided into three modules: logback-core, logback-classic and logback-access. in:

  • logback-core is the basis of the other two modules.
  • logback-classic is an improved version of log4j and is fully compatible with the SLF4J API.
  • logback-access integrates with Servlet containers to provide access logging capabilities for web applications.

2. Logback configuration in Spring Boot

Spring Boot uses Logback as its logging framework by default. To customize the log configuration, you need to create a file (or ​​​​​) in the project's src/main/resources directory.

2.1 Basic configuration example

Here is a basic configuration file example:

<configuration>
    <appender name="STDOUT" class="">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
        </encoder>
    </appender>
 
    <root level="info">
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

2.2 Advanced Configuration

In order to control log output more flexibly, more configuration options can be added, such as file scrolling policies, asynchronous logs, etc.

2.2.1 File scrolling strategy

Use​RollingFileAppender​​ It can realize automatic scrolling and archiving of log files:

&lt;appender name="FILE" class=""&gt;
    &lt;file&gt;logs/&lt;/file&gt;
    &lt;rollingPolicy class=""&gt;
        &lt;!-- Generate a new log file every day --&gt;
        &lt;fileNamePattern&gt;logs/app-%d{yyyy-MM-dd}.log&lt;/fileNamePattern&gt;
        &lt;!-- reserve 30 Day's log file --&gt;
        &lt;maxHistory&gt;30&lt;/maxHistory&gt;
    &lt;/rollingPolicy&gt;
    &lt;encoder&gt;
        &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n&lt;/pattern&gt;
    &lt;/encoder&gt;
&lt;/appender&gt;
 
&lt;root level="info"&gt;
    &lt;appender-ref ref="STDOUT" /&gt;
    &lt;appender-ref ref="FILE" /&gt;
&lt;/root&gt;
2.2.2 Asynchronous logging

Use​Lombok​of​@Slf4j​Annotations and​Logback​of​AsyncAppender​​ Can implement asynchronous logging to improve performance:

<appender name="ASYNC" class="">
    <appender-ref ref="FILE" />
</appender>
 
<root level="info">
    <appender-ref ref="ASYNC" />
</root>

3. Multi-environment configuration

In practical applications, different environments (such as development, testing, production) may require different log configurations. Spring Boot supports differentiating environments through configuration files.

3.1 Use ​​​application-{profile}.properties​ or ​​application-{profile}.yml​

You can create multiple configuration files in the src/main/resources directory, such as ​​​, ​​​​​, etc., and then specify different ​​​​​ or ​​.*​​ properties in these files.

3.2 Dynamically switch configuration files

You can specify the configuration file currently used through command line parameters or system properties:

java -jar  --=dev

3.3 Multi-environment ​​​​

In​​, you can use​<springProfile>​​ Tags to load different configurations according to the activated Spring Profile:

<configuration>
    <springProfile name="dev">
        <appender name="STDOUT" class="">
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
 
        <root level="debug">
            <appender-ref ref="STDOUT" />
        </root>
    </springProfile>
 
    <springProfile name="prod">
        <appender name="FILE" class="">
            <file>logs/</file>
            <rollingPolicy class="">
                <fileNamePattern>logs/app-%d{yyyy-MM-dd}.log</fileNamePattern>
                <maxHistory>30</maxHistory>
            </rollingPolicy>
            <encoder>
                <pattern>%d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
 
        <root level="info">
            <appender-ref ref="FILE" />
        </root>
    </springProfile>
</configuration>

Through the above configuration, we can flexibly manage logging of Spring Boot applications, whether it is simple console output or complex file scrolling and asynchronous recording, it can meet the needs of different environments. Hope this article helps you better utilize Logback for log management in real projects.

In Spring Boot applications, ​​​​ is a commonly used log configuration file that configures log output format, level, and target. Through reasonable configuration, different logging strategies can be implemented in different environments (such as development, testing, and production).

1. Basic configuration

First, create a basic file and place it in the src/main/resources directory. This file will serve as the basic configuration for all environments.

&lt;configuration&gt;
    &lt;property name="LOG_PATH" value="logs"/&gt;
    &lt;property name="APP_NAME" value="myapp"/&gt;
 
    &lt;!-- Console output --&gt;
    &lt;appender name="CONSOLE" class=""&gt;
        &lt;encoder&gt;
            &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} - %msg%n&lt;/pattern&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;
 
    &lt;!-- File output --&gt;
    &lt;appender name="FILE" class=""&gt;
        &lt;file&gt;${LOG_PATH}/${APP_NAME}.log&lt;/file&gt;
        &lt;rollingPolicy class=""&gt;
            &lt;fileNamePattern&gt;${LOG_PATH}/${APP_NAME}-%d{yyyy-MM-dd}.log&lt;/fileNamePattern&gt;
            &lt;maxHistory&gt;30&lt;/maxHistory&gt;
        &lt;/rollingPolicy&gt;
        &lt;encoder&gt;
            &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} - %msg%n&lt;/pattern&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;
 
    &lt;!-- Root log configuration --&gt;
    &lt;root level="info"&gt;
        &lt;appender-ref ref="CONSOLE" /&gt;
        &lt;appender-ref ref="FILE" /&gt;
    &lt;/root&gt;
&lt;/configuration&gt;

2. Multi-environment configuration

To support multi-environment configuration, different log paths and log levels can be defined in ​​​​​ or ​​​​, and Spring Boot's profile feature is used to distinguish different environments.

# Default configuration=info
=${LOG_PATH}/
 
# Development Environment=dev
=debug
 
# Test environment=test
=info
 
# Production environment=prod
=warn

=debug
=logs/

=info
=logs/

=warn
=logs/

3. Use Spring Boot Profile

In​​, can be passed​${}​​ to dynamically obtain the currently activated profile and apply different log configurations according to different profiles.

&lt;configuration&gt;
    &lt;property name="LOG_PATH" value="logs"/&gt;
    &lt;property name="APP_NAME" value="myapp"/&gt;
    &lt;property name="PROFILE" value="${}"/&gt;
 
    &lt;!-- Console output --&gt;
    &lt;appender name="CONSOLE" class=""&gt;
        &lt;encoder&gt;
            &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} - %msg%n&lt;/pattern&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;
 
    &lt;!-- File output --&gt;
    &lt;appender name="FILE" class=""&gt;
        &lt;file&gt;${LOG_PATH}/${PROFILE}-${APP_NAME}.log&lt;/file&gt;
        &lt;rollingPolicy class=""&gt;
            &lt;fileNamePattern&gt;${LOG_PATH}/${PROFILE}-${APP_NAME}-%d{yyyy-MM-dd}.log&lt;/fileNamePattern&gt;
            &lt;maxHistory&gt;30&lt;/maxHistory&gt;
        &lt;/rollingPolicy&gt;
        &lt;encoder&gt;
            &lt;pattern&gt;%d{yyyy-MM-dd HH:mm:ss} - %msg%n&lt;/pattern&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;
 
    &lt;!-- Root log configuration --&gt;
    &lt;root level="${}"&gt;
        &lt;appender-ref ref="CONSOLE" /&gt;
        &lt;appender-ref ref="FILE" /&gt;
    &lt;/root&gt;
&lt;/configuration&gt;

4. Start the application and specify the environment

When starting the application, you can specify the current environment through command line parameters:

# Development Environmentjava -jar  --=dev
 
# Test environmentjava -jar  --=test
 
# Production environmentjava -jar  --=prod

5. Verify log output
After starting the application, check the console and log files to make sure the logs are output as expected. For example, in a development environment, the log level is debug, and the log file name is ​​​​.

Through the above configuration, you can easily manage log output in different environments, ensuring that the log information meets the needs of development and debugging, and maintains the appropriate log volume in the production environment. In Spring Boot applications, Logback is a very popular logging framework that manages application log output via ​​​​​ or ​​​​​​ configuration files. Spring Boot provides native support for Logback, making configuring and using logs very convenient. Below, I will explain in detail how to configure Logback in Spring Boot to support multiple environments and provide some practical code examples.

1. Basic configuration

First, create the ​​​​ file in the src/main/resources directory. This file name is the file name found by Spring Boot by default and is used to configure Logback.

<configuration>
    <property name="LOG_PATH" value="logs"/>
    <property name="LOG_FILE" value="${LOG_PATH}/"/>
 
    <appender name="CONSOLE" class="">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
        </encoder>
    </appender>
 
    <appender name="FILE" class="">
        <file>${LOG_FILE}</file>
        <rollingPolicy class="">
            <!-- daily rollover -->
            <fileNamePattern>${LOG_PATH}/app.%d{yyyy-MM-dd}.log</fileNamePattern>
            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

2. Multi-environment configuration

To support multiple environments (such as development, testing, production), you can refer to these properties in ​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​​

# Development Environment=dev
=debug
=logs/
 
# Test environment# =test
# =info
# =logs/
 
# Production environment# =prod
# =warn
# =logs/

<configuration>
    <springProperty scope="context" name="LOG_LEVEL" source=""/>
    <springProperty scope="context" name="LOG_FILE" source=""/>
 
    <appender name="CONSOLE" class="">
        <encoder>
            <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
        </encoder>
    </appender>
 
    <appender name="FILE" class="">
        <file>${LOG_FILE}</file>
        <rollingPolicy class="">
            <fileNamePattern>${LOG_FILE}.%d{yyyy-MM-dd}.log</fileNamePattern>
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <encoder>
            <pattern>%date %level [%thread] %logger{10} [%file:%line] %msg%n</pattern>
        </encoder>
    </appender>
 
    <root level="${LOG_LEVEL}">
        <appender-ref ref="CONSOLE" />
        <appender-ref ref="FILE" />
    </root>
</configuration>

3. Dynamic log level adjustment

Spring Boot also supports dynamic adjustment of log levels at runtime. This can be done through​Actuator​Module implementation. First, make sure your project contains​spring-boot-starter-actuator​Dependencies:

<dependency>
    <groupId></groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Then, you can​​Enable endpoints in ​:

=*

After starting the application, you can adjust the log level via the following URL:

POST http://localhost:8080/actuator/loggers/{loggerName}
Content-Type: application/json
{
    "configuredLevel": "DEBUG"
}

For example, to include​​The log level under the package is set to​DEBUG​​, the following request can be sent:

POST http://localhost:8080/actuator/loggers/
Content-Type: application/json
{
    "configuredLevel": "DEBUG"
}

Summarize

With the above configuration, you can easily use Logback in Spring Boot apps to manage and record logs, and be able to dynamically adjust log configurations according to different environments. This is very helpful for developing, testing and maintaining applications. Hope this information will be helpful to you!

The above is the detailed content of LogBack log configuration and multi-environment practice in SpringBoot. For more information about SpringBoot LogBack configuration and multi-environment, please pay attention to my other related articles!