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.
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 version 2.4, Log4j now supports configuration via properties files. Note that the property syntax is NOT the same as the syntax used in Log4j 1.
As of version 2.6, this list of identifiers is no longer required as names are inferred upon first usage, however if you wish to use more complex identifies you must still use the list. If the list is present it will be used.
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
<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorInterval="60">
<Properties>
<Property name="filename">export/home/apps/logs</Property>
</Properties>
<Appenders>
<Console name="STDOUT" target="SYSTEM_OUT">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</Console>
<File name="LOGFILE"
fileName="${filename}/app-frontend.log">
<PatternLayout pattern="[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n"/>
</File>
</Appenders>
<Loggers>
<Logger name="guru.springframework.blog.log4j2properties" level="debug">
<AppenderRef ref="LOGFILE"
level="debug" />
</Logger>
<Root level="debug">
<AppenderRef ref="STDOUT"/>
</Root>
</Loggers>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With