简体   繁体   English

使用主 Arguments 查找的 log4j2 属性替换(应用程序)

[英]log4j2 property substitution using Main Arguments Lookup (Application)

I am trying to follow -我正在尝试遵循-

https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution https://logging.apache.org/log4j/2.x/manual/configuration.html#PropertySubstitution

https://logging.apache.org/log4j/2.x/manual/lookups.html#AppMainArgsLookup https://logging.apache.org/log4j/2.x/manual/lookups.html#AppMainArgsLookup

My main class looks like -我的主要 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
    }

It runs it an argument它运行它一个论点

java -jar MyApp.jar --e "appllication1" 

I want name of my log file as Application1.log我希望我的日志文件名称为 Application1.log

Log4j2.xml looks like - 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>

But somehow substition is not happening here, my log file gets created with name -e.log instead of Application1.log但不知何故这里没有发生替换,我的日志文件是用名称 -e.log 而不是 Application1.log 创建的

11:41:36.293 [main] INFO Runner - Location of Log files is: -e.log 11:41:36.293 [main] INFO Runner - 日志文件的位置是:-e.log

I tried to give -我试着给——

<RollingFile name="RollingFile" fileName="${main:1}.log" filePattern="${main:1}.log">

But this gave me error -但这给了我错误-

main ERROR Unable to create file ${main:1}.log java.io.IOException: The filename, directory name, or volume label syntax is incorrect

I see a similar question - Log4j2 system property written as a file我看到一个类似的问题 - Log4j2 system propertywritten as a file

But somehow this is not working for me.但不知何故,这对我不起作用。

Any pointers are appreciated.任何指针表示赞赏。

Kind Regards,亲切的问候,

A一个

In Log4j2 the :- separator is used to provide default values.在 Log4j2 中, :-分隔符用于提供默认值。 It is described in the documentation you cite : 您引用的文档中对此进行了描述:

Note: Many applications use leading dashes to identify command arguments.注意:许多应用程序使用前导破折号来识别命令 arguments。 Specifying ${main:--file} would result in the lookup failing because it would look for a variable named "main" with a default value of "-file".指定${main:--file}将导致查找失败,因为它将查找名为“main”且默认值为“-file”的变量。 To avoid this the ":" separating the Lookup name from the key must be followed by a backslash as an escape character as in ${main:\--file} .为避免这种情况,将查找名称与键分开的“:”必须后跟一个反斜杠作为转义字符,如${main:\--file}

Hence just replace ${main:--e} with ${main:\--e} and it should work.因此只需将${main:--e}替换为${main:\--e}就可以了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM