Java Integration
Integrate error tracking into your Java applications
Spring Boot Integration
1. Add Dependencies
<!-- pom.xml -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
2. Create Error Logger Service
// ErrorLogService.java
package com.example.service;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
@Service
public class ErrorLogService {
@Value("${errorlog.api.key}")
private String apiKey;
@Value("${errorlog.security.key}")
private String securityKey;
private final RestTemplate restTemplate = new RestTemplate();
public void logError(Exception exception, Map<String, Object> context) {
try {
String webhookUrl = "https://your-domain.com/api/ingest/" + apiKey;
Map<String, Object> payload = new HashMap<>();
payload.put("message", exception.getMessage());
payload.put("stackTrace", getStackTrace(exception));
if (context != null) {
payload.putAll(context);
}
HttpHeaders headers = new HttpHeaders();
headers.set("X-Security-Key", securityKey);
headers.set("Content-Type", "application/json");
HttpEntity<Map<String, Object>> request =
new HttpEntity<>(payload, headers);
restTemplate.postForEntity(webhookUrl, request, String.class);
} catch (Exception e) {
// Silently fail
e.printStackTrace();
}
}
private String getStackTrace(Exception exception) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
exception.printStackTrace(pw);
return sw.toString();
}
}
3. Global Exception Handler
// GlobalExceptionHandler.java
package com.example.controller;
import com.example.service.ErrorLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class GlobalExceptionHandler {
@Autowired
private ErrorLogService errorLogService;
@ExceptionHandler(Exception.class)
public ResponseEntity<Map<String, String>> handleException(
Exception ex, HttpServletRequest request
) {
Map<String, Object> context = new HashMap<>();
context.put("url", request.getRequestURI());
context.put("method", request.getMethod());
errorLogService.logError(ex, context);
Map<String, String> response = new HashMap<>();
response.put("error", "Internal server error");
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(response);
}
}
4. Configuration
# application.properties
errorlog.api.key=YOUR_API_KEY
errorlog.security.key=YOUR_SECURITY_KEY
5. Manual Error Logging
@RestController
@RequestMapping("/api")
public class UserController {
@Autowired
private ErrorLogService errorLogService;
@GetMapping("/users/{id}")
public ResponseEntity<User> getUser(@PathVariable Long id) {
try {
User user = userService.findById(id);
return ResponseEntity.ok(user);
} catch (Exception e) {
Map<String, Object> context = new HashMap<>();
context.put("userId", id);
errorLogService.logError(e, context);
return ResponseEntity
.status(HttpStatus.INTERNAL_SERVER_ERROR)
.build();
}
}
}
Security: Never commit application.properties with real API keys! Use environment variables in production.