Toth Ludovic Andreas Toth Ludovic Andreas - 2 months ago 24
Java Question

Log4j2, no logs in file

I am trying to configure log4j2 within a maven project without a standard structure, but even though the file is created nothing is appended.

I have the following dependencies in the pom.xml

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j-version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<version>${log4j-version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>${log4j-version}</version>
</dependency>

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>${log4j-version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>${jackson-version}</version>
</dependency>

<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>${jackson-version}</version>
</dependency>


I am using the following versions

<properties>
<slf4j-version>1.7.21</slf4j-version>
<log4j-version>2.6.2</log4j-version>
<jackson-version>2.8.2</jackson-version>
</properties>


and the following log4j2.properties

status = info
rootLogger.level = info

name = PropertiesConfig
property.fileName = ./log/log.txt

appender.rolling.type = RollingFile
appender.rolling.name = RollingFile
appender.rolling.fileName = ${fileName}
appender.rolling.filePattern = ./log/log_%d{dd-MM-yy}.txt
appender.rolling.append = true

appender.rolling.layout.type = JSONLayout
appender.rolling.layout.complete = true

appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 1
appender.rolling.policies.time.modulate = true

appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5


Also, because of the project's structure I am setting the property file like this

LoggerContext context = (LoggerContext) LogManager.getContext(false);
File file = new File(CONFIGURATION_DIR + File.separator + "log4j2.properties");

// this will force a reconfiguration
context.setConfigLocation(file.toURI());


Even though the file gets created nothing is appended to the file and I have no errors. Am I missing something, or doing something wrong ?

Answer

It looks like I am missing the following configurations for the log4j2.properties file :

logger.rolling.name = name
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RollingFile

Now it works.