Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-1140

Variable substitution removes trailing colon

    XMLWordPrintable

Details

    Description

      When using a variable whose value ends with a colon, that colon is removed when the variable is evaluated.

      For example, I have a property file.

      my.properties
      root=C\:
      

      I want to use that variable to configure my logging. Example is abbreviated to highlight the usage.

      logback.xml
      <configuration>
          <property resource="my.properties"/>
          <appender>
              <file>${root}/out.log</file>
          </appender>
      </configuration>
      

      This produces the following log output in the default logs.

      Will use the pattern C/out.log for the active file

      Test case to prove the issue. Using NodeToStringTransformerTest as a base.

      logback-core/src/test/java/ch/qos/logback/core/subst/NodeToStringTransformerTest.java
      @Before
      public void setUp() {
        // existing setup
        propertyContainer0.putProperty("root", "C:");
      }
      
      @Test
      public void trailingColon() throws ScanException {
        String input = "${root}";
        Node node = makeNode(input);
        NodeToStringTransformer nodeToStringTransformer = new NodeToStringTransformer(node, propertyContainer0);
        assertEquals("C:", nodeToStringTransformer.transform());
      }
      

      Test failure output.

      expected:<C[:]> but was:<C[]>

      The workaround is easy enough: don't let properties end in a colon. Looks bad, but works.

      fix.properties
      root=C\:/
      
      logback.xml
      <configuration>
          <property resource="fix.properties"/>
          <appender>
              <file>${root}out.log</file>
          </appender>
      </configuration>
      

      However, this isn't always possible. My application has a ${root} property that is assumed to be the drive/network path only, and all application paths include a leading directory slash. I can create a separate property, but having two different properties that look similar but are used in two different places is bad for maintenance.

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            ecook Eric C
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: