BAEL-20875: Move spring-boot-logging-log4j2 into spring-boot-modules
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.graylog;
|
||||
|
||||
import org.apache.log4j.Logger;
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class GraylogDemoApplication {
|
||||
|
||||
private final static Logger LOG =
|
||||
Logger.getLogger(GraylogDemoApplication.class);
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(GraylogDemoApplication.class, args);
|
||||
LOG.info("Hello from Spring Boot");
|
||||
}
|
||||
}
|
||||
@@ -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 through Log4j2...";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.baeldung.springbootlogging;
|
||||
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
//import lombok.extern.log4j.Log4j2;
|
||||
//import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
@RestController("LombokLoggingController")
|
||||
@Slf4j
|
||||
// @CommonsLog (Comment any other Lombok logging annotation and uncomment this
|
||||
// to work with Apache Commons Logging)
|
||||
// @Log4j2 (Comment any other Lombok logging annotation and uncomment this to
|
||||
// work directly with Log4j2)
|
||||
public class LombokLoggingController {
|
||||
|
||||
@GetMapping("/lombok")
|
||||
public String index() {
|
||||
log.trace("A TRACE Message");
|
||||
log.debug("A DEBUG Message");
|
||||
log.info("An INFO Message");
|
||||
log.warn("A WARN Message");
|
||||
log.error("An ERROR Message");
|
||||
return "Howdy! Check out the Logs to see the output...";
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
<?xml version="1.0" encoding="UTF-8" ?>
|
||||
<!DOCTYPE log4j:configuration SYSTEM "http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/doc-files/log4j.dtd">
|
||||
<log4j:configuration>
|
||||
<appender name="stdout" class="org.apache.log4j.ConsoleAppender">
|
||||
<layout class="org.apache.log4j.PatternLayout">
|
||||
<param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${PID:- } [%t] --- %-40.40logger{39} : %m%n"/>
|
||||
</layout>
|
||||
</appender>
|
||||
<appender name="graylog" class="org.graylog2.log.GelfAppender">
|
||||
<param name="graylogHost" value="18.217.253.212"/>
|
||||
<param name="originHost" value="localhost"/>
|
||||
<param name="graylogPort" value="12201"/>
|
||||
<param name="extractStacktrace" value="true"/>
|
||||
<param name="addExtendedInformation" value="true"/>
|
||||
<param name="facility" value="log4j"/>
|
||||
<param name="Threshold" value="DEBUG"/>
|
||||
<param name="additionalFields" value="{'environment': 'DEV', 'application': 'GraylogDemoApplication'}"/>
|
||||
</appender>
|
||||
<root>
|
||||
<priority value="DEBUG"/>
|
||||
<appender-ref ref="stdout"/>
|
||||
<appender-ref ref="graylog"/>
|
||||
</root>
|
||||
</log4j:configuration>
|
||||
@@ -0,0 +1,38 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<Configuration>
|
||||
|
||||
<Appenders>
|
||||
<Console name="Console" target="SYSTEM_OUT">
|
||||
<PatternLayout
|
||||
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
|
||||
</Console>
|
||||
|
||||
<RollingFile name="RollingFile"
|
||||
fileName="./logs/spring-boot-logger-log4j2.log"
|
||||
filePattern="./logs/$${date:yyyy-MM}/spring-boot-logger-log4j2-%d{-dd-MMMM-yyyy}-%i.log.gz">
|
||||
<PatternLayout>
|
||||
<pattern>%d %p %C{1.} [%t] %m%n</pattern>
|
||||
</PatternLayout>
|
||||
<Policies>
|
||||
<!-- rollover on startup, daily and when the file reaches
|
||||
10 MegaBytes -->
|
||||
<OnStartupTriggeringPolicy />
|
||||
<SizeBasedTriggeringPolicy
|
||||
size="10 MB" />
|
||||
<TimeBasedTriggeringPolicy />
|
||||
</Policies>
|
||||
</RollingFile>
|
||||
</Appenders>
|
||||
|
||||
<Loggers>
|
||||
<!-- LOG everything at INFO level -->
|
||||
<Root level="info">
|
||||
<AppenderRef ref="Console" />
|
||||
<AppenderRef ref="RollingFile" />
|
||||
</Root>
|
||||
|
||||
<!-- LOG "com.baeldung*" at TRACE level -->
|
||||
<Logger name="com.baeldung" level="trace"></Logger>
|
||||
</Loggers>
|
||||
|
||||
</Configuration>
|
||||
@@ -0,0 +1,17 @@
|
||||
package org.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import com.baeldung.springbootlogging.SpringBootLoggingApplication;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = SpringBootLoggingApplication.class)
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void whenSpringContextIsBootstrapped_thenNoExceptions() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user