Spring Boot

Article 15: Logging & Monitoring in Spring Boot

By Utility Zone · 2026-01-27T18:28:37.687436

1. Introduction

If something breaks in production and you can’t see what happened, you’re blind.

Logging and monitoring help you:

  • Debug issues quickly
  • Understand application behavior
  • Monitor health and performance

Spring Boot provides excellent defaults plus powerful extensions.


2. Logging in Spring Boot (Basics)

Spring Boot uses:

  • SLF4J (logging API)
  • Logback (default implementation)

You don’t need to add dependencies — logging works out of the box.


3. Using Logger in Code

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

@RestController
public class UserController {

    private static final Logger log =
            LoggerFactory.getLogger(UserController.class);

    @GetMapping("/log-test")
    public String test() {
        log.info("Info log");
        log.warn("Warning log");
        log.error("Error log");
        return "Check logs";
    }
}

4. Log Levels Explained

LevelUsage
TRACEVery detailed
DEBUGDebugging
INFONormal flow
WARNPotential issues
ERRORFailures

5. Configuring Log Levels

In application.properties:

logging.level.root=INFO
logging.level.com.example.demo=DEBUG

This allows:

  • Less noise globally
  • More detail for your code

6. Log Format Customization

logging.pattern.console=%d{yyyy-MM-dd HH:mm:ss} %-5level %logger - %msg%n

Readable logs = faster debugging.


7. Why Monitoring Is Important

Monitoring tells you:

  • Is the app running?
  • Is it healthy?
  • How many requests?
  • Memory usage?

This is critical in production.


8. Spring Boot Actuator

Spring Boot Actuator provides:

  • Health checks
  • Metrics
  • Application info

Add dependency:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

9. Important Actuator Endpoints

EndpointPurpose
/actuator/healthApp health
/actuator/infoApp info
/actuator/metricsMetrics
/actuator/envEnvironment

10. Exposing Actuator Endpoints

management.endpoints.web.exposure.include=health,info,metrics

Access:

http://localhost:8080/actuator/health

11. Custom Health Indicator

@Component
public class CustomHealthIndicator implements HealthIndicator {

    @Override
    public Health health() {
        boolean healthy = true;

        if (healthy) {
            return Health.up().build();
        }
        return Health.down().withDetail("Error", "Something failed").build();
    }
}

12. Securing Actuator Endpoints

Never expose all actuator endpoints publicly.

management.endpoints.web.exposure.include=health

Secure others via Spring Security.


13. Common Logging & Monitoring Mistakes

❌ Logging sensitive data
❌ Too much DEBUG in production
❌ No health checks
❌ Ignoring logs until failure


14. Best Practices

✔ Log meaningful events
✔ Use proper log levels
✔ Monitor health endpoints
✔ Combine logs + metrics


15. What You Should Understand Before Moving On

You should now know:

  • How logging works in Spring Boot
  • How to configure log levels
  • What Actuator provides
  • How to monitor application health

16. What’s Next?

➡ Article 16: Performance & Caching

  • Why caching matters
  • @Cacheable
  • Improving response time

Type Next when you’re ready.