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 * Location information is usually specified at the appender level -
28 * all events associated
29 * with an appender either create and parse stack traces or they do not.
30 * This is an expensive operation and in some cases not needed
31 * for all events associated with an appender.
32 *
33 * This filter creates event-level location information only
34 * if the provided expression evaluates to true.
35 *
36 * For information on expression syntax,
37 * see org.apache.log4j.rule.ExpressionRule
38 *
39 * @author Scott Deboy sdeboy@apache.org
40 */
41 public class LocationInfoFilter extends Filter {
42 /***
43 * Convert to in-fix to post-fix.
44 */
45 boolean convertInFixToPostFix = true;
46 /***
47 * Expression.
48 */
49 String expression;
50 /***
51 * Compiled expression.
52 */
53 Rule expressionRule;
54
55 /***
56 * {@inheritDoc}
57 */
58 public void activateOptions() {
59 expressionRule =
60 ExpressionRule.getRule(expression, !convertInFixToPostFix);
61 }
62
63 /***
64 * Set expression.
65 * @param exp expression.
66 */
67 public void setExpression(final String exp) {
68 this.expression = exp;
69 }
70
71 /***
72 * Get expression.
73 * @return expression.
74 */
75 public String getExpression() {
76 return expression;
77 }
78
79 /***
80 * Set whether in-fix expressions should be converted to post-fix.
81 * @param newValue if true, convert/
82 */
83 public void setConvertInFixToPostFix(final boolean newValue) {
84 this.convertInFixToPostFix = newValue;
85 }
86
87 /***
88 * Set whether in-fix expressions should be converted to post-fix.
89 * @return if true, expressions are converted.
90 */
91 public boolean getConvertInFixToPostFix() {
92 return convertInFixToPostFix;
93 }
94
95 /***
96 * If this event does not already contain location information,
97 * evaluate the event against the expression.
98 *
99 * If the expression evaluates to true,
100 * force generation of location information by
101 * calling getLocationInfo.
102 *
103 * @param event event
104 * @return Filter.NEUTRAL.
105 */
106 public int decide(final LoggingEvent event) {
107 if (expressionRule.evaluate(event, null)) {
108 event.getLocationInformation();
109 }
110 return Filter.NEUTRAL;
111 }
112 }