Chapter 9.2☕ 14 min read

Logging (SLF4J) & Spring Boot Actuator

When your app is running on a live server and a user complains it's "slow", how do you know what's wrong? You need to see the app's internal temperature. <strong>Logging</strong> is the app's diary, and <strong>Actuator</strong> is the app's health dashboard.

01The Concept: Monitoring

The HITEC City IT Park Admin Dashboard Analogy:

An IT Park manager doesn't walk up to every server to check if it's running. They sit in a control room looking at a dashboard showing power usage, AC status, and fire alarms. If something goes wrong, an alarm flashes (Logging), and they check the dashboard (Actuator) to see the exact status of Building 4.

Spring Boot Actuator is that dashboard. It exposes HTTP endpoints (like /actuator/health) that tell you if the database is connected, how much memory is left, and if all microservices are up.

02Technical Explanation
  1. SLF4J (Simple Logging Facade for Java): Spring Boot uses SLF4J with Logback by default. You should use org.slf4j.Logger.
  2. @Slf4j (Lombok): The easiest way to get a logger. Just put @Slf4j on your class and use log.info().
  3. Actuator Endpoints: Built-in URLs for monitoring. /actuator/health returns {"status":"UP"}.
  4. Management Endpoints: Configured in application.properties to expose them to the network.
03Full Working Code: Logs and Health Checks

Add the Actuator dependency.

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

application.properties

# Expose all actuator endpoints over HTTP (For dev only, restrict in prod!)
management.endpoints.web.exposure.include=health,info,metrics
# Show detailed health info (like DB connection status)
management.endpoint.health.show-details=always

The Service Layer (BookService.java)

package com.devinhyderabad;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Service
@Slf4j // Lombok injects the SLF4J logger automatically
public class BookService {

public void processBook(String bookName) {
// Using standard SLF4J logging levels
log.info("Processing started for book: {}", bookName);

try {
// ... business logic ...
log.debug("Successfully processed the book."); // Won't show by default
} catch (Exception e) {
log.error("Failed to process book: {}", bookName, e);
}
}
}

If you start your app and go to http://localhost:8080/actuator/health, you will see:

{ "status": "UP" }
04Why It Matters

Interview Question: "How do you monitor the health of a Spring Boot application in production?"

Answer: "I use Spring Boot Actuator. I expose the /actuator/health endpoint to integrate with AWS load balancers or Kubernetes liveness probes. For metrics, I expose /actuator/metrics and usually integrate it with Prometheus and Grafana to visualize JVM memory and CPU usage in real-time."

05Interview Note

Enterprise Note: Never expose all Actuator endpoints (like /env or /heapdump) to the public internet in production. They contain sensitive data like database passwords. Secure them using Spring Security and restrict them to internal admin networks.

Key Takeaways

  • ✅ SLF4J with Logback is Spring Boot's default logging framework
  • ✅ @Slf4j from Lombok injects a Logger automatically
  • ✅ Actuator exposes /actuator/health for liveness and readiness probes
  • ✅ Never expose /env or /heapdump to the public internet