Logging and Monitoring in Spring Boot [Java Spring Boot Mastery Series – Part 11]
Monitoring and logging are vital for observing the health and behavior of your Spring Boot application.
📝 1. Logging with Spring Boot (SLF4J + Logback)
Spring Boot uses SLF4J with Logback by default.
Example:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@RestController
public class LoggingController {
private static final Logger logger = LoggerFactory.getLogger(LoggingController.class);
@GetMapping("/log")
public String logExample() {
logger.info("INFO log");
logger.warn("WARN log");
logger.error("ERROR log");
return "Logs printed to console";
}
}
📌 Explanation:
LoggerFactory.getLoggerinitializes logger for the class.logger.info/warn/errorgenerates logs at various levels
⚙️ 2. Customize Logging Level in application.properties
logging.level.org.springframework=INFO
logging.level.com.yourapp=DEBUG
logging.file.name=app.log
Explanation:
- Adjust log levels per package.
- Output to a file
app.log.
🔍 3. Actuator for Monitoring
Spring Boot Actuator provides endpoints for metrics and monitoring.
Add Dependency:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Enable Endpoints in application.properties
management.endpoints.web.exposure.include=health,info,metrics,beans
Access Endpoints:
http://localhost:8080/actuator/health
http://localhost:8080/actuator/metrics
Use Case:
Helpful for Prometheus, Grafana integration.
📦 4. Micrometer Integration
Micrometer provides metrics facade with built-in support for Prometheus, New Relic, etc.
Example with Prometheus:
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
application.properties
management.metrics.export.prometheus.enabled=true
management.endpoints.web.exposure.include=prometheus
📘 What are Prometheus and New Relic?
📊 Prometheus
Prometheus is an open-source systems monitoring and alerting toolkit originally developed by SoundCloud. It is especially well-suited for time-series data (e.g., system metrics like CPU usage over time).
🔧 Key Features:
- Pull-based data collection over HTTP.
- Stores data in a time-series format.
- Powerful query language called PromQL.
- Integrates with Grafana for visualization.
✅ Use Case in Spring Boot:
Used in combination with Micrometer to collect custom application metrics (e.g., number of HTTP requests, execution time of methods).
🧠 New Relic
New Relic is a commercial full-stack observability platform. It provides real-time performance monitoring for applications, infrastructure, and user experience.
🔧 Key Features:
- Auto-instrumentation of JVM-based applications.
- Deep tracing, dashboards, and alerting.
- Supports custom metrics and distributed tracing.
✅ Use Case in Spring Boot:
By adding the New Relic agent to your JVM, you can automatically monitor your app’s memory, database queries, external calls, and more.
➡️ Next Up: Part 12 – Testing in Spring Boot (Unit + Integration)
