how to configure log file path to current working target directory in log4j2 xml format

邮差的信 提交于 2019-12-24 02:10:15

问题


Iam migrating my project from log4j 1.2.17 to 2.10.0.

I have following file appender configuartion in 1.2.17 properties format.

log4j.appender.example=org.apache.log4j.FileAppender
log4j.appender.example.file=./target/test-classes/example.log
log4j.appender.example.layout=org.apache.log4j.PatternLayout
log4j.appender.example.layout.ConversionPattern=%m%n

how to configure the above target folder file configuration in log4j2 xml format.

Thanks in advance.


回答1:


Here is an example configuration provided by log4j2 doc

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="warn" name="MyApp" packages="">
  <Appenders>
    // This section represents your properties
    <File name="MyFile" fileName="./target/test-classes/example.log">
      <PatternLayout>
        <Pattern>%m%n</Pattern>
      </PatternLayout>
    </File>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="MyFile"/>
    </Root>
  </Loggers>
</Configuration>

https://logging.apache.org/log4j/2.x/manual/appenders.html#FileAppender




回答2:


You can use a param tag inside appender. Something like

<appender name="example" class="exampleClass">
        .....
       <param name="file" value="./target/test-classes/example.log" />
        .....

</appender>


来源:https://stackoverflow.com/questions/47869975/how-to-configure-log-file-path-to-current-working-target-directory-in-log4j2-xml

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!