Skip to content

Commit 9615793

Browse files
committed
add logging to exception handler and service
1 parent 96d3170 commit 9615793

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

src/main/java/com/taskmanagerapi/exception/GlobalExceptionHandler.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package com.taskmanagerapi.exception;
22

3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
35
import org.springframework.http.HttpStatus;
46
import org.springframework.http.ResponseEntity;
57
import org.springframework.web.bind.MethodArgumentNotValidException;
@@ -14,8 +16,11 @@
1416
@RestControllerAdvice
1517
public class GlobalExceptionHandler {
1618

19+
private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandler.class);
20+
1721
@ExceptionHandler(TaskNotFoundException.class)
1822
public ResponseEntity<Map<String, Object>> handleTaskNotFound(TaskNotFoundException ex) {
23+
log.warn("Task not found: {}", ex.getMessage());
1924
return ResponseEntity.status(HttpStatus.NOT_FOUND).body(
2025
Map.of(
2126
"error", ex.getMessage(),
@@ -26,7 +31,8 @@ public ResponseEntity<Map<String, Object>> handleTaskNotFound(TaskNotFoundExcept
2631
}
2732

2833
@ExceptionHandler(Exception.class)
29-
public ResponseEntity<Map<String, Object>> handleGenericError() {
34+
public ResponseEntity<Map<String, Object>> handleGenericError(Exception ex) {
35+
log.error("Unexpected error: {}", ex.getMessage(), ex);
3036
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(
3137
Map.of(
3238
"error", "An unexpected error occurred",

src/main/java/com/taskmanagerapi/service/TaskService.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import com.taskmanagerapi.model.Task;
66
import com.taskmanagerapi.model.TaskStatus;
77
import com.taskmanagerapi.repository.TaskRepository;
8+
import org.slf4j.Logger;
9+
import org.slf4j.LoggerFactory;
810
import org.springframework.stereotype.Service;
911
import org.springframework.transaction.annotation.Transactional;
1012

@@ -15,6 +17,8 @@
1517
@Service
1618
public class TaskService {
1719

20+
private static final Logger log = LoggerFactory.getLogger(TaskService.class);
21+
1822
private final TaskRepository taskRepository;
1923

2024
public TaskService(TaskRepository taskRepository) {
@@ -23,11 +27,14 @@ public TaskService(TaskRepository taskRepository) {
2327

2428
public TaskResponse addTask(String name, String description) {
2529
Task task = new Task(name, description);
26-
return TaskResponse.from(taskRepository.save(task));
30+
TaskResponse response = TaskResponse.from(taskRepository.save(task));
31+
log.info("Task created: id={}, name={}", response.getId(), response.getName());
32+
return response;
2733
}
2834

2935
public List<TaskResponse> getAllTasks() {
3036
List<Task> tasks = taskRepository.findAll();
37+
log.info("Fetched {} tasks", tasks.size());
3138
return tasks.stream()
3239
.map(TaskResponse::from)
3340
.toList();
@@ -47,6 +54,7 @@ public TaskResponse searchTaskByName(String name) {
4754

4855
public List<TaskResponse> searchTaskByStatus(TaskStatus status) {
4956
List<Task> tasks = taskRepository.findTasksByStatus(status);
57+
log.info("Fetched {} tasks with status={}", tasks.size(), status);
5058
return tasks.stream()
5159
.map(TaskResponse::from)
5260
.toList();
@@ -57,6 +65,7 @@ public TaskResponse completeTask(String id) {
5765
Task task = taskRepository.findById(id)
5866
.orElseThrow(() -> new TaskNotFoundException(id));
5967
task.setStatus(DONE);
68+
log.info("Task completed: id={}", id);
6069
return TaskResponse.from(task);
6170
}
6271

@@ -65,6 +74,7 @@ public TaskResponse deleteTask(String id) {
6574
Task task = taskRepository.findById(id)
6675
.orElseThrow(() -> new TaskNotFoundException(id));
6776
task.setStatus(DELETED);
77+
log.info("Task deleted: id={}", id);
6878
return TaskResponse.from(task);
6979
}
7080

@@ -73,6 +83,7 @@ public TaskResponse undoDelete(String id) {
7383
Task task = taskRepository.findById(id)
7484
.orElseThrow(() -> new TaskNotFoundException(id));
7585
task.setStatus(CREATED);
86+
log.info("Task restored: id={}", id);
7687
return TaskResponse.from(task);
7788
}
7889
}

0 commit comments

Comments
 (0)