0

I know my title is not very barely, I try to be more specific here. I'm using log4j2, And up to now my log folder was: "C:\log"

I want to change the root drive("C:") to where the app installed.

For example: If the jar file located in "d:\apps\jar.jar", The log path will be "d:\log" and if the jar file located in "c:\apps\jar.jar", The log path will be "c:\log"

I tried to change the line in my log4j2.xml (Below) to:

fileName="${myapp.data.dir}/log/log_${date:dd-MM-yyyy_HH-mm-ss}.csv">

And call this function before i initial the logger:

private static void initialLog() {
    Path dllPath = Paths.get(System.getProperty("user.dir"));
    String driveLetter = dllPath.getRoot().toString().replace("\\", "");
    System.setProperty("myapp.data.dir", driveLetter);
}

But its not work. Any Idea?

My Old log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>

  <Appenders>
      <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{dd/MM/yyyy HH:mm:ss:SSS}       %p       %c{1.}       %m%n"/>
    </Console>
    <File name="log" 
          fileName="C:/log/log_${date:dd-MM-yyyy_HH-mm-ss}.csv">
      <PatternLayout pattern="%d{dd/MM/yyyy,HH:mm:ss},%p,%m%n"
                        header="Date,Time,Type,Message%n"      />
      <Filters>
        <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="NEUTRAL" />
        <ThresholdFilter level="fatal" onMatch="DENY" onMismatch="NEUTRAL" />
        <ThresholdFilter level="debug" onMatch="DENY" onMismatch="NEUTRAL" />
        <ThresholdFilter level="error" onMatch="DENY" onMismatch="NEUTRAL" />
        <ThresholdFilter level="warn" onMatch="DENY" onMismatch="NEUTRAL" />
      </Filters>
    </File>

  </Appenders>
  <Loggers>
    <Root level="all">
      <AppenderRef ref="log" />
      <AppenderRef ref="Console" />
    </Root>
  </Loggers>
</Configuration>
Idon89
  • 141
  • 4
  • 16
  • 1
    ;aybe the Problem is that the log4j configuration would be done before the code is running. *But its not work* is not an error description – Jens Sep 04 '18 at 06:42

2 Answers2

2

In your example I see that the relative path of the logs is the same in both cases, so in case you always want to have that structure, you could define the path as:

<File name="log" 
      fileName="../log/log_${date:dd-MM-yyyy_HH-mm-ss}.csv">

If you want to (re)configure the file path in runtime check this answer. In short, after you set up the system property with

 System.setProperty("myapp.data.dir", driveLetter);

you should reconfigure the logger:

org.apache.logging.log4j.core.LoggerContext ctx =
    (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
ctx.reconfigure();

Do keep in mind, in that case you should edit your configuration xml file to use the system property as below:

<File name="log" 
      fileName="${sys:myapp.data.dir}/log/log_${date:dd-MM-yyyy_HH-mm-ss}.csv">

Fun fact: There is a tricky way to use the root drive and build paths:

<Properties>
  <Property name="rootDrive">../../../../../../../../../../../../../../</Property>
</Properties>
<File name="log" 
      fileName="${rootDrive}/your/folder/structure/log/log_${date:dd-MM-yyyy_HH-mm-ss}.csv">

Do keep in mind though that I wouldn't suggest this for any use apart from projects that you'd use only yourself, as it might have unexpected results if the user runs the application from a deeper folder level.

Alexios
  • 123
  • 1
  • 10
0

In the case of log4j2, you can use,

# May change log file path as per the need
property.filename = ${sys:user.dir}/logs/debug.log
Nikhil Katre
  • 2,114
  • 23
  • 22