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.
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.
- SLF4J (Simple Logging Facade for Java): Spring Boot uses SLF4J with Logback by default. You should use
org.slf4j.Logger. - @Slf4j (Lombok): The easiest way to get a logger. Just put
@Slf4jon your class and uselog.info(). - Actuator Endpoints: Built-in URLs for monitoring.
/actuator/healthreturns{"status":"UP"}. - Management Endpoints: Configured in
application.propertiesto expose them to the network.
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=alwaysThe 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" }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."
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
Want to track your progress?
Log in to save your place and pick up where you left off.
Progress track karna chahte ho?
Login karo apni progress save karne ke liye aur jahan chhoda tha wahan se shuru karo.
Login