📖 Scenario: You are building a Spring Boot application that needs to keep track of important events and errors by writing logs to a file. This helps developers and system administrators understand what happened during the app's runtime.
🎯 Goal: Set up file-based logging in a Spring Boot application by creating a log file configuration, adding a log message in the code, and verifying the log output is written to the file.
📋 What You'll Learn
Create a Spring Boot application main class
Configure logging to write to a file named app.log
Add a log message using Spring Boot's logger
Run the application and verify the log message appears in app.log
💡 Why This Matters
🌍 Real World
File-based logging helps track application behavior and errors over time, which is essential for troubleshooting and monitoring in real-world software.
💼 Career
Understanding how to configure and use logging in Spring Boot is a key skill for backend developers and DevOps engineers to maintain reliable applications.
Progress0 / 4 steps
1
Create Spring Boot main application class
Create a Java class called LoggingApplication in package com.example.logging with a main method that starts the Spring Boot application using SpringApplication.run(LoggingApplication.class, args).
Spring Boot
Hint
This is the standard way to start a Spring Boot app. Use the @SpringBootApplication annotation and a main method.
2
Configure logging to write to a file
Create a file named src/main/resources/application.properties and add a property to set the logging file name to app.log using logging.file.name=app.log.
Spring Boot
Hint
The application.properties file controls Spring Boot settings. Add logging.file.name=app.log to write logs to app.log.
3
Add a log message in the application
In the LoggingApplication class, add a private static final logger using LoggerFactory.getLogger(LoggingApplication.class). Then add a System.out.println line inside main to print "Application started" and a log info message with the same text using logger.info("Application started").
Spring Boot
Hint
Use SLF4J Logger to add a log message. Also print to console to see immediate output.
4
Run and verify log output
Run the Spring Boot application. Then open the app.log file in the project root folder and verify it contains the line with "Application started" logged by the logger.
Spring Boot
Hint
Run the app using your IDE or command line. Then open app.log to see the logged message.
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
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 A
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
Step 1: Recall Spring Boot logging property names
The correct property to set the log file name is logging.file.name.
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.
Final Answer:
logging.file.name=logs/app.log -> Option B
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
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 D
Quick Check:
File path + INFO level = logs/myapp.log with INFO+ logs [OK]
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
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 C
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
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.
Step 2: Check each option for exact spelling and structure