Dependencies
Enabling the Spring Boot application to use Log4j2
<!-- Exclude Spring Boot's Default Logging -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<!-- Add Log4j2 Dependency -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Log4j Configuration
Add to classpath (or into src/main/resources folder) one of the following files:
log4j2-spring.xmllog4j2.xmllog4j2.properties
See Log4j2 for more configurations
Example log4j-spring.xml file
Click here to expand...
<?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>
Spring Boot Log4j2 Demo - Using Slf4j
Click here to expand...
Using Log4j2 via Slf4j
import org.slf4j.Logger; import org.slf4j.LoggerFactory; @SpringBootApplication public class Application extends SpringBootServletInitializer { private static final Logger log = LoggerFactory.getLogger(Application.class); public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); log.info("Info level log message"); log.debug("Debug level log message"); log.error("Error level log message"); } }see output
2018-06-01T13:55:42.506+0530 INFO Info level log message 2018-06-01T13:55:42.506+0530 DEBUG Debug level log message 2018-06-01T13:55:42.506+0530 ERROR Error level log message
Spring Boot Log4j2 Demo - Using Log4j2 Directly
Click here to expand...
Using Log4j2 directly
import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; @SpringBootApplication public class Application extends SpringBootServletInitializer { private static final Logger log = LogManager.getLogger(Application.class); public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); log.info("Info level log message"); log.debug("Debug level log message"); log.error("Error level log message"); } }