📖 Scenario: You are building a Spring Boot application that logs messages. You want to make the logs easier to read by formatting them with a clear pattern.
🎯 Goal: Configure the Spring Boot application to use a custom log format pattern that shows the timestamp, log level, logger name, and message.
📋 What You'll Learn
Create a basic Spring Boot application properties file
Add a configuration variable for the log pattern
Apply the log pattern to the logging system
Verify the final log format configuration
💡 Why This Matters
🌍 Real World
Clear and consistent log formatting helps developers and system operators quickly understand application behavior and troubleshoot issues.
💼 Career
Knowing how to configure logging is essential for backend developers and DevOps engineers working with Spring Boot applications.
Progress0 / 4 steps
1
Create application.properties file
Create a file named application.properties in the src/main/resources folder with this exact content: logging.pattern.console= (leave it empty for now).
Spring Boot
Hint
This file controls Spring Boot settings including logging.
2
Add a log pattern variable
In the application.properties file, set logging.pattern.console to the pattern %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n exactly.
Spring Boot
Hint
This pattern shows date/time, log level, logger name, and message in each log line.
3
Apply the log pattern in Spring Boot
Ensure your Spring Boot application uses the application.properties file with the logging.pattern.console property set to format console logs with the pattern %d{yyyy-MM-dd HH:mm:ss} %-5level %logger{36} - %msg%n.
Spring Boot
Hint
Spring Boot automatically applies this property to console logs.
4
Complete and verify log formatting
Run your Spring Boot application and verify that console logs show the timestamp, log level, logger name, and message formatted as 2024-01-01 12:00:00 INFO com.example.MyClass - Application started.
Spring Boot
Hint
Look at the console output when the app runs to confirm the format.
Practice
(1/5)
1. What is the purpose of configuring log formatting in a Spring Boot application?
easy
A. To disable logging completely
B. To increase the size of log files automatically
C. To make logs easier to read and understand by customizing their appearance
D. To encrypt log messages for security
Solution
Step 1: Understand the role of log formatting
Log formatting controls how log messages appear, making them clearer and easier to read.
Step 2: Identify the main goal of formatting
The main goal is to customize the log output for better readability, not to disable or encrypt logs.
Final Answer:
To make logs easier to read and understand by customizing their appearance -> Option C
Quick Check:
Log formatting = Customize log appearance [OK]
Hint: Log formatting improves readability, not disables logging [OK]
Common Mistakes:
Thinking log formatting disables logs
Confusing formatting with log encryption
Assuming formatting increases log file size
2. Which of the following is the correct way to set a simple console log pattern in application.properties?
easy
A. console.logging.pattern=%date %level %logger %message
B. logging.pattern.console=%d{HH:mm:ss} %-5level %logger{36} - %msg%n
C. logging.console.pattern=%d{HH:mm:ss} %level %logger - %msg
D. logging.pattern=%d %level %logger %msg
Solution
Step 1: Identify the correct property key for console log pattern
The correct key is logging.pattern.console in Spring Boot.
Step 2: Check the pattern syntax
The pattern uses placeholders like %d for date, %-5level for log level, %logger{36} for logger name, and %msg for message, ending with %n for newline.
Final Answer:
logging.pattern.console=%d{HH:mm:ss} %-5level %logger{36} - %msg%n -> Option B
A. 2024-06-01 14:30:00 [main] INFO MyApp - Application started
B. [main] 2024-06-01 14:30:00 INFO MyApp - Application started
C. INFO 2024-06-01 14:30:00 [main] MyApp - Application started
D. MyApp INFO [main] 2024-06-01 14:30:00 - Application started
Solution
Step 1: Analyze the pattern placeholders
%d{yyyy-MM-dd HH:mm:ss} prints date/time, [%thread] prints thread in brackets, %-5level prints log level left-aligned in 5 spaces, %logger{10} prints logger name truncated to 10 chars, %msg prints the message, %n adds newline.
Step 2: Match the pattern to the output options
2024-06-01 14:30:00 [main] INFO MyApp - Application started matches the exact order and format: date, thread in brackets, level padded, logger, dash, message.
Final Answer:
2024-06-01 14:30:00 [main] INFO MyApp - Application started -> Option A
Quick Check:
Pattern placeholders = Output format [OK]
Hint: Match placeholders order exactly to output format [OK]
5. You want to configure your Spring Boot app to log to both console and file with different formats. Which configuration snippet correctly sets a detailed file log pattern and a simple console pattern?