Bird
Raised Fist0
Spring Bootframework~10 mins

File-based logging in Spring Boot - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to set the logging file name in application.properties.

Spring Boot
logging.file.name=[1]
Drag options to blanks, or click blank then click option'
Aoutput.log
Blogfile.txt
Capp.log
Dserver.log
Attempts:
3 left
💡 Hint
Common Mistakes
Using a directory path instead of a file name
Forgetting the file extension
2fill in blank
medium

Complete the code to set the logging level to DEBUG in application.properties.

Spring Boot
logging.level.root=[1]
Drag options to blanks, or click blank then click option'
ADEBUG
BINFO
CERROR
DWARN
Attempts:
3 left
💡 Hint
Common Mistakes
Using INFO or ERROR which are less detailed
Using invalid logging levels
3fill in blank
hard

Fix the error in the logback-spring.xml snippet to write logs to a file named app.log.

Spring Boot
<appender name="FILE" class="ch.qos.logback.core.FileAppender">
  <file>[1]</file>
  <encoder>
    <pattern>%d{yyyy-MM-dd HH:mm:ss} - %msg%n</pattern>
  </encoder>
</appender>
Drag options to blanks, or click blank then click option'
A/var/log/app
Bapp.log
Clogs/
Dapp
Attempts:
3 left
💡 Hint
Common Mistakes
Using a directory path without file name
Omitting the file extension
4fill in blank
hard

Fill both blanks to configure a rolling file appender with max file size 10MB and max history 5.

Spring Boot
<rollingPolicy class="ch.qos.logback.core.rolling.[1]">
  <maxFileSize>[2]</maxFileSize>
  <maxHistory>5</maxHistory>
</rollingPolicy>
Drag options to blanks, or click blank then click option'
ASizeBasedRollingPolicy
BTimeBasedRollingPolicy
C10MB
D5MB
Attempts:
3 left
💡 Hint
Common Mistakes
Using time-based rolling policy instead of size-based
Setting maxFileSize to 5MB instead of 10MB
5fill in blank
hard

Fill all three blanks to create a log pattern with date, log level, and message.

Spring Boot
<pattern>[1] - [2] - [3]</pattern>
Drag options to blanks, or click blank then click option'
A%d{yyyy-MM-dd HH:mm:ss}
B%level
C%msg
D%thread
Attempts:
3 left
💡 Hint
Common Mistakes
Using thread name instead of log level
Omitting the message part

Practice

(1/5)
1. What is the main purpose of file-based logging in a Spring Boot application?
easy
A. To save application messages to a file for later review
B. To send application messages only to the console
C. To delete old log files automatically
D. To encrypt application data before saving

Solution

  1. Step 1: Understand logging basics in Spring Boot

    Logging captures messages about app behavior. File-based logging stores these messages in a file.
  2. Step 2: Identify the purpose of file-based logging

    It allows reviewing logs later for debugging or monitoring, unlike console-only logging.
  3. Final Answer:

    To save application messages to a file for later review -> Option A
  4. Quick 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
A. logging.filepath=logs/app.log
B. logging.file.name=logs/app.log
C. log.file=logs/app.log
D. logging.pathfile=logs/app.log

Solution

  1. Step 1: Recall Spring Boot logging property names

    The correct property to set the log file name is logging.file.name.
  2. Step 2: Check each option for correctness

    Only logging.file.name=logs/app.log uses the exact property logging.file.name with a valid file path.
  3. Final Answer:

    logging.file.name=logs/app.log -> Option B
  4. Quick 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 application.yml snippet:
logging:
  file:
    name: logs/myapp.log
  level:
    root: INFO

What will happen when the Spring Boot app runs?
medium
A. Logs will only print to console with DEBUG level
B. Application will fail to start due to syntax error
C. Logs will be saved to logs/myapp.log but only ERROR level
D. Logs will be saved to logs/myapp.log with INFO level and above

Solution

  1. Step 1: Analyze logging.file.name setting

    The logs will be saved to the file path logs/myapp.log as configured.
  2. Step 2: Analyze logging.level.root setting

    The root log level is INFO, so logs INFO and above will be recorded.
  3. Final Answer:

    Logs will be saved to logs/myapp.log with INFO level and above -> Option D
  4. Quick 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 application.properties as:
logging.file.name=logs/app.log
But no log file is created after running the app. What is the most likely cause?
medium
A. The property should be logging.file.path, not logging.file.name
B. The log level is set to OFF by default
C. The logs directory does not exist and is not created automatically
D. Spring Boot does not support file logging

Solution

  1. 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.
  2. Step 2: Verify other options

    Log level defaults to INFO, property name is correct, and Spring Boot supports file logging.
  3. Final Answer:

    The logs directory does not exist and is not created automatically -> Option C
  4. Quick 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
A. logging: file: name: logs/app.log logback: rollingpolicy: file-name-pattern: logs/app-%d{yyyy-MM-dd}.log
B. logging: file: name: logs/app.log logback: rollingPolicy: filePattern: logs/app-%d{yyyy-MM-dd}.log
C. logging: file: name: logs/app.log logback: rollingpolicy: fileNamePattern: logs/app-%d{yyyy-MM-dd}.log
D. logging: file: path: logs logback: rollingPolicy: fileNamePattern: logs/app-%d{yyyy-MM-dd}.log

Solution

  1. Step 1: Identify correct property names for Logback rolling policy

    The correct key is rollingpolicy (lowercase 'p'), and file-name-pattern (kebab-case) is the correct pattern key.
  2. 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: rollingpolicy and file-name-pattern.
  3. Final Answer:

    logging: file: name: logs/app.log logback: rollingpolicy: file-name-pattern: logs/app-%d{yyyy-MM-dd}.log -> Option A
  4. Quick 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
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard