3
3
.
.
1
1
.
.
9
9
L
L
o
o
g
g
4
4
j
j
-
-
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
]
]
[
[
R
R
]
]
[
[
R
R
]
]
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: @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_log4j_config_xml (add Spring Boot Starters from the table)
Edit File: pom.xml (add Log4j dependencies, exclude default logging from Starter)
Create File: log4j2.xml (inside Directory resources)
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>
hello()
MyController
http://localhost:8080/Hello
Browser
log4j2.xml
log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<!-- CONSOLE APPENDER -->
<Console name="MyConsoleAppenderName"> </Console>
<!-- FILE APPENDER -->
<File name="MyFileAppenderName" fileName="logs/File.log">
<PatternLayout pattern="My File Appender: %d %p %c{2} %m %n" />
</File>
<!-- ROLLING FILE APPENDER -->
<RollingFile name = "MyRollingFileAppenderName"
fileName = "logs/RollingFile.log"
filePattern = "logs/$${date:MM-yyyy}/RollingFile_%d{dd.MM.yyyy}_%i.log">
<!-- PATTERN LAYOUT -->
<PatternLayout pattern="My Rolling File Appender: %d %p %c{2} %m %n"/>
<!-- ROLLOVER POLICY (on startup, daily and when the file reaches 10MB) -->
<Policies>
<OnStartupTriggeringPolicy />
<SizeBasedTriggeringPolicy size="10 MB" />
<TimeBasedTriggeringPolicy />
</Policies>
</RollingFile>
</Appenders>
<Loggers>
<!-- LOGGER -->
<Logger name="com.ivoronline.springboot_log_log4j_config_xml.controllers" level="info">
<AppenderRef ref="MyConsoleAppenderName" />
<AppenderRef ref="MyFileAppenderName" />
<AppenderRef ref="MyRollingFileAppenderName"/>
</Logger>
<!-- PREVENT DEFAULT LOGGER (since no appenders are defined) -->
<Root level="info"></Root>
</Loggers>
</Configuration>
MyController.java
package com.ivoronline.springboot_log_log4j_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
Some error occured
Some warn occured
Some info occured
logs/app.log (My File Appender: %d %p %c{2} %m %n)
My File Appender: 2021-03-16 21:56:24,216 ERROR controllers.MyController Some error occured
My File Appender: 2021-03-16 21:56:24,219 WARN controllers.MyController Some warn occured
My File Appender: 2021-03-16 21:56:24,220 INFO controllers.MyController Some info occured
Application Structure
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>