[英]Using log4j2 property substitution with properties defined in another file
[英]log4j2 property substitution using Main Arguments Lookup (Application)
我正在尝试遵循-
https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution
https://logging.apache.org/log4j/2.x/manual/lookups.html#AppMainArgsLookup
我的主要 class 看起来像 -
public static void main(String[] args) throws IOException {
--Some Code
MyappArgs jArgs=new MyappArgs();
JCommander MyappCmd=JCommander.newBuilder()
.addObject(jArgs)
.build();
MyappCmd.parse(args);
MainMapLookup.setMainArguments(args);
--Some code
}
它运行它一个论点
java -jar MyApp.jar --e "appllication1"
我希望我的日志文件名称为 Application1.log
Log4j2.xml 看起来像 -
<RollingFile name="RollingFile" fileName="${main:--e}.log" filePattern="${main:--e}.log">
<Policies>
<OnStartupTriggeringPolicy/>
</Policies>
<DefaultRolloverStrategy fileIndex="max" max="10"/>
<PatternLayout pattern="[%d{dd/MMM/yyyy HH:mm:ss.SSS}]> %-5p - %m%n"/>
</RollingFile>
但不知何故这里没有发生替换,我的日志文件是用名称 -e.log 而不是 Application1.log 创建的
11:41:36.293 [main] INFO Runner - 日志文件的位置是:-e.log
我试着给——
<RollingFile name="RollingFile" fileName="${main:1}.log" filePattern="${main:1}.log">
但这给了我错误-
main ERROR Unable to create file ${main:1}.log java.io.IOException: The filename, directory name, or volume label syntax is incorrect
我看到一个类似的问题 - Log4j2 system propertywritten as a file
但不知何故,这对我不起作用。
任何指针表示赞赏。
亲切的问候,
一个
在 Log4j2 中, :-
分隔符用于提供默认值。 您引用的文档中对此进行了描述:
注意:许多应用程序使用前导破折号来识别命令 arguments。 指定
${main:--file}
将导致查找失败,因为它将查找名为“main”且默认值为“-file”的变量。 为避免这种情况,将查找名称与键分开的“:”必须后跟一个反斜杠作为转义字符,如${main:\--file}
。
因此只需将${main:--e}
替换为${main:\--e}
就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.