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

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

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.

Related

Spring: exclude some properties files from context

suppose we have some jars with properties files with the same key/values.
configA.jar:
log4j.A.properties
configB.jar:
log4j.B.properties
The problem: Spring mixes values from the both properties files. So, how to exclude log4j.A.properties from the context and process only log4j.B.properties?
UPDATE (added some stuff): there is a maven build which produces two jars mentioned above. Here in webapp (applicationContext.xml) following setup:
<util:properties id="propertyConfigurer" location="classpath:common.properties,classpath*:edrive.properties,classpath*:job.properties,classpath*:log4j.B.properties"/>
After the startup Spring mixes both jars and takes random (or the last one) jar and it's log4j.properties. But we need only the log4j.B.properties. How to do that?
try adding the config file to be used in your properties file
logging.config=log4j.B.properties
I resolved the issue by myself. I've upgraded logging facility to Log4j2 with following configuration:
log4j2.component.properties in classpath:
log4j.configurationFile=classpath:log4j2.web.xml
That's it.

#Configuration bean to set spring.application.name

What would be the appropriate way to set spring.application.name in an #Configuration file instead of within an applicaiton.properties file?
Thanks,
Brian
Basically I want to set the application name the same as the
following: myartifiact-myversion myartifact
myversion-SNAPSHOT
From docs , you can use #..# placeholders to refer to properties in pom.xml if you use spring-boot-starter-parent (Normally most spring-boot project already use it) .
So in the application.properties:
spring.application.name=#artifactId#-#version#

Spring Boot - Log4j2 - 2 files are generated

I am using Log4j2 and Spring Boot (1.2.4). Following the documentation (and the log4j2-file.xml as example in spring-boot.jar), here is my configuration
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<File name="Logs" fileName="${sys:LOG_FILE}" append="false">
...
<Logger level="warn">
<AppenderRef ref="Logs"/>
</Logger>
In application.properties file :
logging.file: /var/tmp/logs/mylog.log
As a result, 2 files are generated :
One file named ${sys:LOG_FILE} which remains empty
One file /var/tmp/logs/mylog.log properly filled
I do not understand why the file ${sys:LOG_FILE} is generated.
Any idea ?
Thanks a lot.
I'm using version 1.2.5.RELEASE of Spring Boot (including the starter parent) and I'm seeing the same issue.
My assumption is that Log4j2 tries to initalize the file before Spring Boot loaded the configuration, resulting in an empty file called ${sys:LOG_FILE} or ${sys (on Windows).
One way of avoiding this is to just set the system property (-DLOG_FILE=/var/tmp/logs/mylog.log) and remove logger.file from your configuration.
It seem log4j2.xml is loaded and log file is created before application.properties being loaded. One way to fix this is to change the name of log4j2.xml to something else, for example log4j2-example.xml to present auto loading and then add the following line into application.properties:
logging.config=classpath:log4j2-example.xml
This will ensure the LOG_PATH are loaded before creating logger.
logging.file is just used for default logger configured by spring only.
In this case, your LOG_FILE must be passed into system variable before execute the jar by -DLOG_FILE=/location/of/log.file

How can I suppress the default config location of Spring boot

How can I have Spring Boot only look for config files under the the directories specified by spring.config.location property and not look under the default location as specified in the ConfigFileApplicationListener javadoc.
Setting spring.config.location, causes the ConfigFileApplicationListener to look in both spring.config.location directories and the default locations.
you can use profiles.
in your application.properties you will only have :
spring.profiles.active=other
and have a file in the same folder named application-other.properties where you define your properties.
the default properties can be in a file named application-default.properties and when you want to use it, juste change the value in application.properties to 'default'.
Spring-boot documentation of Profiles

Overriding a few properties for junit test using spring or camel property placeholder in a maven layout

I want to specify only the properties I want to override in a test properties file of the same name in the src/test/resources folder.
A little more detail...
In a maven layout I have a properties file that contains the deployed value to use (eg. input.uri).
src/main/resources/app.properties:
input.uri=jms:topic:in
app.name=foo_bar
This file's properties are loaded into the context.xml file by the property-placeholder:
src/main/resources/META-INF/spring/context.xml:
<context:property-placeholder properties-ref="springProperties"/>
<util:properties id="springProperties" local-override="true" location="classpath:app.properties" />
I have the test properties file with the same name, app.properties, in src/test/resources folder and override the input.uri definition to one that my junit test will use. (note, app.name doesn't change).
src/test/resources/app.properties:
input.uri=seda:in
How would you write the junit test and/or a test context.xml file so that properties are loaded from the src/main/resources/app.properties file, but any properties defined in the src/test/resources/app.properties file override the ones in the src/main/resources/app.properties file? Without it being obvious that you're loading two different files either in the src/main files or src/test junit test files - I want the property placeholder to search the classpath and pick the right values.
You will have to provide a different name though - if both the properties in the main and the test have the same name, the entire properties in one or the other will take effect.
Instead an approach like this has worked for me:
In your src/main/resources/META-INF/spring/context.xml do this:
<context:property-placeholder location="classpath:app.properties" local-override="true" properties-ref="springProperties"/>
<util:properties id="springProperties">
</util:properties>
In a test-context.xml file:
<import resource="classpath:/META-INF/spring/context.xml">
<util:properties id="springProperties"> <!-- or refer to a overriding file -->
<prop key="input.uri">seda.in</prop>
</util:properties>
This will override the properties for you, while maintaining the not overridden values from the original file.

Resources