如何在Maven环境下为Spring Boot配置Log4j默认日志格式?
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 (matches2019-03-05 10:57:51.112)%-5level: Left-aligns the log level (INFO, DEBUG, etc.) and reserves 5 characters (matchesINFO)%pid: Shows the current application process ID (matches45469)---: 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 (matchesorg.apache.catalina.core.StandardEngineoro.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.propertiesor oldlogback.xmlfiles) - 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/resourcesfor Maven projects)
内容的提问来源于stack exchange,提问作者Dark_shadow_side

