简体   繁体   中英

How to get log4j2.properties file to read filename from pom.xml in Spring Boot

I been searching internet and i might have missed. But what i am trying to achieve in my code is, have a log4j2.properties file read file names defined in pom.xml in a Spring boot project. Something like below.

pom.xml:

<properties>
 <log.file>/expo/net/logs/xol/aws.log</log.file>
 <status.file>/export/net/logs/xol/tdlg.log</status.file>
</properties>

log4j2.properties:

appender.main.type=RollingFile
appender.main.name=MAIN
#appender.main.fileName=${log.file}
appender.main.filePattern=${log.file}.%d{yyyyMMddHH}
appender.main.layout.type=PatternLayout
appender.main.layout.pattern=%d{MM/dd/yyyy HH:mm:ss.SSS} %-5p  %highlight{%t}  %replace{%msg}{\n\r|\n|\r}{ }%n
appender.main.policies.type=Policies
appender.main.policies.time.type=TimeBasedTriggeringPolicy
appender.main.policies.time.interval=1
appender.main.policies.time.modulate=true

My expectation is when run the maven correct values should get replaced in. Any suggestions where i am going wrong?

I think i found the solution. Someone had asked similar question.

Maven resource filtering not working - because of spring boot dependency

It is explained on below link

https://docs.spring.io/spring-boot/docs/current/reference/html/howto-properties-and-configuration.html

In my log4j2.properties file i replaced "${" and "}" with "@".

appender.main.type=RollingFile
appender.main.name=MAIN
#appender.main.fileName=@log.file@
appender.main.filePattern=@log.file@.%d{yyyyMMddHH}
appender.main.layout.type=PatternLayout
appender.main.layout.pattern=%d{MM/dd/yyyy HH:mm:ss.SSS} %-5p  %highlight{%t}  %replace{%msg}{\n\r|\n|\r}{ }%n
appender.main.policies.type=Policies
appender.main.policies.time.type=TimeBasedTriggeringPolicy
appender.main.policies.time.interval=1
appender.main.policies.time.modulate=true

Worked like a charm. Thanks guys for all the inputs.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM