From 6656f45f0d08925b92f0269d1de667104ada9ee8 Mon Sep 17 00:00:00 2001 From: Andrea Ligios Date: Sat, 30 Jun 2018 04:43:22 +0200 Subject: [PATCH] BAEL-1783 (#4545) * BAEL-1783 * tabs to spaces, artifact id renamed * tabs to spaces * Added module spring-boot-logging-log4j2 * Removed node * @GetMapping instead of the older @RequestMapping * @GetMapping instead of the older @RequestMapping --- pom.xml | 1 + spring-boot-logging-log4j2/.gitignore | 29 ++++++++++ spring-boot-logging-log4j2/pom.xml | 58 +++++++++++++++++++ .../springbootlogging/LoggingController.java | 37 ++++++++++++ .../SpringBootLoggingApplication.java | 12 ++++ .../src/main/resources/application.properties | 0 .../src/main/resources/log4j2-spring.xml | 38 ++++++++++++ .../springbootmvc/LoggingController.java | 23 ++++++++ 8 files changed, 198 insertions(+) create mode 100644 spring-boot-logging-log4j2/.gitignore create mode 100644 spring-boot-logging-log4j2/pom.xml create mode 100644 spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java create mode 100644 spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java create mode 100644 spring-boot-logging-log4j2/src/main/resources/application.properties create mode 100644 spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml create mode 100644 spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/LoggingController.java diff --git a/pom.xml b/pom.xml index e1b85e27c0..45421969ec 100644 --- a/pom.xml +++ b/pom.xml @@ -147,6 +147,7 @@ spring-boot-ops spring-boot-security spring-boot-mvc + spring-boot-logging-log4j2 spring-cloud-data-flow spring-cloud spring-core diff --git a/spring-boot-logging-log4j2/.gitignore b/spring-boot-logging-log4j2/.gitignore new file mode 100644 index 0000000000..d129c74ec9 --- /dev/null +++ b/spring-boot-logging-log4j2/.gitignore @@ -0,0 +1,29 @@ +/target/ +!.mvn/wrapper/maven-wrapper.jar + +### STS ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache + +### IntelliJ IDEA ### +.idea +*.iws +*.iml +*.ipr + +### NetBeans ### +/nbproject/private/ +/build/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ +/logs/ +/bin/ +/mvnw +/mvnw.cmd diff --git a/spring-boot-logging-log4j2/pom.xml b/spring-boot-logging-log4j2/pom.xml new file mode 100644 index 0000000000..c07c157eee --- /dev/null +++ b/spring-boot-logging-log4j2/pom.xml @@ -0,0 +1,58 @@ + + + 4.0.0 + + com.baeldung + spring-boot-logging-log4j2 + 0.0.1-SNAPSHOT + jar + Demo project for Spring Boot Logging with Log4J2 + + + org.springframework.boot + spring-boot-starter-parent + 2.0.3.RELEASE + + + + + UTF-8 + UTF-8 + 1.8 + + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot + spring-boot-starter-logging + + + + + org.springframework.boot + spring-boot-starter-log4j2 + + + + org.springframework.boot + spring-boot-starter-test + test + + + + + + + org.springframework.boot + spring-boot-maven-plugin + + + + + diff --git a/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java b/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java new file mode 100644 index 0000000000..07763c8c3b --- /dev/null +++ b/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/LoggingController.java @@ -0,0 +1,37 @@ +package com.baeldung.springbootlogging; + +import org.apache.logging.log4j.LogManager; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class LoggingController { + + private final static Logger logger = LoggerFactory.getLogger(LoggingController.class); + + @GetMapping("/") + public String index() { + logger.trace("A TRACE Message"); + logger.debug("A DEBUG Message"); + logger.info("An INFO Message"); + logger.warn("A WARN Message"); + logger.error("An ERROR Message"); + + return "Howdy! Check out the Logs to see the output..."; + } + + private static final org.apache.logging.log4j.Logger loggerNative = LogManager.getLogger(LoggingController.class); + + @GetMapping("/native") + public String nativeLogging() { + loggerNative.trace("This TRACE message has been printed by Log4j2 without passing through SLF4J"); + loggerNative.debug("This DEBUG message has been printed by Log4j2 without passing through SLF4J"); + loggerNative.info("This INFO message has been printed by Log4j2 without passing through SLF4J"); + loggerNative.warn("This WARN message been printed by Log4j2 without passing through SLF4J"); + loggerNative.error("This ERROR message been printed by Log4j2 without passing through SLF4J"); + loggerNative.fatal("This FATAL message been printed by Log4j2 without passing through SLF4J"); + return "Howdy! Check out the Logs to see the output printed directly throguh Log4j2..."; + } +} diff --git a/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java b/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java new file mode 100644 index 0000000000..336997a81e --- /dev/null +++ b/spring-boot-logging-log4j2/src/main/java/com/baeldung/springbootlogging/SpringBootLoggingApplication.java @@ -0,0 +1,12 @@ +package com.baeldung.springbootlogging; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class SpringBootLoggingApplication { + + public static void main(String[] args) { + SpringApplication.run(SpringBootLoggingApplication.class, args); + } +} diff --git a/spring-boot-logging-log4j2/src/main/resources/application.properties b/spring-boot-logging-log4j2/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2 diff --git a/spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml b/spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml new file mode 100644 index 0000000000..b08cd2d22d --- /dev/null +++ b/spring-boot-logging-log4j2/src/main/resources/log4j2-spring.xml @@ -0,0 +1,38 @@ + + + + + + + + + + + %d %p %C{1.} [%t] %m%n + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/LoggingController.java b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/LoggingController.java new file mode 100644 index 0000000000..819ee589fe --- /dev/null +++ b/spring-boot-mvc/src/main/java/com/baeldung/springbootmvc/LoggingController.java @@ -0,0 +1,23 @@ +package com.baeldung.springbootmvc; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +public class LoggingController { + + Logger logger = LoggerFactory.getLogger(LoggingController.class); + + @GetMapping("/") + public String index() { + logger.trace("A TRACE Message"); + logger.debug("A DEBUG Message"); + logger.info("An INFO Message"); + logger.warn("A WARN Message"); + logger.error("An ERROR Message"); + + return "Howdy! Check out the Logs to see the output..."; + } +}