问题描述
我想根据我的应用程序更改log4j2.xml文件的某些属性.
我采用了不同的方法,但我仍然没有得到正确的事情.我想根据环境(DEV,QA或PROD)具有不同的配置.如何完成此操作?
我试图在我的属性中
#Place holders for log4j2.xml file log.file.path=/opt/tomcat/logs log.file.name=dummydummy log.file.size=100 MB log.level=DEBUG
我的log4j2下面.
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <Property name="PID">????</Property> <property name="name">my-log</property> </Properties> <Appenders> <RollingFile name="file" fileName="${log.file.path}${log.file}.log" filePattern="${log.file.path}${log.file}-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %c{1}(%M:%L) : %m%n%wEx" /> <Policies> <TimeBasedTriggeringPolicy /><!-- Rotated everyday --> <SizeBasedTriggeringPolicy size="${log.file.size}" /> <!-- Or every 100 MB --> </Policies> </RollingFile> <Console name="Console" target="SYSTEM_OUT" follow="true"> <PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%t]}{faint} %clr{%c{1}(%M:%L)}{cyan} %clr{:}{faint} %m%n%wEx" /> </Console> </Appenders> <Loggers> <Logger name="org.hibernate.validator.internal.util.Version" level="warn" /> <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" /> <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" /> <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" /> <Logger name="org.springframework.web" level="error" /> <Root level="${log.level}"> <AppenderRef ref="Console" /> <AppenderRef ref="file" /> </Root> </Loggers> </Configuration>
推荐答案
属性查找元素允许从log4j配置中的外部属性文件引用属性. 就您的示例而言,应该是这样的:
-
文件 env.properties 包含以下属性:
log.file.path=/opt/tomcat/logs log.file.name=dummydummy log.file.size=100 MB log.level=DEBUG
属性查找应定义为 log> log4j2.xml 的属性:
<Configuration> <Properties> <property name="log.file.path">${bundle:env:log.file.path}</property> <property name="log.file.name">${bundle:env:log.file.name}</property> <property name="log.file.size">${bundle:env:log.file.size}</property> <property name="log.level">${bundle:env:log.level}</property> </Properties>
现在,属性可以在带有$ {property_name}符号的附录中引用.每个属性参考将与 env.properties .
的实际值插值.您可以找到属性查找的另一个示例/a>.
其他推荐答案
从Log4J 2.13.0 log4j 2开始,现在提供了弹簧查找,作为其弹簧云配置支持的一部分.它将允许您参考您的应用程序中定义的属性.properties或application.yml文件,您的Spring Boot应用程序的log4j2.xml.
其他推荐答案
由于log4j 2.14.0,您现在可以使用弹簧启动环境变量而无需弹簧云,而无需直接引用属性文件.您至少需要Spring Boot 2.0.3
<property name="applicationName">${spring:spring.application.name}</property>
文档: https .org/log4j/2.x/log4j-spring-boot/index.html
maven存储库: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-spring-boot
问题描述
I would like to change some properties from the log4j2.xml file depending on the my application.properties, for example define some properties and then substitute in the log4j2 those properties that are parameters.
I ran different approaches but I still did not get the right thing. I would like to have different configs depending on the environment (DEV, QA or PROD). How to accomplish this?
I'm trying to have this in my properties
#Place holders for log4j2.xml file log.file.path=/opt/tomcat/logs log.file.name=dummydummy log.file.size=100 MB log.level=DEBUG
My log4j2 below.
<?xml version="1.0" encoding="UTF-8"?> <Configuration status="WARN" monitorInterval="30"> <Properties> <Property name="PID">????</Property> <property name="name">my-log</property> </Properties> <Appenders> <RollingFile name="file" fileName="${log.file.path}${log.file}.log" filePattern="${log.file.path}${log.file}-%d{yyyy-MM-dd}-%i.log.gz"> <PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %c{1}(%M:%L) : %m%n%wEx" /> <Policies> <TimeBasedTriggeringPolicy /><!-- Rotated everyday --> <SizeBasedTriggeringPolicy size="${log.file.size}" /> <!-- Or every 100 MB --> </Policies> </RollingFile> <Console name="Console" target="SYSTEM_OUT" follow="true"> <PatternLayout pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%t]}{faint} %clr{%c{1}(%M:%L)}{cyan} %clr{:}{faint} %m%n%wEx" /> </Console> </Appenders> <Loggers> <Logger name="org.hibernate.validator.internal.util.Version" level="warn" /> <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" /> <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" /> <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" /> <Logger name="org.springframework.web" level="error" /> <Root level="${log.level}"> <AppenderRef ref="Console" /> <AppenderRef ref="file" /> </Root> </Loggers> </Configuration>
推荐答案
The properties lookup element allows to refer properties from an external properties file in the log4j configuration. For your example it should be something like this:
A file env.properties contains the following properties:
log.file.path=/opt/tomcat/logs log.file.name=dummydummy log.file.size=100 MB log.level=DEBUG
The properties lookup should be defined as properties of the log4j2.xml:
<Configuration> <Properties> <property name="log.file.path">${bundle:env:log.file.path}</property> <property name="log.file.name">${bundle:env:log.file.name}</property> <property name="log.file.size">${bundle:env:log.file.size}</property> <property name="log.level">${bundle:env:log.level}</property> </Properties>
Now the properties may be referred in appenders with ${property_name} notation. Each property reference will be interpolated with the real value from the env.properties.
You can find another example of properties lookup here.
其他推荐答案
As of Log4j 2.13.0 Log4j 2 now provides a Spring Lookup as part of its Spring Cloud Config support. It will allow you to reference properties defined in your application.properties or application.yml file of your Spring Boot application in the log4j2.xml.
其他推荐答案
Since log4j 2.14.0, you can now use Spring Boot environment variables without Spring Cloud and without doing direct reference to the properties file. You'll need at least Spring Boot 2.0.3
<property name="applicationName">${spring:spring.application.name}</property>
Documentation: https://logging.apache.org/log4j/2.x/log4j-spring-boot/index.html
Maven repository: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-spring-boot