Logging log created in the same directory as during build

I have created a command line tool that comes as a jar executable. When someone uses my instrument, I don't know where the bank will be located. All logging is done using a log. I want my log file to go to the same directory as the jar file, no matter where the jar is located and no matter what the current directory is.

My current logback.xml files look like this.

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>admintool.log</file>
        <encoder>
            <charset>UTF-8</charset>
            <pattern>%d{"yyyy-MM-dd'T'HH:mm:ss.SSSXXX"} [%thread] %-5level %logger{35} - %msg %n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="FILE" />
    </root>
</configuration>

      

Does anyone know how I can get the journal to go to where the bank is? Thanks in advance.

+3


source to share


1 answer


There is a way to provide values ​​for log configuration properties at runtime:

http://logback.qos.ch/manual/configuration.html#definingPropsOnTheFly

You need to create a class that implements the interface PropertyDefiner

and returns the location of the container



public class MyPropertyDefiner extends PropertyDefinerBase {
    @Override
    public String getPropertyValue() {
        return MyPropertyDefiner.class.getProtectionDomain().getCodeSource().getLocation().getFile();
    }
}

      

In your logback.xml file you can specify something like this

<configuration>
    <define name="jarLocation" class="MyPropertyDefiner"/>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <file>${jarLocation}</file>
        ...
    </appender>
    ...
</configuration>

      

+2


source







All Articles