Create Project: springboot_log (add Spring Boot Starters from the table)
Edit File: pom.xml (add Log4j dependencies, exclude default logging from every Starter)
Create Package: controllers (inside main package)
– Create Class: MyController.java (inside controllers package)
pom.xml
<dependencies>
<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>
</dependencies>
MyController.java
package com.ivoronline.springboot_log_log4j.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class MyController {
private static final Logger log = LoggerFactory.getLogger(MyController.class);
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
log.info("Hello from Controller");
return "Hello from Controller";
}
}