You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Maven环境下为Spring Boot配置Log4j默认日志格式?

How to Configure Maven App Logs to Match Your Desired Format

Hey there! The log format you're showing is actually the default log style used by Spring Boot (it relies on Logback as the underlying logging framework). Let me break down exactly how to set this up for your Maven application, whether it's a Spring Boot project or a standard Maven app:


1. For Spring Boot Maven Projects (Simplest Approach)

If you're using Spring Boot, you don't need to mess with complex XML configs—just add these settings to your application.properties or application.yml file:

Using application.properties:

# Match your exact desired log pattern
logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %pid --- [%thread] %logger{36} : %msg%n

# Optional: Set log levels for specific packages (adjust as needed)
logging.level.org.apache.catalina=INFO
logging.level.org.springframework.web=INFO

Using application.yml:

logging:
  pattern:
    console: "%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %pid --- [%thread] %logger{36} : %msg%n"
  level:
    org.apache.catalina: INFO
    org.springframework.web: INFO

2. For Standard Maven Projects (Non-Spring Boot)

If this is a regular Maven app without Spring Boot, you'll need to set up Logback manually:

Step 1: Add Logback & SLF4J Dependencies

Add these to your pom.xml to include the logging libraries:

<dependencies>
    <!-- SLF4J API (logging facade) -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.36</version>
    </dependency>
    <!-- Logback Classic (actual logging implementation) -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.11</version>
    </dependency>
</dependencies>

Step 2: Create Logback Configuration File

Create src/main/resources/logback.xml with this content to define your log pattern:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <!-- Console appender to output logs to terminal -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <!-- The exact pattern matching your desired format -->
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} %-5level %pid --- [%thread] %logger{36} : %msg%n</pattern>
            <charset>UTF-8</charset>
        </encoder>
    </appender>

    <!-- Set root log level to INFO (adjust to DEBUG if needed) -->
    <root level="INFO">
        <appender-ref ref="CONSOLE"/>
    </root>
</configuration>

What Each Part of the Pattern Does

Let me explain the placeholders so you understand how it matches your example:

  • %d{yyyy-MM-dd HH:mm:ss.SSS}: Outputs the timestamp with millisecond precision (matches 2019-03-05 10:57:51.112)
  • %-5level: Left-aligns the log level (INFO, DEBUG, etc.) and reserves 5 characters (matches INFO )
  • %pid: Shows the current application process ID (matches 45469)
  • ---: A static separator string (matches your example's ---)
  • [%thread]: Displays the thread name, truncating it if it's longer than 15 characters (matches [ main] or [ost-startStop-1])
  • %logger{36}: Shows the logger/class name, abbreviating package names if it exceeds 36 characters (matches org.apache.catalina.core.StandardEngine or o.a.c.c.C.[Tomcat].[localhost].[/])
  • %msg%n: Outputs the log message followed by a newline

Troubleshooting Tips

  • If your old log pattern is still showing up, make sure you've replaced any existing logging config files (like log4j.properties or old logback.xml files)
  • For Spring Boot, ensure no other logging frameworks (like Log4j) are overriding Logback—Spring Boot defaults to Logback, but conflicting dependencies can cause issues
  • Double-check that your config file is in the correct location (src/main/resources for Maven projects)

内容的提问来源于stack exchange,提问作者Dark_shadow_side

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 08:15:41