Practice
1. What is the main purpose of file-based logging in a Spring Boot application?
easy
Solution
Step 1: Understand logging basics in Spring Boot
Logging captures messages about app behavior. File-based logging stores these messages in a file.Step 2: Identify the purpose of file-based logging
It allows reviewing logs later for debugging or monitoring, unlike console-only logging.Final Answer:
To save application messages to a file for later review -> Option AQuick Check:
File-based logging = save logs to file [OK]
Hint: File-based logging means saving logs to a file [OK]
Common Mistakes:
- Confusing file logging with console logging
- Thinking it deletes old logs automatically
- Assuming it encrypts logs by default
2. Which of the following is the correct way to enable file-based logging in
application.properties?easy
Solution
Step 1: Recall Spring Boot logging property names
The correct property to set the log file name islogging.file.name.Step 2: Check each option for correctness
Only logging.file.name=logs/app.log uses the exact propertylogging.file.namewith a valid file path.Final Answer:
logging.file.name=logs/app.log -> Option BQuick Check:
Correct property for log file name = logging.file.name [OK]
Hint: Use logging.file.name to set log file path [OK]
Common Mistakes:
- Using incorrect property names like logging.filepath
- Misspelling property keys
- Confusing logging.file.name with logging.pathfile
3. Given this
What will happen when the Spring Boot app runs?
application.yml snippet:logging:
file:
name: logs/myapp.log
level:
root: INFOWhat will happen when the Spring Boot app runs?
medium
Solution
Step 1: Analyze logging.file.name setting
The logs will be saved to the file path logs/myapp.log as configured.Step 2: Analyze logging.level.root setting
The root log level is INFO, so logs INFO and above will be recorded.Final Answer:
Logs will be saved to logs/myapp.log with INFO level and above -> Option DQuick Check:
File path + INFO level = logs/myapp.log with INFO+ logs [OK]
Hint: logging.file.name sets file, logging.level.root sets minimum log level [OK]
Common Mistakes:
- Assuming logs only go to console
- Confusing log level INFO with DEBUG or ERROR
- Thinking YAML syntax is invalid here
4. You configured file logging in
But no log file is created after running the app. What is the most likely cause?
application.properties as:logging.file.name=logs/app.logBut no log file is created after running the app. What is the most likely cause?
medium
Solution
Step 1: Check directory existence for log file
If the logs directory does not exist, Spring Boot may not create it automatically, so no file is created.Step 2: Verify other options
Log level defaults to INFO, property name is correct, and Spring Boot supports file logging.Final Answer:
The logs directory does not exist and is not created automatically -> Option CQuick Check:
Missing log directory = no log file created [OK]
Hint: Ensure log directory exists before running app [OK]
Common Mistakes:
- Using wrong property name
- Assuming Spring Boot disables file logging
- Ignoring missing directory errors
5. You want to keep log files organized by date in Spring Boot. Which configuration snippet in
application.yml correctly enables daily rolling log files using Logback?hard
Solution
Step 1: Identify correct property names for Logback rolling policy
The correct key isrollingpolicy(lowercase 'p'), andfile-name-pattern(kebab-case) is the correct pattern key.Step 2: Check each option for exact spelling and structure
logging: file: name: logs/app.log logback: rollingpolicy: file-name-pattern: logs/app-%d{yyyy-MM-dd}.log uses correct keys:rollingpolicyandfile-name-pattern.Final Answer:
logging: file: name: logs/app.log logback: rollingpolicy: file-name-pattern: logs/app-%d{yyyy-MM-dd}.log -> Option AQuick Check:
Correct keys for rollingpolicy = logging: file: name: logs/app.log logback: rollingpolicy: file-name-pattern: logs/app-%d{yyyy-MM-dd}.log [OK]
Hint: Use exact keys: rollingpolicy and file-name-pattern [OK]
Common Mistakes:
- Using uppercase 'rollingPolicy' instead of 'rollingpolicy'
- Using 'filePattern' or 'fileNamePattern' instead of 'file-name-pattern'
- Confusing logging.file.name with logging.file.path
