logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From <Alexander.Rat...@materna.de>
Subject AW: Log4j2 Appender attributes with strict xml config
Date Thu, 17 Oct 2013 16:16:03 GMT

I'm not sure how to do implement it the "right" way. What I did in my project was more like
a hack (imho). I wanted quick results that are not so dirty that I should feel bad. Therefore
I reused KeyValuePair and just allowed them to appear in an AppendersType and then put the
name of a KeyValuePair along with its value (or body) to the attributes of the parent node.
KeyValuePair is used as a parameter for some Plugins like MapRewritePolicy, DynamicThresholdFilter
and others.
I'm currently striving towards another solution that would feel more right:
I'd like to create a new XML complexType that just has a name and value - probably not even
a value but a body/extending xs:string (just like PropertyType) and call it AttributeType
(Attribute) *edit* better call it ParameterType (Parameter) because in the documentation they're
referred as Parameters and that's what they are.
We could then configure an Appender like this:

			<Parameter name="target">SYSTEM_ERR</Parameter>
			<Layout Type="PatternLayout">
				<Pattern> %date{dd.MM.yyyy HH:mm:ss,SSS} %C{1}.%M %logger in thread %thread %m %xEx%n</Pattern>
					onMismatch="NEUTRAL" />
					onMismatch="DENY" />
This will work as long as nobody implements a @Plugin with name "Parameter".
I'm eager to read your thoughts on this proposal.

My patch proposal:
(I also edited the current Log4j2-config.xsd to reflect the current implementation on the
ConfigurationType element - and since there are so many attributes I sorted them - and I also
fixed some minOccurs and maxOccurs attributes.)
It's really only a small change if I don't have to hack around but alter the sources directly
I implemented it in getType (which was saved as a name... - final String name = getType(child);)

Index: src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java
--- src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java	(revision 1533024)
+++ src/main/java/org/apache/logging/log4j/core/config/XMLConfiguration.java	(working copy)
@@ -329,6 +329,12 @@
     private String getType(final Element element) {
         if (strict) {
+			if( "parameter".equalsIgnoreCase(element.getTagName()) ) {
+				final String nameValue = element.getAttribute("name");
+				if( nameValue != null ) {
+					return nameValue;
+				}
+			}
             final NamedNodeMap attrs = element.getAttributes();
             for (int i = 0; i < attrs.getLength(); ++i) {
                 final org.w3c.dom.Node w3cNode = attrs.item(i);
Index: src/main/resources/Log4j-config.xsd
--- src/main/resources/Log4j-config.xsd	(revision 1533024)
+++ src/main/resources/Log4j-config.xsd	(working copy)
@@ -20,28 +20,34 @@
     <xs:element name="Configuration" type="ConfigurationType"/>
     <xs:complexType name="ConfigurationType">
-            <xs:element name="Properties" type="PropertiesType"/>
+            <xs:element name="Properties" type="PropertiesType" minOccurs="0"/>
             <xs:choice minOccurs="0" maxOccurs="1">
                 <xs:element name="Filters" type="FiltersType"/>
                 <xs:element name="Filter" type="FilterType"/>
-            <xs:element name="ThresholdFilter" type="ThresholdFilterType"/>
+            <xs:element name="ThresholdFilter" type="ThresholdFilterType" minOccurs="0"/>
             <xs:element name="Appenders" type="AppendersType"/>
             <xs:element name="Loggers" type="LoggersType"/>
+        <xs:attribute name="advertiser" type="xs:string"/>
+        <xs:attribute name="dest" type="xs:string"/>
+        <xs:attribute name="monitorInterval" type="xs:int"/>
+        <xs:attribute name="name" type="xs:string"/>
         <xs:attribute name="packages" type="xs:string"/>
+        <xs:attribute name="schema" type="xs:string"/>
+        <xs:attribute name="shutdownHook" type="xs:boolean"/>
         <xs:attribute name="status" type="xs:string"/>
         <xs:attribute name="strict" type="xs:string"/>
-        <xs:attribute name="name" type="xs:string"/>
-        <xs:attribute name="advertiser" type="xs:string"/>
+        <xs:attribute name="verbose" type="xs:boolean"/>
     <xs:complexType name="PropertiesType">
-            <xs:element name="Property" type="PropertyType"/>
+            <xs:element name="Property" type="PropertyType" minOccurs="1" maxOccurs="unbounded"/>
     <xs:complexType name="AppenderType">
+        	<xs:element name="Parameter" type="ParameterType" minOccurs="0" maxOccurs="unbounded"/>
             <xs:element name="Layout" type="LayoutType" minOccurs="0"/>
             <xs:choice minOccurs="0" maxOccurs="1">
                 <xs:element name="Filters" type="FiltersType"/>
@@ -65,6 +71,13 @@
+    <xs:complexType name="ParameterType">
+        <xs:simpleContent>
+            <xs:extension base="xs:string">
+                <xs:attribute name="name" type="xs:string"/>
+            </xs:extension>
+        </xs:simpleContent>
+    </xs:complexType>
     <xs:complexType name="KeyValuePairType">
             <xs:extension base="xs:string">

The config can then be strict, the general structure of the document validated but one is
still able to use custom Plugins.
Not the ideal world scenario Gary wanted (which I'd appreciate) but still an improvement imho.

Best Regards,

-----Urspr√ľngliche Nachricht-----
Von: Rathai, Alexander 
Gesendet: Donnerstag, 17. Oktober 2013 09:15
An: log4j-user@logging.apache.org
Betreff: AW: Log4j2 Appender attributes with strict xml config


will do as soon as I got some spare time. I already checked out the trunk.
I have to admit that my solution is a bit dirty (imho) and there's a warning when validating
a config that uses KeyValuePairs on Appenders. When I make a diff I'll patch the original
classes and thus make sure that everything is set up nicely.

Best Regards,

-----Urspr√ľngliche Nachricht-----
Von: Remko Popma [mailto:remko.popma@gmail.com]
Gesendet: Mittwoch, 16. Oktober 2013 16:40
An: Log4J Users List
Betreff: Re: Log4j2 Appender attributes with strict xml config

On Wednesday, October 16, 2013, Gary Gregory wrote:

> On Wed, Oct 16, 2013 at 8:48 AM, Remko Popma 
> <remko.popma@gmail.com<javascript:;>>
> wrote:
> > Perfection may be hard to achieve, but if Alexander's suggestions 
> > are an improvement over the current schema, should we add them to 
> > the current
> code
> > base?
> >
> Probably but where are they? It looks like some text was lost in a 
> reply to this thread.
> Gary

Hm, it's a bit hard to do this via mail (but perhaps that's just me...)

Alexander,  would you mind raising a JIRA ticket and appending your modifications to it?
A diff patch file would be ideal.

Best regards,

*snip old conversation*

To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org

View raw message