Bird
Raised Fist0
Spring Bootframework~5 mins

File-based logging in Spring Boot

Choose your learning style10 modes available
Introduction

File-based logging saves messages from your application into files. This helps you keep track of what happened and find problems later.

You want to keep a record of application errors for troubleshooting.
You need to monitor application activity over time.
You want to share logs with your team for debugging.
You want to keep logs even after the application stops.
You want to analyze logs with external tools.
Syntax
Spring Boot
logging:
  file:
    name: logs/app.log
  level:
    root: INFO

This example is for application.yml in Spring Boot.

logging.file.name sets the log file path.

Examples
This is the same setup but in application.properties format.
Spring Boot
logging.file.name=logs/app.log
logging.level.root=INFO
Logs saved to /var/log/myapp.log with debug level for more details.
Spring Boot
logging:
  file:
    name: /var/log/myapp.log
  level:
    root: DEBUG
Custom log message format with date and message.
Spring Boot
logging:
  file:
    name: logs/app.log
  pattern:
    file: "%d{yyyy-MM-dd HH:mm:ss} - %msg%n"
Sample Program

This configuration saves logs to logs/app.log with info level messages and above.

Spring Boot
logging:
  file:
    name: logs/app.log
  level:
    root: INFO
OutputSuccess
Important Notes

Make sure the folder for logs exists or your app can create it.

Log files can grow large; consider log rotation tools for long-term use.

You can change log levels to control how much detail is saved.

Summary

File-based logging saves app messages to a file for later review.

Configure file path and log level in application.yml or application.properties.

Useful for debugging, monitoring, and sharing logs.

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