Log4j 2
  1. Log4j 2
  2. LOG4J2-704

-Dlog4j.configurationFile no longer accepts relative paths

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: 2.0-rc2
    • Fix Version/s: 2.0.1
    • Component/s: Configurators
    • Labels:
      None
    • Environment:

      Ubuntu. Oracle Java 1.7.

      Description

      I just upgraded from beta9 to rc2 and my command line applications now fail on startup.

      I am passing -Dlog4j.configurationFile=conf/log4j2.xml

      The documentation does not state that relative paths are no longer supported but the error indicates that only absolute paths can now be used.

      [java] ERROR StatusLogger Unable to access conf/log4j2.xml java.lang.IllegalArgumentException: URI is not absolute
      [java] at java.net.URI.toURL(URI.java:1095)
      [java] at org.apache.logging.log4j.core.config.ConfigurationFactory.getInputFromUri(ConfigurationFactory.java:265)
      [java] at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:382)
      [java] at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:415)
      [java] at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:146)
      [java] at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:75)
      [java] at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:37)
      [java] at org.apache.logging.log4j.LogManager.getContext(LogManager.java:268)
      [java] at org.apache.logging.slf4j.Log4jLoggerFactory$PrivateManager.getContext(Log4jLoggerFactory.java:98)
      [java] at org.apache.logging.slf4j.Log4jLoggerFactory.getContext(Log4jLoggerFactory.java:84)
      [java] at org.apache.logging.slf4j.Log4jLoggerFactory.getLogger(Log4jLoggerFactory.java:44)
      [java] at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:270)
      [java] at org.slf4j.LoggerFactory.getLogger(LoggerFactory.java:281)

        Activity

        Hide
        Remko Popma added a comment -

        If the specified configuration location could not be found, ConfigurationFactory now reports a better error to the status logger: "File not found in file system or classpath: " + configLocation.

        Fixed in revision 1614390.
        Please verify and close.

        Show
        Remko Popma added a comment - If the specified configuration location could not be found, ConfigurationFactory now reports a better error to the status logger: "File not found in file system or classpath: " + configLocation. Fixed in revision 1614390. Please verify and close.
        Hide
        Remko Popma added a comment -

        Thanks for pointing this out! I did not look at the stack trace above carefully.

        Now that I think about it, we could make this a little user-friendlier by checking if the URI is absolute or not before trying to convert it into a URL (which will fail if URI is relative). If we know we can't turn it into a URL anyway, we can log a FileNotFoundException: configLocation + " not found in file system or in classpath" and return null.

        Show
        Remko Popma added a comment - Thanks for pointing this out! I did not look at the stack trace above carefully. Now that I think about it, we could make this a little user-friendlier by checking if the URI is absolute or not before trying to convert it into a URL (which will fail if URI is relative). If we know we can't turn it into a URL anyway, we can log a FileNotFoundException: configLocation + " not found in file system or in classpath" and return null.
        Hide
        Scott Harrington added a comment -

        I stumbled across this.

        Of course you can specify relative paths – this has always worked and still works.

        The problem is if the file does not exist, then the ConfigurationFactory.getInputFromUri method falls back to a classpath search, and if that fails then it falls back to URI.toURL().openStream() which is what emits the unhelpful error.

        Looks kinda tricky to untangle. For now I would simply warn users to interpret the "Unable to access foo.xml // IllegalArgumentException: URI is not absolute" as simply a File Not Found message.

        Show
        Scott Harrington added a comment - I stumbled across this. Of course you can specify relative paths – this has always worked and still works. The problem is if the file does not exist, then the ConfigurationFactory.getInputFromUri method falls back to a classpath search, and if that fails then it falls back to URI.toURL().openStream() which is what emits the unhelpful error. Looks kinda tricky to untangle. For now I would simply warn users to interpret the "Unable to access foo.xml // IllegalArgumentException: URI is not absolute" as simply a File Not Found message.
        Hide
        Remko Popma added a comment -

        Andy, we are thinking about doing a maintenance release soon. If you have a sample project that demonstrates the issue I can try to take a look at this.

        Show
        Remko Popma added a comment - Andy, we are thinking about doing a maintenance release soon. If you have a sample project that demonstrates the issue I can try to take a look at this.
        Hide
        Remko Popma added a comment -

        Andy, have you had a chance to take another look at this?

        Show
        Remko Popma added a comment - Andy, have you had a chance to take another look at this?
        Hide
        Remko Popma added a comment -

        I tried but I am unable to reproduce the issue.
        Can you provide a small project that demonstrates the problem?

        Show
        Remko Popma added a comment - I tried but I am unable to reproduce the issue. Can you provide a small project that demonstrates the problem?
        Hide
        Remko Popma added a comment - - edited

        I was worried that my changes for LOG4J2-539 caused this issue but when looking closely I don't think that is the case.

        Also, if the file "conf/log4j2.xml" exists and is readable, this error should not happen. Either a file relative to the current location in the file system or (as Ralph mentioned) putting conf/log4j2.xml in the classpath should work.

        Do you see any other WARN or ERROR messages before this error occurs? If not, can you add system property -Dlog4j2.StatusLogger.level=TRACE and let us know if you see any configuration-related error messages?

        Show
        Remko Popma added a comment - - edited I was worried that my changes for LOG4J2-539 caused this issue but when looking closely I don't think that is the case. Also, if the file "conf/log4j2.xml" exists and is readable, this error should not happen. Either a file relative to the current location in the file system or (as Ralph mentioned) putting conf/log4j2.xml in the classpath should work. Do you see any other WARN or ERROR messages before this error occurs? If not, can you add system property -Dlog4j2.StatusLogger.level=TRACE and let us know if you see any configuration-related error messages?
        Hide
        Remko Popma added a comment - - edited

        This may be related to the changes I made for LOG4J2-539.
        (But it would be good to know if this is a web app or not).

        Show
        Remko Popma added a comment - - edited This may be related to the changes I made for LOG4J2-539 . (But it would be good to know if this is a web app or not).
        Hide
        Ralph Goers added a comment -

        Is this a standalone app or in a web container? I don't believe we disabled relative paths, per se, but I don't know that they were ever enabled either. If conf/log4j2.xml is in the classpath then it would work.

        Show
        Ralph Goers added a comment - Is this a standalone app or in a web container? I don't believe we disabled relative paths, per se, but I don't know that they were ever enabled either. If conf/log4j2.xml is in the classpath then it would work.
        Hide
        Andy Grove added a comment -

        You are correct. This must have been from when I tried rc1. I have now updated the stack trace with the one from rc2.

        Should I be able to use relative paths with -Dlog4j.configurationFile? If not then then I'll change this to a documentation bug.

        Thanks.

        Show
        Andy Grove added a comment - You are correct. This must have been from when I tried rc1. I have now updated the stack trace with the one from rc2. Should I be able to use relative paths with -Dlog4j.configurationFile? If not then then I'll change this to a documentation bug. Thanks.
        Hide
        Gary Gregory added a comment -

        That does NOT look like a 2.0-rc2 stack trace.

        ConfigurationFactory.getInputFromURI() was renamed to ConfigurationFactory.getInputFromUri() before rc2.

        Verify your classpath and try again.

        Show
        Gary Gregory added a comment - That does NOT look like a 2.0-rc2 stack trace. ConfigurationFactory.getInputFromURI() was renamed to ConfigurationFactory.getInputFromUri() before rc2. Verify your classpath and try again.

          People

          • Assignee:
            Remko Popma
            Reporter:
            Andy Grove
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development