krallus commented on PR #1441:
URL: https://github.com/apache/logging-log4j2/pull/1441#issuecomment-2276619307

   Yes, my IDE flags the XML as having errors if I use the XSD at 
https://logging.apache.org/xml/ns/log4j-config-2.xsd when I have any property 
substitution. I think property substitution in Log4j XML configurations is 
probably quite common. I do it in many places. I just provided a simple 
example. Personally, I would be in favour of making all attributes be of type 
`xsd:string`.  Not only is there property substitution to consider, but there 
may be other valid values that the XSD simply will not be aware of. For 
example, in the case of "level", the documentation in the XSD itself has 
documentation, _"NOTE: The Log4j API supports custom levels, the following list 
contains only the standard ones."_. There is still great utility in having an 
XSD both validate the structure of all of the elements and required attributes, 
as well as XML authoring assistance in an IDE, just not for attribute values.
   
   However, there is a way to define an XSD that will grant both IDE guidance 
for attribute values *and* allow for any string at the expense of slightly more 
sophistication in the XSD.  Here is an oversimplified example just to 
demonstrate how this can be done.
   
   Example XSD file that allows a list for IDE guidance but will also validate 
any string. Note the "levelOrSubstitution" type:
   ```xsd
   <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"; 
elementFormDefault="qualified">
   
        <xs:simpleType name="levelOrSubstitution">
                <xs:union>
                        <xs:simpleType>
                                <xs:restriction base="xs:string">
                                        <xs:enumeration value="TRACE" />
                                        <xs:enumeration value="DEBUG" />
                                        <xs:enumeration value="INFO" />
                                        <xs:enumeration value="WARN" />
                                </xs:restriction>
                        </xs:simpleType>
                        <xs:simpleType>
                                <xs:restriction base="xs:string">
                                        <xs:pattern value=".+" />
                                </xs:restriction>
                        </xs:simpleType>
                </xs:union>
        </xs:simpleType>
   
        <!-- Root element definition -->
        <xs:element name="Configuration">
                <xs:complexType>
                        <xs:sequence>
                                <xs:element name="Root">
                                        <xs:complexType>
                                                <xs:sequence>
                                                        <xs:element 
name="AppenderRef">
                                                                <xs:complexType>
                                                                        
<xs:attribute name="ref" type="xs:string" use="required" />
                                                                
</xs:complexType>
                                                        </xs:element>
                                                </xs:sequence>
                                                <xs:attribute name="level" 
type="levelOrSubstitution" use="required" />
                                        </xs:complexType>
                                </xs:element>
                        </xs:sequence>
                </xs:complexType>
        </xs:element>
   
   </xs:schema>
   ```
   
   Valid XML File:
   ```xml
   <?xml version="1.0" encoding="UTF-8"?>
   <Configuration xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance";
        xsi:noNamespaceSchemaLocation="testconfig.xsd">
        <Root 
level="${sys:myapp.root.logging.level:-${sys:rootLogger.level:-WARN}}">
                <AppenderRef ref="FileRfc5424Inspired" />
        </Root>
   </Configuration>
   ```
   
   Eclipse IDE guidance when filling out the XML:
   _(Ignore the first grey line as it is simply an informative artifact that 
did not refresh after I renamed the XSD)_
   ![Eclipse IDE 
guidance](https://github.com/user-attachments/assets/48936de4-e7e9-498c-9eff-2f9bb9b97f8f)
   
   Note that for the `xs:pattern`, I considered using a pattern that would 
match only on `${...}` instead of any string, but that is not general enough. 
Consider `fileName="knownPath/${someDerivedName}.ext"`.
   
   I hope that helps some.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to