Spring boot’s default logging uses Logback which is included as transitive dependency. Spring Boot supports Log4j2 for logging configuration if it is on the classpath.
To configure Log4j2 with Spring Boot2, follow these two simple steps:
1. Log4j2 Maven Dependency
In fact, it is two steps change. First remove spring’s default logging dependency and then add log4j2 dependency.
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> </dependencies>
2. Add log4j2.xml file in resources folder
Spring will automatically configure log4j2 once it finds it’s jar files in classpath. Now you have to add log4j2.xml
or (log4j2.properties
) in src/main/resources
folder. That’s all.
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <Property name="LOG_PATTERN">%d{yyyy-MM-dd'T'HH:mm:ss.SSSZ} %p %m%n</Property> <Property name="APP_LOG_ROOT">c:/temp</Property> </Properties> <Appenders> <Console name="Console" target="SYSTEM_OUT" follow="true"> <PatternLayout pattern="${LOG_PATTERN}" /> </Console> <RollingFile name="appLog" fileName="${APP_LOG_ROOT}/SpringBoot2App/application.log" filePattern="${APP_LOG_ROOT}/SpringBoot2App/application-%d{yyyy-MM-dd}-%i.log"> <PatternLayout pattern="${LOG_PATTERN}" /> <Policies> <SizeBasedTriggeringPolicy size="19500KB" /> </Policies> <DefaultRolloverStrategy max="1" /> </RollingFile> </Appenders> <Loggers> <Logger name="com.howtodoinjava.app" additivity="false"> <AppenderRef ref="appLog" /> <AppenderRef ref="Console" /> </Logger> <Root level="debug"> <AppenderRef ref="Console" /> </Root> </Loggers> </Configuration>
3. Spring boot log4j2 demo
Now add few log statements in application and see the logs in configured appenders e.g. I configured console and file.
package com.howtodoinjava.app; 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.boot.web.servlet.support.SpringBootServletInitializer; import org.springframework.context.ApplicationContext; @SpringBootApplication public class Application extends SpringBootServletInitializer { private static final Logger LOGGER = LogManager.getLogger(Application.class); public static void main(String[] args) { ApplicationContext ctx = SpringApplication.run(Application.class, args); LOGGER.info("Info level log message"); LOGGER.debug("Debug level log message"); LOGGER.error("Error level log message"); } }
Check the 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
Let me know if you face any problem in this spring boot log4j2 example.
Happy Learning !!
I am getting this problem and in the console and Info and error log is showing on console but debug line is not printing on console
In Springboot the default logging level is INFO which means info and higher logging levels be printed, like ERROR, WARN, FATAL etc but lower levels like DEBUG and TRACE wont be printed. Change the logging level to see DEBUG logs
log file is not generated.
Its only printed the log on console
Same here, Just Writes the Logs to the Console but the log file doesnt gets generated.any extra configuration needed. let us know
Same problem here. I am getting the log in console only not in the log file.
Thanks for such nice tutorial. Keep this site updated with new articles. This is my one of the best reference.
Hi Getting below error while starting boot app
ERROR StatusLogger Log4j2 could not find a logging implementation. Please add log4j-core to the classpath. Using SimpleLogger to log to the console…
snippet:
org.springframework.boot
spring-boot-starter-web
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2