Log4j 2
  1. Log4j 2
  2. LOG4J2-409

Specifying log4jConfiguration in web.xml fails on Windows when using ${user.home}

    Details

    • Type: Bug Bug
    • Status: Resolved
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 2.0-beta8
    • Fix Version/s: 2.0-rc1
    • Component/s: None
    • Labels:
      None
    • Environment:

      Windows 7, Apache Tomcat 7, Java 7

      Description

      When specifying the context parameter log4jConfiguration in the web.xml of a web application and using ${user.home} as part of the path, e.g.

      <context-param>
          <param-name>log4jConfiguration</param-name>
          <param-value>file://${user.home}/someDirectory/log4j2.xml</param-value>
      </context-param>
      

      the following exception is thrown on Windows systems:

      java.net.URISyntaxException: Illegal character in authority at index 7: file://C:\Users\steinman/someDirectory/log4j2.xml
          at java.net.URI$Parser.fail(Unknown Source)
          at java.net.URI$Parser.parseAuthority(Unknown Source)
          at java.net.URI$Parser.parseHierarchical(Unknown Source)
          at java.net.URI$Parser.parse(Unknown Source)
          at java.net.URI.<init>(Unknown Source)
          at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:46)
          ...
      

      It is because of the backslashes that are not allowed in URIs.

      There is an example in the documentation using backslashes (http://logging.apache.org/log4j/2.x/manual/webapp.html) which will produce the same exception.

      1. LOG4J2-409.patch
        2 kB
        Thomas Neidhart

        Activity

        Hide
        Nick Williams added a comment -

        Thanks for the help identifying the problem and for the patch. Had to change URI creation in several places. Fixed with r1561945.

        Show
        Nick Williams added a comment - Thanks for the help identifying the problem and for the patch. Had to change URI creation in several places. Fixed with r1561945.
        Hide
        Gary Gregory added a comment -

        Thomas' patch looks good. I agree that a patch should not generically fiddle with all properties, only in code that will end up as a URIs. This means there might be more than one place to change in the code...

        Show
        Gary Gregory added a comment - Thomas' patch looks good. I agree that a patch should not generically fiddle with all properties, only in code that will end up as a URIs. This means there might be more than one place to change in the code...
        Hide
        Thomas Neidhart added a comment -

        Attached a simple patch to replace all backslashes with normal slashes.

        Show
        Thomas Neidhart added a comment - Attached a simple patch to replace all backslashes with normal slashes.
        Hide
        Frank Steinmann added a comment -

        Ralph, you are right with the leading slash. Index 7 in the error message is indeed the 'C'. But when I add a slash, I get

        Illegal character in path at index 10: file:///C:\Users\steinman/someDirectory/log4j2.xml

        Now index 10 is the first backslash.

        Show
        Frank Steinmann added a comment - Ralph, you are right with the leading slash. Index 7 in the error message is indeed the 'C'. But when I add a slash, I get Illegal character in path at index 10: file:///C:\Users\steinman/someDirectory/log4j2.xml Now index 10 is the first backslash.
        Hide
        Ralph Goers added a comment - - edited

        I'm not sure whether or not the backslashes have anything to do with this.

        Can you please try

        file:///${user.home}/someDirectory/log4j2.xml

        . Note the extra forward slash. The syntax for a uri is [scheme:][//authority][path]. For an absolute path you need a leading '/' which makes it "/C:path" on Windows.

        Show
        Ralph Goers added a comment - - edited I'm not sure whether or not the backslashes have anything to do with this. Can you please try file: ///${user.home}/someDirectory/log4j2.xml . Note the extra forward slash. The syntax for a uri is [scheme:] [//authority] [path] . For an absolute path you need a leading '/' which makes it "/C:path" on Windows.
        Hide
        Nick Williams added a comment - - edited

        Thanks for reporting this, Frank. Looks like there are two distinct but related problems here. First, the documentation should not show backslashes, because those are indeed illegal in URIs. That's an easy fix. Second, the variable substitution is resulting in an invalid URI (because of the backslashes). This is a trickier problem, because variable substitution has no idea what the semantics of a variable value are. It could be a file path, a person's name, a database connection, or someone's favorite color. We're either going to have to put some context around variable substitution so that it can intelligently replace backslashes with forward slashes, or we're going to have to deal with the problem further down the line before converting values to URIs. I'm going to have to think on this one for a bit.

        Show
        Nick Williams added a comment - - edited Thanks for reporting this, Frank. Looks like there are two distinct but related problems here. First, the documentation should not show backslashes, because those are indeed illegal in URIs. That's an easy fix. Second, the variable substitution is resulting in an invalid URI (because of the backslashes). This is a trickier problem, because variable substitution has no idea what the semantics of a variable value are. It could be a file path, a person's name, a database connection, or someone's favorite color. We're either going to have to put some context around variable substitution so that it can intelligently replace backslashes with forward slashes, or we're going to have to deal with the problem further down the line before converting values to URIs. I'm going to have to think on this one for a bit.

          People

          • Assignee:
            Nick Williams
            Reporter:
            Frank Steinmann
          • Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

            Dates

            • Created:
              Updated:
              Resolved:

              Development