Log4j 2
  1. Log4j 2
  2. LOG4J2-419

Support default value for missing key in look ups with fallbacking to looking in the properties map

    Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Minor Minor
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: 2.0-rc2
    • Component/s: None
    • Labels:
      None

      Description

      It is more useful and flexible to allow default value in the variable expression itself.
      For example,

      ${var1:-defvalue1},
      ${ctx:request_id:-defvalue2}, etc.
      

      This generic feature has been included in StrSubstitutor of commons-lang3 as well with the following issue:

      The benefit from the feature with LANG-893 is we can use it more widely whenever used in look ups.

        Issue Links

          Activity

          Hide
          Woonsan Ko added a comment -

          Oliver's patch in LOG4J-418 should work more efficiently for map pattern converters, but the goodness of the feature with LANG-893 is we can use it more widely. e.g, file path configurations or routing appender configuration, etc.

          Show
          Woonsan Ko added a comment - Oliver's patch in LOG4J-418 should work more efficiently for map pattern converters, but the goodness of the feature with LANG-893 is we can use it more widely. e.g, file path configurations or routing appender configuration, etc.
          Hide
          Woonsan Ko added a comment -

          As Ralph pointed out in the mailing list, it should still fall back to looking in the properties map if no default value is provided this way.

          Show
          Woonsan Ko added a comment - As Ralph pointed out in the mailing list, it should still fall back to looking in the properties map if no default value is provided this way.
          Hide
          Woonsan Ko added a comment -

          I know StrSubstitutor in log4j2 added more operations to deal with LogEvent argument, so I can post a patch to include the feature of LANG-893.
          I'm open to any other opinions. So please let me know if I missed anything.

          Cheers,

          Woonsan

          Show
          Woonsan Ko added a comment - I know StrSubstitutor in log4j2 added more operations to deal with LogEvent argument, so I can post a patch to include the feature of LANG-893 . I'm open to any other opinions. So please let me know if I missed anything. Cheers, Woonsan
          Hide
          Woonsan Ko added a comment -

          Posting a patch (LOG4J2-419-patch.txt).

          Basically, with this patch, we can append default value for a variable after the delimiter (the default delimiter is ':-'. The delimiter is customizable. See StrSubstitutor's setters).
          For example,

          ${var1:-defval1},
          ${jndi:var2:-defval2}
          

          The basic usages are shown and tested in the following unit tests:

          • StrSubstitutorTest.java and XMLLoggerPropsTest.java

          Also, RoutingAppenderWithJndiTest has been improved to show another default value setting example. This example can be used in the scenario where you want to leave log files per each web application by setting different JNDI context variable in each web application. Because we can use the JNDI context variable with default value, we don't have to even define multiple routes in this kind of use cases, but we can define single route with the variable and default value to leave separate logs for each web application.

          By the way, the log4 configuration (log4j-loggerprops.xml) for XMLLoggerPropsTest.java also has a (global) property as well which have been used and is still working to define a default value.
          The default value can be defined by either (global) property or inline default value after the delimiter.

          If you have both (global) property and inline default value to specify default value for a variable, then the (global) property has precedence currently because the (global) property is used in StrLookup. The precedence order might be debatable later, but this seems okay to me for now.

          Please review the patch.

          Cheers,

          Woonsan

          Show
          Woonsan Ko added a comment - Posting a patch ( LOG4J2-419 -patch.txt). Basically, with this patch, we can append default value for a variable after the delimiter (the default delimiter is ':-'. The delimiter is customizable. See StrSubstitutor's setters). For example, ${var1:-defval1}, ${jndi:var2:-defval2} The basic usages are shown and tested in the following unit tests: StrSubstitutorTest.java and XMLLoggerPropsTest.java Also, RoutingAppenderWithJndiTest has been improved to show another default value setting example. This example can be used in the scenario where you want to leave log files per each web application by setting different JNDI context variable in each web application. Because we can use the JNDI context variable with default value, we don't have to even define multiple routes in this kind of use cases, but we can define single route with the variable and default value to leave separate logs for each web application. By the way, the log4 configuration (log4j-loggerprops.xml) for XMLLoggerPropsTest.java also has a (global) property as well which have been used and is still working to define a default value. The default value can be defined by either (global) property or inline default value after the delimiter. If you have both (global) property and inline default value to specify default value for a variable, then the (global) property has precedence currently because the (global) property is used in StrLookup. The precedence order might be debatable later, but this seems okay to me for now. Please review the patch. Cheers, Woonsan
          Hide
          Ralph Goers added a comment -

          Patch applied in revision 1601546 after modifying to account for changes since the patch was created. Please verify and close

          Show
          Ralph Goers added a comment - Patch applied in revision 1601546 after modifying to account for changes since the patch was created. Please verify and close
          Hide
          Woonsan Ko added a comment -

          Thank you so much for applying the patch!
          Everything seems very fine.

          Cheers,

          Woonsan

          Show
          Woonsan Ko added a comment - Thank you so much for applying the patch! Everything seems very fine. Cheers, Woonsan

            People

            • Assignee:
              Unassigned
              Reporter:
              Woonsan Ko
            • Votes:
              0 Vote for this issue
              Watchers:
              2 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development