前言
在某些特定环境下,需要指定log的存储位置,所以在启动前需要动态的更改log.home这一自定义参数, 例:生产环境直接部署jar包,因为log4j2.xml配置文件设置了默认的log存储,需要通过系统参数修改文件位置。位置经查,Log4j是支持部分自定义参数的。
查看官方文档
在Log4j2的Property Substitution一节,有相关描述
Log4j 2 supports the ability to specify tokens in the configuration as references to properties defined elsewhere. Some of these properties will be resolved when the configuration file is interpreted while others may be passed to components where they will be evaluated at runtime. To accomplish this, Log4j uses variations of Apache Commons Lang‘s StrSubstitutor and StrLookup classes. In a manner similar to Ant or Maven, this allows variables declared as
${name}to be resolved using properties declared in the configuration itself. For example, the following example shows the filename for the rolling file appender being declared as a property.
基本意思就是可以通过指定的引用标记做属性替换,具体属性如下:
在表中,sys参数能够解决当前问题,按照示例的格式修改配置文件,指定默认的位置是logs文件夹,同时也支持系统参数指定
1 | <Property name="LOG_HOME">${sys:log.home:-logs}</Property> |
然后在jar启动的时候通过-D传入即可, 例:
1 | > java -jar xxx-fat.jar -Dlog.home=newlogs |
到同级目录就会看到newlogs目录和其中的log文件
完整模板
1 |
|
结束!🔚
