3
3
.
.
1
1
.
.
1
1
U
U
s
s
i
i
n
n
g
g
L
L
o
o
g
g
g
g
e
e
r
r
F
F
a
a
c
c
t
t
o
o
r
r
y
y
I
I
n
n
f
f
o
o
[
[
G
G
]
]
[
[
R
R
]
]
This tutorial shows how to log with Slf4J.
At this point we are not using any additional adjustments, so Slf4J will route our calls to default Logback implementation.
Later if your decide to change underlying logger implementation, for instance to switch from Logback to Log4j, the way
you would use SLF4J to log events would remain the same as described in this tutorial.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: @RequestMapping, Tomcat Server
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_log (add Spring Boot Starters from the table)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
MyController.java
package com.ivoronline.springboot_log.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 {
Logger log = LoggerFactory.getLogger(MyController.class);
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
log.info("Hello from Controller");
return "Hello from Controller";
}
}
hello()
MyController
http://localhost:8080/Hello
Browser
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Console
2021-03-16 08:59:53.267 INFO 14180 - [nio-8080-exec-2] c.i.s.controllers.MyController : Hello from Controller
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>