Spring Boot - Log4j2 - 2 files are generated - spring-boot

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

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.

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
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.

Is there a way to include properties file in another properties file?

I'd like to avoid cluttering the application.properties file with lots of things than, in my opinion, would be better in a separate file.
application.properties should be something like
#include module1.properties
#include module1.properties
...
###################################
######### Spring Misc #############
###################################
# Direct log to a log file
logging.file=/tmp/kmp-manager.log
#local listening port
server.port=8082
spring.profiles=nr_dev nr_testing production
spring.profiles.active=production
spring.datasource.platform=postgresql
java.security.egd=file:/dev/./urandom
Is this at all possibile?
If not, what would be a sane way to avoid cluttering?
Spring Boot Spring Boot 2.4 has added a feature for importing
We can now use spring.config.import=developer.properties to import other file. Check this blog post for more details
It's possible in YML file and the configuration are very simple
EXAMPLE:
To include properties of application-DATABASE.yml file in application.yml, use
spring:
profiles:
include: DATABASE
[EDIT - for 2.4.0 and above]
spring.profiles.group.prod=DATABASE
OR
Add the file name in application.properties
spring.config.import=classpath:application-DEV.yml,classpath:application-UDEV.yml,classpath:application-PRO.yml,classpath:application-SBA.yml
spring.config.import: file:${CLOUDJAVA_ROOT}/config/application.yaml
Imports are processed as they are discovered, and are treated as additional documents inserted immediately below the one that declares the import. Values from the imported file will take precedence over the file that triggered the import.
See spring-boot docs: Importing Additional Data

Spring how to load file path of a text file in spring property file

This might be an extremely simple question but I'm really new to spring framework and am just getting my feet wet. I am trying to specify a text file property as part of a bean, the value for which I am specifying from a properties file.
The relevant code is as follows in context.xml file
<bean id="myAssembler"
class="com.pkg.search.myclass.collector.assembler.myAssembler">
<property name="popularUrlsFileName" value="${POPULAR_URLS_FILE}"/>
</bean>
The POPULAR_URLS_FILE is specified in a .properties file as :
POPULAR_URLS_FILE="README.md"
But I am getting an error in the xml file as it says it can't find the file with README.md path. What exactly are we supposed to specify as far as the path is concerned for it to find the text file?
Exact error is "Cannot Resolve File README.md"
Any help would be much appreciated. Thanks in advance!
You have two options, if the file is on the classpath than
POPULAR_URLS_FILE="classpath:path/to/README.md"
or, if not on the classpath, use the file URL syntax
POPULAR_URLS_FILE="file:path/to/README.md"

Spring load parts of one config with different loaders from file and classpath

Is it possible load custom part of config from file and automatically load other part of config from classpath, placed inside jar?
I have command line apllication written on java with spring 2.5.6 framework.
Config of apllication consist from 2 parts:
bigApplicationContext.xml
customConfig.xml with import of bigApplicationContext.xml
bigApplicationContext has references to some beans from customConfig.
I placed bigApplicationContext and default customConfig inside jar. Configs loaded by ClassPathXmlApplicationContext. It's OK
Troubles goes when I want provide additional command line option for my application --pathToCustomConfig
I want to load custom part of config from file and automatically load other part of config from bigApplicationContext, placed inside jar.
Is it possible? Now I have
Configuration problem: Failed to import bean definitions from relative
location
Found solution with using
<import resource="classpath:applicationContext.xml" />
I used
<import resource="applicationContext.xml" />

Resources