Log4J2 not logging anything when reading properties file programmatically

Viewed 744

I have a SpringBoot (2.2.4) / Java 11 application and I try to implement Log4J2 logging using an external configuration file (in the .properties format). The code to initialize the logger is the following:

public static void initLogger(String fileName) {
  checkLoggingPropertiesFile(fileName);

  try (FileInputStream loggingConfigurationProperties = new FileInputStream(fileName)) {
    ConfigurationSource source = new ConfigurationSource(loggingConfigurationProperties, new File(fileName));
    Configurator.initialize(null, source);
  } catch (IOException ioe) {
    System.err.println("log4j2.properties could not be loaded! " + ioe.getCause() + "/" + ioe.getMessage());
  }

}

In the console, I get the following messages:

Set log configuration file config\log4j2.properties
Set log configuration file config\log4j2.properties

The configuration file exists at the file location and it has the following content:

name = MyPropertiesConfiguration
status = warn
monitorInterval = 30

property.basePath = logs

appenders = rolling, console

appender.rolling.type = RollingFile
appender.rolling.name = fileAppender
appender.rolling.fileName = ${basePath}/MyLogFile-${date:yyyyMMdd}.log
appender.rolling.filePattern = ${basePath}/MyLogFile-${date:yyyyMMdd}-%i.log
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n
appender.rolling.policies.type = Policies
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size = 10MB

appender.console.type = Console
appender.console.name = consoleAppender
appender.console.target = SYSTEM_OUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n

loggers = main, springframework

logger.main.name = com.mypackage.main
logger.main.level = debug
logger.main.aditivity = false
logger.main.appenderRef.rolling.ref = fileAppender
logger.main.appenderRef.console.ref = consoleAppender

logger.springframework.name = org.springframework.boot
logger.springframework.level = error
logger.springframework.aditivity = false
logger.springframework.appenderRef.rolling.ref = fileAppender
logger.springframework.appenderRef.console.ref = consoleAppender

rootLogger.level = error
rootLogger.additivity = false
rootLogger.appenderRef.rolling.ref = fileAppender
rootLogger.appenderRef.console.ref = consoleAppender

The problem is that nothing is logged. File MyLogFile-20200312.log is nevertheless created, but it is empty. When I place this configuration file in the classpath, everything works as expected. Can you please point out what am I doing wrong?

1 Answers

After investigating the problem further, it seems the solution is pretty simple to load the configuration file:

Configurator.initialize(null, fileName);

Initially, I didn't think it worked with different file formats than XML.

Related