1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.apache.log4j.filter;
19
20 import org.apache.log4j.spi.Filter;
21 import org.apache.log4j.spi.LoggingEvent;
22 import org.apache.log4j.spi.OptionHandler;
23 import org.apache.log4j.xml.UnrecognizedElementHandler;
24 import org.w3c.dom.Element;
25
26 import java.util.Properties;
27
28
29 /***
30 * A filter that 'and's the results of any number of contained filters together.
31 *
32 * For the filter to process events, all contained filters must return Filter.ACCEPT.
33 *
34 * If the contained filters do not return Filter.ACCEPT, Filter.NEUTRAL is returned.
35 *
36 * If acceptOnMatch is set to true, Filter.ACCEPT is returned.
37 * If acceptOnMatch is set to false, Filter.DENY is returned.
38 *
39 * Here is an example config that will accept only events that contain BOTH
40 * a DEBUG level AND 'test' in the message:
41 *
42 *<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
43 * <filter class="org.apache.log4j.filter.AndFilter">
44 * <filter class="org.apache.log4j.filter.LevelMatchFilter">
45 * <param name="levelToMatch" value="DEBUG" />
46 * <param name="acceptOnMatch" value="true" />
47 * </filter>
48 * <filter class="org.apache.log4j.filter.StringMatchFilter">
49 * <param name="stringToMatch" value="test" />
50 * <param name="acceptOnMatch" value="true" />
51 * </filter>
52 * <param name="acceptOnMatch" value="false"/>
53 * </filter>
54 * <filter class="org.apache.log4j.filter.DenyAllFilter"/>
55 *<layout class="org.apache.log4j.SimpleLayout"/>
56 *</appender>
57 *
58 * To accept all events EXCEPT those events that contain a
59 * DEBUG level and 'test' in the message:
60 * change the AndFilter's acceptOnMatch param to false and remove the DenyAllFilter
61 *
62 * NOTE: If you are defining a filter that is only relying on logging event content
63 * (no external or filter-managed state), you could opt instead
64 * to use an ExpressionFilter with one of the following expressions:
65 *
66 * LEVEL == DEBUG && MSG ~= 'test'
67 * or
68 * ! ( LEVEL == DEBUG && MSG ~= 'test' )
69 *
70 * XML configuration of this filter requires use of either log4j 1.2.15 or later or
71 * org.apache.log4j.rolling.DOMConfigurator.
72 *
73 * @author Scott Deboy sdeboy@apache.org
74 */
75 public class AndFilter extends Filter implements UnrecognizedElementHandler {
76 Filter headFilter = null;
77 Filter tailFilter = null;
78 boolean acceptOnMatch = true;
79
80 public void activateOptions() {
81 }
82
83 public void addFilter(final Filter filter) {
84 System.out.println("add"+filter);
85 if (headFilter == null) {
86 headFilter = filter;
87 tailFilter = filter;
88 } else {
89 tailFilter.next = filter;
90 }
91 }
92
93 public void setAcceptOnMatch(final boolean acceptOnMatch) {
94 this.acceptOnMatch = acceptOnMatch;
95 }
96 /***
97 * If this event does not already contain location information,
98 * evaluate the event against the expression.
99 *
100 * If the expression evaluates to true, generate a LocationInfo instance
101 * by creating an exception and set this LocationInfo on the event.
102 *
103 * Returns {@link Filter#NEUTRAL}
104 */
105 public int decide(final LoggingEvent event) {
106 boolean accepted = true;
107 Filter f = headFilter;
108 while (f != null) {
109 accepted = accepted && (Filter.ACCEPT == f.decide(event));
110 f = f.next;
111 }
112 if (accepted) {
113 if(acceptOnMatch) {
114 return Filter.ACCEPT;
115 }
116 return Filter.DENY;
117 }
118 return Filter.NEUTRAL;
119 }
120
121 /***
122 * {@inheritDoc}
123 */
124 public boolean parseUnrecognizedElement(final Element element,
125 final Properties props) throws Exception {
126 final String nodeName = element.getNodeName();
127 if ("filter".equals(nodeName)) {
128 OptionHandler filter =
129 org.apache.log4j.extras.DOMConfigurator.parseElement(
130 element, props, Filter.class);
131 if (filter instanceof Filter) {
132 filter.activateOptions();
133 this.addFilter((Filter) filter);
134 }
135 return true;
136 }
137 return false;
138 }
139
140 }