From 37ab0a449a29fc6d7fa64ef13cda06f50d9e057e Mon Sep 17 00:00:00 2001 From: Kiran Date: Mon, 16 Mar 2020 23:36:41 +0100 Subject: [PATCH] Added logging to the project using slf4j and log4j2 (#17) * Added logging to the project using slf4j and log4j2 --- pom.xml | 10 ++++++++++ .../TodoApp/api/HelloWorldController.java | 19 +++++++++++++------ .../personal/TodoApp/service/TaskService.java | 15 ++++++++++++--- src/main/resources/log4j2-spring.xml | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+), 9 deletions(-) create mode 100644 src/main/resources/log4j2-spring.xml diff --git a/pom.xml b/pom.xml index bf9c4a4..e666a41 100644 --- a/pom.xml +++ b/pom.xml @@ -31,6 +31,16 @@ org.springframework.boot spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 org.springframework.boot diff --git a/src/main/java/no/rini/personal/TodoApp/api/HelloWorldController.java b/src/main/java/no/rini/personal/TodoApp/api/HelloWorldController.java index c32327c..6c95cc3 100644 --- a/src/main/java/no/rini/personal/TodoApp/api/HelloWorldController.java +++ b/src/main/java/no/rini/personal/TodoApp/api/HelloWorldController.java @@ -2,12 +2,13 @@ import no.rini.personal.TodoApp.model.Task; import no.rini.personal.TodoApp.service.TaskService; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; -import java.util.ArrayList; @RestController @RequestMapping("/api") @@ -16,38 +17,44 @@ public class HelloWorldController { @Autowired TaskService taskService; - ArrayList tasks = new ArrayList<>(); + Logger logger = LoggerFactory.getLogger(HelloWorldController.class); @GetMapping(value = "/sayHello/{name}", produces = "application/json") public ResponseEntity sayHello(@PathVariable("name") String name) { + logger.info("Invoked sayHello Method"); return ResponseEntity.ok("Hello " + name); } @GetMapping(value = "/getallTasks", produces = "application/json") public ResponseEntity getallTasks() { + logger.info("Invoked getallTasks Method"); return ResponseEntity.ok(taskService.getallTasks()); } @GetMapping(value = "/getTask/{id}", produces = "application/json") public ResponseEntity getTask(@PathVariable("id") Integer id) { + logger.info("Invoked getTask Method"); return ResponseEntity.ok(taskService.getTask(id)); } @PostMapping(value = "/addTask", produces = "application/json", consumes = "application/json") public ResponseEntity addTask(@RequestBody Task task) { + logger.info("Invoked addTask Method"); return ResponseEntity.ok(taskService.addTask(task)); } @PutMapping("/editTask/{id}") public ResponseEntity editTask(@Valid @RequestBody Task editTask, @PathVariable("id") Integer id) { - tasks.removeIf((Task task) -> editTask.getId().equals(id)); - tasks.add(editTask); - return ResponseEntity.ok(taskService.editTask(editTask,id)); + logger.info("Invoked editTask Method"); + taskService.getallTasks().removeIf((Task task) -> editTask.getId().equals(id)); + taskService.addTask(editTask); + return ResponseEntity.ok(taskService.editTask(editTask, id)); } @DeleteMapping("/deleteTask/{id}") public ResponseEntity deleteTask(@PathVariable("id") Integer id) { - tasks.removeIf((task) -> (task.getId().equals(id))); + logger.info("Invoked deleteTask Method"); + taskService.getallTasks().removeIf(task -> (task.getId().equals(id))); return ResponseEntity.ok(taskService.deleteTask(id)); } diff --git a/src/main/java/no/rini/personal/TodoApp/service/TaskService.java b/src/main/java/no/rini/personal/TodoApp/service/TaskService.java index 3106499..c5915eb 100644 --- a/src/main/java/no/rini/personal/TodoApp/service/TaskService.java +++ b/src/main/java/no/rini/personal/TodoApp/service/TaskService.java @@ -2,6 +2,8 @@ import no.rini.personal.TodoApp.model.Task; import no.rini.personal.TodoApp.repository.TaskRepository; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; @@ -15,34 +17,41 @@ public class TaskService { @Autowired private TaskRepository taskRepository; + Logger logger = LoggerFactory.getLogger(TaskService.class); + ArrayList tasks = new ArrayList<>(); public List getallTasks() { + logger.info("Invoked the getallTasks in Service"); return taskRepository.findAll(); } public Task getTask(Integer id) { + logger.info("Invoked the getTask in Service"); Optional foundTask = taskRepository.findById(id); return foundTask.orElse(null); } public String addTask(Task task) { + logger.info("Invoked the addTask in Service"); taskRepository.saveAndFlush(task); return "Task Added"; } public String editTask(Task editTask, Integer id) { - if(taskRepository.existsById(id)){ + logger.info("Invoked the editTask in Service"); + + if (taskRepository.existsById(id)) { taskRepository.deleteById(id); taskRepository.saveAndFlush(editTask); return "Task edited"; - } - else + } else return "Task does not exist"; } public String deleteTask(Integer id) { + logger.info("Invoked the deleteTask in Service"); taskRepository.deleteById(id); return "Task deleted"; } diff --git a/src/main/resources/log4j2-spring.xml b/src/main/resources/log4j2-spring.xml new file mode 100644 index 0000000..1ca0e13 --- /dev/null +++ b/src/main/resources/log4j2-spring.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + +