I have used log4j2 with springboot, log file has been created but logs are not written in file.
log4j2.properties
name=PropertiesConfig
property.filename = /export/home/apps/logs
appenders = console, file
appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.file.type = File
appender.file.name = LOGFILE
appender.file.fileName=${filename}/app-frontend.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
loggers=file
logger.file.name=guru.springframework.blog.log4j2properties
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = LOGFILE
rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = STDOUT
pom.xml
<dependencies>
<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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
</dependencies>
method using logger
private static Logger logger = LogManager.getLogger();
@RequestMapping(value="/check", method = RequestMethod.GET)
public String healthCheck() {
logger.debug("Health-check invoked");
return "Hey, I am fine";
}
Above I have mention the codes I have used. Still can not find a way to solve. Logs are even not appearing in the console.
In log4j2.xml we should say the name of
getLogger("String")
to listen and write to file.I tried in multiple ways, but log4j2.properties is not working. I tried with so many versions of spring-boot(upto 1.4.3) & log4j2 combinations and came to conclusion
Here is the sample how I achieved.
build.gradle
dependencies {
}
log4j2.yml
place this file inside src->main->resources
Please rename your logger.file.name with your own property.
Usage of "log4j2.properties" for configuration in spring-boot has certain limitations. Log4J 2 did not support configuration through the properties file when it was initially released. It was from Log4J 2.4 that support for the properties file was again added, but with a completely different syntax.As mentioned in the documentation
As of spring boot release 1.4.0, the log4j2 api version used is 2.6.2. Note that spring-boot uses the slf4j api to support multiple underlying Logging Framework. There seems to be an issue when using properties based configuration for log4j2 without juggling with classpath dependencies for slf4j bindings.
It would make sense to use the XML (or yaml/json) based configuration to achieve the same and to enable the usage of all the features log4j2 is capable of.
Here is an xml based configuration for the same properties.
log4j2.xml