how to configure log file path to current working

2019-08-23 03:41发布

问题:

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>