📖 Scenario: You are building a Spring Boot application that has multiple packages. You want to control the logging level separately for each package to help with debugging and monitoring.
🎯 Goal: Configure logging levels at the package level in a Spring Boot application using application.properties file.
📋 What You'll Learn
Create a Spring Boot project with at least two packages: com.example.service and com.example.controller
Set the logging level for com.example.service to DEBUG
Set the logging level for com.example.controller to INFO
Verify that the logging configuration is correctly applied
💡 Why This Matters
🌍 Real World
In real projects, controlling logging levels per package helps developers focus on relevant logs and reduces noise during debugging and monitoring.
💼 Career
Understanding package-level logging configuration is essential for backend developers working with Spring Boot to maintain and troubleshoot applications efficiently.
Progress0 / 4 steps
1
Create packages in the Spring Boot project
Create two packages in your Spring Boot project: com.example.service and com.example.controller. You do not need to add any classes yet.
Spring Boot
Hint
In your IDE or project structure, create the two packages exactly as com.example.service and com.example.controller.
2
Add logging level configuration in application.properties
In the src/main/resources/application.properties file, add two lines to set the logging level for com.example.service to DEBUG and for com.example.controller to INFO.
Spring Boot
Hint
Open application.properties and add these exact lines:
Create a class ServiceClass in com.example.service and a class ControllerClass in com.example.controller. In each class, add a logger and a method that logs a message at the DEBUG level for ServiceClass and at the INFO level for ControllerClass.
Spring Boot
Hint
Create two classes with the exact names and add logging using LoggerFactory.getLogger. Use logger.debug in ServiceClass and logger.info in ControllerClass.
4
Complete the Spring Boot application with main class and run
Add the main Spring Boot application class Application in com.example package. In the main method, create instances of ServiceClass and ControllerClass and call their logging methods. This completes the package-level log configuration setup.
Spring Boot
Hint
Create a Application class annotated with @SpringBootApplication. In main, run the app and call the logging methods on instances of ServiceClass and ControllerClass.
Practice
(1/5)
1. What is the purpose of setting logging.level.<package>=<level> in Spring Boot's application.properties?
easy
A. To control the logging detail for a specific Java package
B. To define the database connection URL
C. To set the server port number
D. To configure the application's timezone
Solution
Step 1: Understand the configuration key
The key logging.level.<package> is used to specify logging levels for Java packages in Spring Boot.
Step 2: Identify the purpose of this setting
This setting controls how detailed the logs are for that package, helping to focus on important logs and reduce noise.
Final Answer:
To control the logging detail for a specific Java package -> Option A
Quick Check:
Package-level log control = A [OK]
Hint: Remember: logging.level.package sets log detail per package [OK]
Common Mistakes:
Confusing logging level with server or database settings
Trying to set logging level without specifying a package
Using incorrect property keys like logging.package.level
2. Which of the following is the correct syntax to set the log level to DEBUG for the package com.example.service in application.properties?
easy
A. logging.level=com.example.service=DEBUG
B. log.level.com.example.service=DEBUG
C. logging.com.example.service.level=DEBUG
D. logging.level.com.example.service=DEBUG
Solution
Step 1: Recall the correct property format
The correct format is logging.level.<package>=<level>, so the package name comes immediately after logging.level..
Step 2: Match the option to the format
logging.level.com.example.service=DEBUG matches this format exactly: logging.level.com.example.service=DEBUG.
Final Answer:
logging.level.com.example.service=DEBUG -> Option D
Quick Check:
Correct syntax = B [OK]
Hint: Use logging.level.package=LEVEL exactly as shown [OK]
Common Mistakes:
Adding extra '=' signs in the property
Swapping order of package and level
Using 'log.level' instead of 'logging.level'
3. Given the following application.properties settings: logging.level.com.example=INFO logging.level.com.example.service=DEBUG What will be the log level for classes in com.example.service package?
medium
A. INFO
B. DEBUG
C. ERROR
D. WARN
Solution
Step 1: Understand package-level log hierarchy
More specific package settings override broader ones. Here, com.example.service is more specific than com.example.
Step 2: Determine effective log level
The com.example.service package is set to DEBUG, which overrides the INFO level set for com.example.
Final Answer:
DEBUG -> Option B
Quick Check:
Specific package level overrides general = C [OK]
Hint: More specific package log level overrides parent package [OK]
Common Mistakes:
Assuming parent package level applies to all subpackages
Ignoring more specific package settings
Confusing INFO and DEBUG levels
4. You set logging.level.com.example=TRACE in application.properties but still see only INFO logs from com.example classes. What is the most likely cause?
medium
A. Logging is disabled globally
B. TRACE level is not supported in Spring Boot
C. Another configuration overrides the log level to INFO
D. The package name is misspelled in the properties file
Solution
Step 1: Verify if TRACE is supported
TRACE is a valid log level in Spring Boot, so this is not the issue.
Step 2: Consider configuration overrides
If logs show INFO instead of TRACE, likely another config (like environment variables or YAML) overrides the level to INFO.
Final Answer:
Another configuration overrides the log level to INFO -> Option C
Quick Check:
Config override causes unexpected log level = D [OK]
Hint: Check for conflicting configs if log level seems ignored [OK]
Common Mistakes:
Assuming TRACE is unsupported
Not checking for typos in package name
Ignoring global logging settings
5. You want to reduce log noise by setting ERROR level for com.example.repository but keep DEBUG for com.example.service. Which application.properties configuration achieves this?
hard
A. logging.level.com.example.repository=ERROR
logging.level.com.example.service=DEBUG
B. logging.level.com.example=ERROR
logging.level.com.example.service=DEBUG
C. logging.level.com.example.repository=DEBUG
logging.level.com.example.service=ERROR
D. logging.level.com.example.repository=INFO
logging.level.com.example.service=DEBUG
Solution
Step 1: Set ERROR level specifically for repository package
Use logging.level.com.example.repository=ERROR to reduce noise from repository logs.
Step 2: Set DEBUG level specifically for service package
Use logging.level.com.example.service=DEBUG to keep detailed logs for service package.
Step 3: Combine both settings correctly
logging.level.com.example.repository=ERROR
logging.level.com.example.service=DEBUG correctly sets both packages with their respective levels in application.properties.
Final Answer:
logging.level.com.example.repository=ERROR
logging.level.com.example.service=DEBUG -> Option A
Quick Check:
Separate package levels set correctly = A [OK]
Hint: Set each package level separately for precise control [OK]
Common Mistakes:
Setting parent package level instead of specific packages
Swapping log levels between packages
Using incorrect property keys or syntax
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.