本文介绍了在Spring Boot中,如何获取log4j2.properties文件以从pom.xml读取文件名的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在搜索互联网,但我可能错过了.但是我想在我的代码中实现的是,让一个log4j2.properties文件读取Spring引导项目中pom.xml中定义的文件名.像下面这样.

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:

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

我的期望是在运行Maven时应替换正确的值.有什么建议我要去哪里吗?

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资源过滤不起作用启动依赖项

在下面的链接中说明

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

在我的log4j2.properties文件中,我用"@"替换了"$ {"和}".

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.

这篇关于在Spring Boot中,如何获取log4j2.properties文件以从pom.xml读取文件名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-20 04:32