[ 
https://issues.apache.org/jira/browse/LOG4J2-1313?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=15484058#comment-15484058
 ] 

Remko Popma commented on LOG4J2-1313:
-------------------------------------

(Debugging out loud)
What is currently happening is that given this configuration:
{noformat}
<Configuration status="ERROR">
  <Properties>
    <Property name="emptyElementKey" />
    <Property name="emptyAttributeKey" value="" />
    <Property name="emptyAttributeKey2" value=""></Property>
    <Property name="elementKey">elementValue</Property>
    <Property name="attributeKey" value="attributeValue" />
    <Property name="attributeWithEmptyElementKey" 
value="attributeValue2"></Property>
    <Property name="bothElementAndAttributeKey" 
value="attributeValue3">elementValue3</Property>
  </Properties>
  <Appenders>
    <List name="List">
      <PatternLayout 
pattern="1=${sys:elementKey},2=${sys:emptyElementKey},a=${sys:emptyAttributeKey},b=${sys:emptyAttributeKey2},3=${sys:attributeKey},4=${sys:attributeWithEmptyElementKey},5=${sys:bothElementAndAttributeKey},m=%m"
 />
    </List>
  </Appenders>
...
{noformat}

Properties internally become represented as the following StrSubstitutor in the 
Configuration object:
{noformat}
subst = {StrSubstitutor@1473} "StrSubstitutor({date, ctx, main, env, sys, sd, 
java, marker, jndi, jvmrunargs, map, bundle, log4j})"
 escapeChar = '$' 36
 prefixMatcher = {StrMatcher$StringMatcher@3436} 
"org.apache.logging.log4j.core.lookup.StrMatcher$StringMatcher@2667f029 [$, {]"
 suffixMatcher = {StrMatcher$StringMatcher@3437} 
"org.apache.logging.log4j.core.lookup.StrMatcher$StringMatcher@67a20f67 [}]"
 valueDelimiterMatcher = {StrMatcher$StringMatcher@3438} 
"org.apache.logging.log4j.core.lookup.StrMatcher$StringMatcher@57c758ac [:, -]"
 variableResolver = {Interpolator@3289} "{date, ctx, main, env, sys, sd, java, 
marker, jndi, jvmrunargs, map, bundle, log4j}"
  lookups = {HashMap@3444}  size = 13
  defaultLookup = {MapLookup@3445} 
   map = {HashMap@3491}  size = 9
    0 = {HashMap$Node@3494} "elementKey" -> "elementValue"
    1 = {HashMap$Node@3495} "bothElementAndAttributeKey" -> "elementValue3"
    2 = {HashMap$Node@3496} "hostName" -> "933P66151121001"
    3 = {HashMap$Node@3497} "contextName" -> 
"org.apache.logging.log4j.core.config.PropertyTest"
    4 = {HashMap$Node@3498} "attributeWithEmptyElementKey" -> "attributeValue2"
    5 = {HashMap$Node@3499} "attributeKey" -> "attributeValue"
    6 = {HashMap$Node@3500} "emptyAttributeKey2" -> 
    7 = {HashMap$Node@3501} "emptyElementKey" -> "null"  <--- note that the key 
is in the map with defaults but its value is null
    8 = {HashMap$Node@3502} "emptyAttributeKey" -> 
{noformat}

And the PatternLayout object in the Configuration object becomes 
{code}
0 = {PatternFormatter} 
"...[converter=LiteralPatternConverter["literal=1=elementValue,2=${sys:emptyElementKey},a=,b=,3=attributeValue,4=attributeValue2,5=elementValue3,m=",]"
1 = {PatternFormatter} "...[converter=MessagePatternConverter]"
{code}

This is because the StrSubstitutor's VariableResolver will return {{null}} when 
resolving "sys:emptyElementKey". There is no distinction between the 
defaultLookup map containing a null value for a key, or the key not existing at 
all in the defaultLookup map. In both cases the variable is not resolved and 
the layout pattern contains "$\{sys:emptyElementKey}".

-Long story short:- *Conclusion:*
if both the value attribute and the element value are "", PluginValueVisitor 
returns null, and a Property object is created with a {{null}} value. When this 
Property is registered in the defaultLookup map, the null value means the 
property is effectively "undefined". One way to fix this is to modify the 
Property class so that {{Property::getValue}} returns {{""}} if it was 
constructed with a null value. The defaultLookup map will then correctly 
resolve $\{sys:emptyElementKey} to the empty string.

In PluginValueVisitor we can log to the status logger at error level when both 
a "value" attribute and an element value are configured (see below). I don't 
think we should treat this as a fatal error. We should just pick one (the 
element value) and continue processing. In the manual we document that the 
element is used if both a value attribute and an element value are configured.
{code}
    @Override
    public Object visit(final Configuration configuration, final Node node, 
final LogEvent event,
                        final StringBuilder log) {
        final String name = this.annotation.value();
        final String elementValue = node.getValue();
        final String attributeValue = node.getAttributes().get("value");
        String rawValue = null; // if neither is specified, return null
        if (Strings.isNotEmpty(elementValue)) {
            if (Strings.isNotEmpty(attributeValue)) {
                LOGGER.error("Configuration contains {} with both attribute 
value=\"{}\" AND element" +
                                " value \"{}\". Please specify only one value. 
Using the element value.",
                        node.getName(), attributeValue, elementValue);
            }
            rawValue = elementValue;
        } else if (Strings.isNotEmpty(attributeValue)) {
            rawValue = removeAttributeValue(node.getAttributes(), "value");
        }
        final String value = this.substitutor.replace(event, rawValue);
        StringBuilders.appendKeyDqValue(log, name, value);
        return value;
    }
{code}

> <Property name="" value="" /> not working
> -----------------------------------------
>
>                 Key: LOG4J2-1313
>                 URL: https://issues.apache.org/jira/browse/LOG4J2-1313
>             Project: Log4j 2
>          Issue Type: Bug
>    Affects Versions: 2.5
>            Reporter: Philipp Knobel
>            Assignee: Remko Popma
>             Fix For: 2.7
>
>
> The documentation shows examples of the property tag like this 
> ([http://logging.apache.org/log4j/2.x/manual/configuration.html#ConfigurationSyntax]):
> {noformat}
> <Properties>
>     <Property name="name1">value</property>
>     <Property name="name2" value="value2"/>
>   </Properties>
> {noformat}
> When using the later one with the value attribute you'll get on startup this 
> error:
> {noformat}
> ERROR Property contains an invalid element or attribute "value"
> {noformat}
> It would be good to have the attribute support working as well, as otherwise 
> it might happen that IDE auto formatting is line breaking an attribute. If 
> this isn't possible please update the documentation.



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to