Bael 6011: Spring Boot Logback and Log4j2 Extensions (#13327)

This commit is contained in:
Hamid Reza Sharifi
2023-01-23 18:14:23 +03:30
committed by GitHub
parent 0f48c407f4
commit a1eb6e64ba
12 changed files with 180 additions and 1 deletions

View File

@@ -0,0 +1,25 @@
package com.baeldung.extensions;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;
@SpringBootApplication
@PropertySource("classpath:application-log4j2-extensions.properties")
public class SpringBootLog4j2ExtensionsApplication {
private static final Logger logger = LogManager.getLogger(SpringBootLog4j2ExtensionsApplication.class);
public static void main(String[] args) {
SpringApplication.run(SpringBootLog4j2ExtensionsApplication.class, args);
logger.trace("Trace log message");
logger.debug("Debug log message");
logger.info("Info log message");
logger.error("Error log message");
logger.warn("Warn log message");
logger.fatal("Fatal log message");
}
}

View File

@@ -0,0 +1,2 @@
logging.config=classpath:log4j2-spring.xml
spring.application.name=log4j2-extension

View File

@@ -7,6 +7,11 @@
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
<Console name="Console-Extensions" target="SYSTEM_OUT">
<PatternLayout
pattern="%d %p %c{1.} [%t] ${spring:spring.application.name} %m%n" />
</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">
@@ -37,7 +42,20 @@
</Root>
<!-- LOG "com.baeldung*" at TRACE level -->
<Logger name="com.baeldung" level="trace"></Logger>
<SpringProfile name="!development, !production">
<Logger name="com.baeldung" level="trace"></Logger>
</SpringProfile>
<SpringProfile name="development">
<Logger name="com.baeldung.extensions" level="debug"></Logger>
</SpringProfile>
<SpringProfile name="production">
<Logger name="com.baeldung.extensions" level="error">
<AppenderRef ref="Console-Extensions" />
</Logger>
</SpringProfile>
</Loggers>
</Configuration>