Skip to content

Commit

Permalink
Added logging to the project using slf4j and log4j2 (#17)
Browse files Browse the repository at this point in the history
* Added logging to the project using slf4j and log4j2
  • Loading branch information
skirankumars31 authored Mar 16, 2020
1 parent 33184d4 commit 37ab0a4
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 9 deletions.
10 changes: 10 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,16 @@
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -16,38 +17,44 @@ public class HelloWorldController {
@Autowired
TaskService taskService;

ArrayList<Task> tasks = new ArrayList<>();
Logger logger = LoggerFactory.getLogger(HelloWorldController.class);

@GetMapping(value = "/sayHello/{name}", produces = "application/json")
public ResponseEntity<String> 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<String> 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));
}

Expand Down
15 changes: 12 additions & 3 deletions src/main/java/no/rini/personal/TodoApp/service/TaskService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -15,34 +17,41 @@ public class TaskService {
@Autowired
private TaskRepository taskRepository;

Logger logger = LoggerFactory.getLogger(TaskService.class);

ArrayList<Task> tasks = new ArrayList<>();

public List<Task> getallTasks() {
logger.info("Invoked the getallTasks in Service");
return taskRepository.findAll();
}

public Task getTask(Integer id) {
logger.info("Invoked the getTask in Service");
Optional<Task> 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";
}
Expand Down
18 changes: 18 additions & 0 deletions src/main/resources/log4j2-spring.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable"/>
</Console>
</Appenders>

<Loggers>
<!-- LOG everything at INFO level -->
<Root level="warn">
<AppenderRef ref="Console"/>
</Root>
<Logger name="no.rini.personal" level="info"></Logger>
</Loggers>

</Configuration>

0 comments on commit 37ab0a4

Please sign in to comment.