Skip to content

Commit 6656f45

Browse files
andrea-ligiosKevinGilmore
authored andcommitted
BAEL-1783 (eugenp#4545)
* BAEL-1783 * tabs to spaces, artifact id renamed * tabs to spaces * Added module spring-boot-logging-log4j2 * Removed <name> node * @GetMapping instead of the older @RequestMapping * @GetMapping instead of the older @RequestMapping
1 parent 0fd7481 commit 6656f45

8 files changed

Lines changed: 198 additions & 0 deletions

File tree

‎pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@
147147
<module>spring-boot-ops</module>
148148
<module>spring-boot-security</module>
149149
<module>spring-boot-mvc</module>
150+
<module>spring-boot-logging-log4j2</module>
150151
<module>spring-cloud-data-flow</module>
151152
<module>spring-cloud</module>
152153
<module>spring-core</module>
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
.sts4-cache
12+
13+
### IntelliJ IDEA ###
14+
.idea
15+
*.iws
16+
*.iml
17+
*.ipr
18+
19+
### NetBeans ###
20+
/nbproject/private/
21+
/build/
22+
/nbbuild/
23+
/dist/
24+
/nbdist/
25+
/.nb-gradle/
26+
/logs/
27+
/bin/
28+
/mvnw
29+
/mvnw.cmd

‎spring-boot-logging-log4j2/pom.xml‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>spring-boot-logging-log4j2</artifactId>
9+
<version>0.0.1-SNAPSHOT</version>
10+
<packaging>jar</packaging>
11+
<description>Demo project for Spring Boot Logging with Log4J2</description>
12+
13+
<parent>
14+
<groupId>org.springframework.boot</groupId>
15+
<artifactId>spring-boot-starter-parent</artifactId>
16+
<version>2.0.3.RELEASE</version>
17+
<relativePath /> <!-- lookup parent from repository -->
18+
</parent>
19+
20+
<properties>
21+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
22+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
23+
<java.version>1.8</java.version>
24+
</properties>
25+
26+
<dependencies>
27+
<dependency>
28+
<groupId>org.springframework.boot</groupId>
29+
<artifactId>spring-boot-starter-web</artifactId>
30+
<exclusions>
31+
<exclusion>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-logging</artifactId>
34+
</exclusion>
35+
</exclusions>
36+
</dependency>
37+
<dependency>
38+
<groupId>org.springframework.boot</groupId>
39+
<artifactId>spring-boot-starter-log4j2</artifactId>
40+
</dependency>
41+
42+
<dependency>
43+
<groupId>org.springframework.boot</groupId>
44+
<artifactId>spring-boot-starter-test</artifactId>
45+
<scope>test</scope>
46+
</dependency>
47+
</dependencies>
48+
49+
<build>
50+
<plugins>
51+
<plugin>
52+
<groupId>org.springframework.boot</groupId>
53+
<artifactId>spring-boot-maven-plugin</artifactId>
54+
</plugin>
55+
</plugins>
56+
</build>
57+
58+
</project>
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.baeldung.springbootlogging;
2+
3+
import org.apache.logging.log4j.LogManager;
4+
import org.slf4j.Logger;
5+
import org.slf4j.LoggerFactory;
6+
import org.springframework.web.bind.annotation.GetMapping;
7+
import org.springframework.web.bind.annotation.RestController;
8+
9+
@RestController
10+
public class LoggingController {
11+
12+
private final static Logger logger = LoggerFactory.getLogger(LoggingController.class);
13+
14+
@GetMapping("/")
15+
public String index() {
16+
logger.trace("A TRACE Message");
17+
logger.debug("A DEBUG Message");
18+
logger.info("An INFO Message");
19+
logger.warn("A WARN Message");
20+
logger.error("An ERROR Message");
21+
22+
return "Howdy! Check out the Logs to see the output...";
23+
}
24+
25+
private static final org.apache.logging.log4j.Logger loggerNative = LogManager.getLogger(LoggingController.class);
26+
27+
@GetMapping("/native")
28+
public String nativeLogging() {
29+
loggerNative.trace("This TRACE message has been printed by Log4j2 without passing through SLF4J");
30+
loggerNative.debug("This DEBUG message has been printed by Log4j2 without passing through SLF4J");
31+
loggerNative.info("This INFO message has been printed by Log4j2 without passing through SLF4J");
32+
loggerNative.warn("This WARN message been printed by Log4j2 without passing through SLF4J");
33+
loggerNative.error("This ERROR message been printed by Log4j2 without passing through SLF4J");
34+
loggerNative.fatal("This FATAL message been printed by Log4j2 without passing through SLF4J");
35+
return "Howdy! Check out the Logs to see the output printed directly throguh Log4j2...";
36+
}
37+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.baeldung.springbootlogging;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class SpringBootLoggingApplication {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(SpringBootLoggingApplication.class, args);
11+
}
12+
}

‎spring-boot-logging-log4j2/src/main/resources/application.properties‎

Whitespace-only changes.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<Configuration>
3+
4+
<Appenders>
5+
<Console name="Console" target="SYSTEM_OUT">
6+
<PatternLayout
7+
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
8+
</Console>
9+
10+
<RollingFile name="RollingFile"
11+
fileName="./logs/spring-boot-logger-log4j2.log"
12+
filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
13+
<PatternLayout>
14+
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
15+
</PatternLayout>
16+
<Policies>
17+
<!-- rollover on startup, daily and when the file reaches
18+
10 MegaBytes -->
19+
<OnStartupTriggeringPolicy />
20+
<SizeBasedTriggeringPolicy
21+
size="10 MB" />
22+
<TimeBasedTriggeringPolicy />
23+
</Policies>
24+
</RollingFile>
25+
</Appenders>
26+
27+
<Loggers>
28+
<!-- LOG everything at INFO level -->
29+
<Root level="info">
30+
<AppenderRef ref="Console" />
31+
<AppenderRef ref="RollingFile" />
32+
</Root>
33+
34+
<!-- LOG "com.baeldung*" at TRACE level -->
35+
<Logger name="com.baeldung" level="trace"></Logger>
36+
</Loggers>
37+
38+
</Configuration>
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.springbootmvc;
2+
3+
import org.slf4j.Logger;
4+
import org.slf4j.LoggerFactory;
5+
import org.springframework.web.bind.annotation.GetMapping;
6+
import org.springframework.web.bind.annotation.RestController;
7+
8+
@RestController
9+
public class LoggingController {
10+
11+
Logger logger = LoggerFactory.getLogger(LoggingController.class);
12+
13+
@GetMapping("/")
14+
public String index() {
15+
logger.trace("A TRACE Message");
16+
logger.debug("A DEBUG Message");
17+
logger.info("An INFO Message");
18+
logger.warn("A WARN Message");
19+
logger.error("An ERROR Message");
20+
21+
return "Howdy! Check out the Logs to see the output...";
22+
}
23+
}

0 commit comments

Comments
 (0)