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.rule.ExpressionRule;
21 import org.apache.log4j.rule.Rule;
22 import org.apache.log4j.spi.Filter;
23 import org.apache.log4j.spi.LoggingEvent;
24
25
26 /***
27 * A filter supporting complex expressions - supports both infix and postfix
28 * expressions (infix expressions must first be converted to postfix prior
29 * to processing).
30 * <p/>
31 * <p>See <code>org.apache.log4j.chainsaw.LoggingEventFieldResolver.java</code>
32 * for the correct names for logging event fields
33 * used when building expressions.
34 * <p/>
35 * <p>See <code>org.apache.log4j.chainsaw.rule</code> package
36 * for a list of available
37 * rules which can be applied using the expression syntax.
38 * <p/>
39 * <p>See <code>org.apache.log4j.chainsaw.RuleFactory</code> for the symbols
40 * used to activate the corresponding rules.
41 * <p/>
42 * NOTE: Grouping using parentheses is supported -
43 * all tokens must be separated by spaces, and
44 * operands which contain spaces are not yet supported.
45 * <p/>
46 * Example:
47 * <p/>
48 * In order to build a filter that displays all messages with
49 * infomsg-45 or infomsg-44 in the message,
50 * as well as all messages with a level of WARN or higher,
51 * build an expression using
52 * the LikeRule (supports java.util.regex based regular expressions) and the InequalityRule.
53 * <b> ( MSG LIKE infomsg-4[4,5] ) && ( LEVEL >= WARN ) </b>
54 * <p/>
55 * Three options are required:
56 * <b>Expression</b> - the expression to match
57 * <b>ConvertInFixToPostFix</b> - convert from infix to posfix (default true)
58 * <b>AcceptOnMatch</b> - true or false (default true)
59 * <p/>
60 * Meaning of <b>AcceptToMatch</b>:
61 * If there is a match between the value of the
62 * Expression option and the {@link LoggingEvent} and AcceptOnMatch is true,
63 * the {@link #decide} method returns {@link Filter#ACCEPT}.
64 * <p/>
65 * If there is a match between the value of the
66 * Expression option and the {@link LoggingEvent} and AcceptOnMatch is false,
67 * {@link Filter#DENY} is returned.
68 * <p/>
69 * If there is no match, {@link Filter#NEUTRAL} is returned.
70 *
71 * @author Scott Deboy sdeboy@apache.org
72 */
73 public class ExpressionFilter extends Filter {
74 /***
75 * accept on match.
76 */
77 boolean acceptOnMatch = true;
78 /***
79 * Convert in-fix to post-fix.
80 */
81 boolean convertInFixToPostFix = true;
82 /***
83 * Expression.
84 */
85 String expression;
86 /***
87 * Evaluated rule.
88 */
89 Rule expressionRule;
90
91 /***
92 * {@inheritDoc}
93 */
94 public void activateOptions() {
95 expressionRule =
96 ExpressionRule.getRule(expression, !convertInFixToPostFix);
97 }
98
99 /***
100 * Set exp.
101 * @param exp exp.
102 */
103 public void setExpression(final String exp) {
104 this.expression = exp;
105 }
106
107 /***
108 * Get expression.
109 * @return expression.
110 */
111 public String getExpression() {
112 return expression;
113 }
114
115 /***
116 * Set convert in-fix to post-fix.
117 * @param newValue new value.
118 */
119 public void setConvertInFixToPostFix(final boolean newValue) {
120 this.convertInFixToPostFix = newValue;
121 }
122
123 /***
124 * Get in-fix to post-fix conversion setting.
125 * @return true if in-fix expressions are converted to post-fix.
126 */
127 public boolean getConvertInFixToPostFix() {
128 return convertInFixToPostFix;
129 }
130
131 /***
132 * Set whether filter should accept events if they match the expression.
133 * @param newValue if true, accept on match.
134 */
135 public void setAcceptOnMatch(final boolean newValue) {
136 this.acceptOnMatch = newValue;
137 }
138
139 /***
140 * Gets whether filter accepts matching or non-matching events.
141 * @return if true, accept matching events.
142 */
143 public boolean getAcceptOnMatch() {
144 return acceptOnMatch;
145 }
146
147 /***
148 * Determines if event matches the filter.
149 * @param event logging event;
150 * @return {@link Filter#NEUTRAL} is there is no string match.
151 */
152 public int decide(final LoggingEvent event) {
153 if (expressionRule.evaluate(event, null)) {
154 if (acceptOnMatch) {
155 return Filter.ACCEPT;
156 } else {
157 return Filter.DENY;
158 }
159 }
160 return Filter.NEUTRAL;
161 }
162 }