/webportal/src/main/java/org/apache/log4j/filter/MDCMatchFilter.java
Java | 167 lines | 51 code | 16 blank | 100 comment | 14 complexity | 20a12e125cd6d86ced66582023000c8b MD5 | raw file
1/* 2 * Copyright 1999,2004 The Apache Software Foundation. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package org.apache.log4j.filter; 18 19import org.apache.log4j.spi.LoggingEvent; 20import org.apache.log4j.MDC; 21 22 23/** 24 The MDCMatchFilter matches a configured value against the 25 value of a configured key in the MDC of a logging event. 26 27 <p>The filter admits three options <b>KeyToMatch</b>, 28 <b>ValueToMatch</b>, and <b>ExactMatch</b>. 29 30 <p>The value of <b>KeyToMatch</b> property determines which 31 key is used to match against in the MDC. The value of that 32 key is used to test against the <b>ValueToMatch</b property. 33 The <b>KeyToMatch</b> property must be set before this filter 34 can function properly. 35 36 <p>The value of <b>ValueToMatch</b> property determines the 37 string value to match against. If <b>ExactMatch</b> is set 38 to true, a match will occur only when <b>ValueToMatch</b> exactly 39 matches the MDC value of the logging event. Otherwise, if the 40 <b>ExactMatch</b> property is set to <code>false</code>, a match 41 will occur if <b>ValueToMatch</b> is contained anywhere within the 42 MDC value. The <b>ExactMatch</b> property is set to 43 <code>false</code> by default. 44 45 <p>Note that by default the value to match is set to 46 <code>null</code> and will only match if the key is not contained 47 or the value is null in the MDC. 48 49 <p>For more information about how the logging event will be 50 passed to the appender for reporting, please see 51 the {@link MatchFilterBase} class. 52 53 @author Mark Womack 54 55 @since 1.3 56*/ 57public class MDCMatchFilter extends MatchFilterBase { 58 /** 59 The key to match in the MDC of the LoggingEvent. */ 60 String keyToMatch; 61 62 /** 63 The value to match in the MDC value of the LoggingEvent. */ 64 String valueToMatch; 65 66 /** 67 Do we look for an exact match or just a "contains" match? */ 68 boolean exactMatch = false; 69 70 /** 71 Sets the key to match in the MDC of the LoggingEvent. 72 73 @param key The key that will be matched. */ 74 public void setKeyToMatch(String key) { 75 keyToMatch = key; 76 } 77 78 /** 79 Gets the key to match in the MDC of the LoggingEvent. 80 81 @return String The key that will be matched. */ 82 public String getKeyToMatch() { 83 return keyToMatch; 84 } 85 86 /** 87 Sets the value to match in the NDC value of the LoggingEvent. 88 89 @param value The value to match. */ 90 public void setValueToMatch(String value) { 91 valueToMatch = value; 92 } 93 94 /** 95 Gets the value to match in the NDC value of the LoggingEvent. 96 97 @return String The value to match. */ 98 public String getValueToMatch() { 99 return valueToMatch; 100 } 101 102 /** 103 Set to true if configured value must exactly match the MDC 104 value of the LoggingEvent. Set to false if the configured 105 value must only be contained in the MDC value of the 106 LoggingEvent. Default is false. 107 108 @param exact True if an exact match should be checked for. */ 109 public void setExactMatch(boolean exact) { 110 exactMatch = exact; 111 } 112 113 /** 114 Returns the true if an exact match will be checked for. 115 116 @return boolean True if an exact match will be checked for. */ 117 public boolean getExactMatch() { 118 return exactMatch; 119 } 120 121 /** 122 Returns true if a key to match has been configured. 123 124 @return boolean True if a match can be performed. */ 125 protected boolean canMatch() { 126 return (keyToMatch != null); 127 } 128 129 /** 130 If <b>ExactMatch</b> is set to true, returns true only when 131 <b>ValueToMatch</b> exactly matches the MDC value of the 132 logging event. If the <b>ExactMatch</b> property 133 is set to <code>false</code>, returns true when 134 <b>ValueToMatch</b> is contained anywhere within the MDC 135 value. Otherwise, false is returned. 136 137 @param event The logging event to match against. 138 @return boolean True if matches criteria. */ 139 protected boolean match(LoggingEvent event) { 140 // get the mdc value for the key from the event 141 // use the toString() value of the value object 142 //Object mdcObject = event.getMDC(keyToMatch); //removed in Log4j-1.3 143 Object mdcObject = MDC.get(keyToMatch); 144 String mdcValue; 145 146 if (mdcObject != null) { 147 mdcValue = mdcObject.toString(); 148 } else { 149 mdcValue = null; 150 } 151 152 // check for a match 153 if (mdcValue == null) { 154 return (valueToMatch == null); 155 } else { 156 if (valueToMatch != null) { 157 if (exactMatch) { 158 return mdcValue.equals(valueToMatch); 159 } else { 160 return (mdcValue.indexOf(valueToMatch) != -1); 161 } 162 } else { 163 return false; 164 } 165 } 166 } 167}