3
3
.
.
1
1
.
.
4
4
L
L
o
o
g
g
b
b
a
a
c
c
k
k
-
-
C
C
o
o
n
n
f
f
i
i
g
g
u
u
r
r
e
e
-
-
X
X
M
M
L
L
I
I
n
n
f
f
o
o
[
[
G
G
]
]
This tutorial shows how to configure Log4j by creating log4j2.xml file inside resources Directory.
Application Schema [Results]
Spring Boot Starters
GROUP
DEPENDENCY
DESCRIPTION
Web
Spring Web
Enables: Controller Annotations, Tomcat Server
P
P
r
r
o
o
c
c
e
e
d
d
u
u
r
r
e
e
Create Project: springboot_log_logback_config_xml (add Spring Boot Starters from the table)
Create File: logback.xml (inside Directory resources)
Create Package: controllers (inside main package)
Create Class: MyController.java (inside controllers package)
logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<!-- CONSOLE APPENDER -->
<appender name="MyConsoleAppenderName" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>My File Appender: %d %p %c{0} %m %n</Pattern>
</encoder>
</appender>
<!-- FILE APPENDER -->
<appender name="MyFileAppenderName" class="ch.qos.logback.core.FileAppender">
<!-- FILE NAME -->
<file>logs/File.log</file>
<!-- ENCODER -->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>My File Appender: %d %p %c{30} %m %n</Pattern>
</encoder>
</appender>
<!-- ROLLING FILE APPENDER -->
<appender name="MyRollingFileAppenderName" class="ch.qos.logback.core.rolling.RollingFileAppender">
<!-- FILE NAME -->
<file>logs/RollingFile.log</file>
hello()
MyController
http://localhost:8080/Hello
Browser
logback.xml
<!-- ENCODER -->
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>My File Appender: %d %p %c{30} %m %n</Pattern>
</encoder>
<!-- ROLLOVER POLICY (daily and when the file reaches 1MB) -->
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>logs/archived/RollingFile_%d{dd.MM.yyyy}_%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy
class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<!-- LOGGER -->
<logger name="com.ivoronline.springboot_log_logback_config_xml.controllers" level="INFO">
<appender-ref ref="MyConsoleAppenderName" />
<appender-ref ref="MyFileAppenderName" />
<appender-ref ref="MyRollingFileAppenderName"/>
</logger>
<!-- DISABLE DEFAULT LOGGER (since no appenders are defined) -->
<root level="info"> </root>
</configuration>
MyController.java
package com.ivoronline.springboot_log_logback_config_xml.controllers;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class MyController {
Logger log = LoggerFactory.getLogger(MyController.class);
@ResponseBody
@RequestMapping("/Hello")
public String hello() {
log.error("Some error occured");
log.warn ("Some warn occured");
log.info ("Some info occured");
log.debug("Some debug occured");
log.trace("Some trace occured");
return "Hello from Controller";
}
}
R
R
e
e
s
s
u
u
l
l
t
t
s
s
http://localhost:8080/Hello
Console
My File Appender: 2021-03-17 12:17:39,691 ERROR c.i.s.controllers.MyController Some error occured
My File Appender: 2021-03-17 12:17:39,692 WARN c.i.s.controllers.MyController Some warn occured
My File Appender: 2021-03-17 12:17:39,692 INFO c.i.s.controllers.MyController Some info occured
logs/File.log (My File Appender: %d %p %c{2} %m %n)
My File Appender: 2021-03-17 12:17:39,691 ERROR c.i.s.controllers.MyController Some error occured
My File Appender: 2021-03-17 12:17:39,692 WARN c.i.s.controllers.MyController Some warn occured
My File Appender: 2021-03-17 12:17:39,692 INFO c.i.s.controllers.MyController Some info occured
Application Structure
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>