1

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?

GP009
  • 11
  • 3
  • May be this link will help you :- http://www.mojohaus.org/properties-maven-plugin/read-project-properties-mojo.html – Ashwani Tiwari Feb 15 '18 at 18:12
  • I think the best thing might be to use filtering... – khmarbaise Feb 15 '18 at 18:13
  • I did try filtering, but doesn't seem to work. I think somehow parent thing messing up everything. org.springframework.boot spring-boot-starter-parent 1.5.9.RELEASE – GP009 Feb 15 '18 at 18:15
  • Make sure that `log4j2.properties` is placed within `resources` folder. Otherwise it should be explicitly provided to maven-resources-plugin to be filtered – Nikolai Shevchenko Feb 15 '18 at 18:19
  • It is in resources folder. But the way Spring-Boot works is different i feel. – GP009 Feb 15 '18 at 18:22

1 Answers1

0

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.

GP009
  • 11
  • 3