Search code examples
javalogback

How to specify absolute path within logback.xml?


<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="true">
    <include resource="/opt/logback/logback_mobile.xml"></include>
</configuration>

Could not find resource corresponding to [/opt/logback/logback_mobile.xml]

But the file does exist.

How can I specify an absolute path within logback.xml?


Solution

  • You have three options when including a resource within a logback.xml file:

    As a file:

    To include a file use the file attribute. You can use relative paths but note that the current directory is defined by the application and is not necessarily related to the path of the configuration file.

    As a resource:

    To include a resource, i.e a file found on the class path, use the resource attribute. <include resource="includedConfig.xml"/>

    As a URL:

    To include the contents of a URL use the url attribute. <include url="http://some.host.com/includedConfig.xml"/>

    From the docs.

    So, you can use a file path relative to where the application is running or you can use a classpath reference or you can use a URL buut absolute file paths are not supported.

    To include this file: /opt/logback/logback_mobile.xml I think your options are:

    • Use a relative reference, assuming your application is running from /opt/app/myservice then your include declaration would be: <include resource="../../logback/logback_mobile.xml"></include>.
    • Include /opt/logback/ on your application's classpath and then change the include declaration to <include resource="logback_mobile.xml"></include>.
    • Serve logback_mobile.xml from a web server and then include it by URL.