Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can spring-boot application.properties file woking with log4j2.xml configuration?

I want to define high-level file logging in application.properties as a convenience to leverage my log4j2.xml file configuration. By itself my log4j2 config is working fine, however I was hoping to control logging levels and log file and path info from the application.properties file. I have the spring-boot-starter-log4j2 dependency in the application's pom file.

In log4j2.xml I have as one of the properties

<Property name="LOG_FILE">${LOG-DIR}/test.log</Property>

, where LOG-DIR is defined in another (previous) property in the same file. In my application.properties file, I have

logging.file=LOG_FILE 

as a property, plus several level properties such as

logging.level.org.springframework.web=DEBUG

none of these log-related properties as defined in my application.properties file are working to build the corresponding log file. Again, when I simply use log4j2.xml by itself it works fine, but wanted to take advantage of the convenience of application.properties for logging configuration.

Any insights into what I am doing wrong are greatly appreciated. thank you

like image 483
Timothy Clotworthy Avatar asked Mar 14 '19 18:03

Timothy Clotworthy


People also ask

How does Spring Boot apply application properties?

Spring Boot automatically loads the application. properties whenever it starts up. You can dereference values from the property file in your java code through the environment. Put a property in the application.

Where do log4j2 properties go?

We should put log4j2. properties anywhere in the application's classpath. Log4j2 will scan all classpath locations to find out this file and then load it. We have put the file in resources folder.


1 Answers

If I understood your question right, you are looking at Property Substitution feature of log4j2.

You can define logging property in application.properties file like below:

log.file.path=/myDir/logpath 

And then the property(s) lookup defined as Property in log4j2.xml:

<Configuration>  
  <Properties>  
      <property name="LOG_FILE">${bundle:application:log.file.path}</property> 
  </Properties>  

Now all the property can be referred in same log4j2.xml with ${propertyName} notation, which eventually points to values from application.properties file.

like image 141
Amith Kumar Avatar answered Oct 30 '22 00:10

Amith Kumar