How to enable debugging log in the Spring Boot application?

Debug logging in Spring Boot is used to generate detailed runtime information about the application's execution. It helps developers troubleshoot issues, understand application behavior, track bean initialization, inspect request processing, and diagnose configuration problems during development and testing.

Key Points: • Debug logging provides detailed information about application execution. • Logging levels can be configured globally or for specific packages. • DEBUG logs are useful for troubleshooting and root cause analysis. • Spring Boot supports configuration through properties, YAML, command-line arguments, and environment variables. • Excessive debug logging should generally be avoided in production environments.

Why Enable Debug Logging?

Debug logs help developers understand:

• Application startup process • Bean creation and dependency injection • Auto-configuration decisions • Database interactions • Request processing flow • Exception root causes

This makes troubleshooting significantly easier.

Method 1: Enable DEBUG Level Globally

The simplest approach is to set the root logging level.

Code Example:

logging.level.root=DEBUG

After restarting the application, detailed logs will be generated for all components.

Method 2: Enable DEBUG for a Specific Package

Instead of logging everything, we can target a specific package.

Code Example:

logging.level.com.company.service=DEBUG

Only classes within the specified package will generate DEBUG logs.

This approach is preferred because it reduces log noise.

Method 3: Using application.yml

Code Example:

logging: level: root: DEBUG

Or for a specific package:

logging: level: com.company.service: DEBUG

Method 4: Using Command-Line Arguments

Code Example:

java -jar app.jar --logging.level.root=DEBUG

This is useful when debugging without modifying configuration files.

Method 5: Using Spring Boot Debug Mode

Spring Boot provides a special debug flag.

Code Example:

debug=true

or

java -jar app.jar --debug

This enables additional startup diagnostics and auto-configuration reports.

Example Startup Output

When debug mode is enabled, Spring Boot displays information such as:

• Auto-configurations applied • Auto-configurations skipped • Bean initialization details • Environment configuration

This is extremely useful for troubleshooting startup issues.

How Logging Levels Work

TRACE | DEBUG | INFO | WARN | ERROR

DEBUG provides more details than INFO but fewer details than TRACE.

Common Logging Levels

TRACE

• Very detailed diagnostics

DEBUG

• Development and troubleshooting

INFO

• General application information

WARN

• Potential issues

ERROR

• Application failures

Example: Suppose a REST API is not connecting to the database.

By enabling:

logging.level.org.springframework.jdbc=DEBUG

Spring Boot logs:

• SQL statements • Connection creation • Query execution details

This helps identify the exact cause of the issue.

Benefits of Debug Logging

• Easier troubleshooting • Better visibility into application behavior • Faster root cause analysis • Improved development productivity • Detailed startup diagnostics

Production Considerations

DEBUG logging generates a large amount of output.

For production environments:

• INFO is generally preferred. • DEBUG should be enabled temporarily when investigating issues. • Excessive DEBUG logging can affect performance and increase log storage requirements.

Real-World Example

A Spring Boot application fails to start because a required bean is missing.

After enabling:

debug=true

Spring Boot generates an auto-configuration report showing:

• Which configurations were applied • Which configurations were skipped • Why a bean could not be created

This helps developers quickly identify and fix the problem.

Interview Tip: A concise interview answer is:

"Debug logging in Spring Boot can be enabled by setting logging.level.root=DEBUG in application.properties or by using debug=true for additional startup diagnostics. It provides detailed runtime information that helps troubleshoot application behavior, configuration issues, and startup problems."