/webportal/src/main/java/org/apache/log4j/filter/MDCMatchFilter.java

http://alageospatialportal.googlecode.com/ · 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. package org.apache.log4j.filter;
  17. import org.apache.log4j.spi.LoggingEvent;
  18. import org.apache.log4j.MDC;
  19. /**
  20. The MDCMatchFilter matches a configured value against the
  21. value of a configured key in the MDC of a logging event.
  22. <p>The filter admits three options <b>KeyToMatch</b>,
  23. <b>ValueToMatch</b>, and <b>ExactMatch</b>.
  24. <p>The value of <b>KeyToMatch</b> property determines which
  25. key is used to match against in the MDC. The value of that
  26. key is used to test against the <b>ValueToMatch</b property.
  27. The <b>KeyToMatch</b> property must be set before this filter
  28. can function properly.
  29. <p>The value of <b>ValueToMatch</b> property determines the
  30. string value to match against. If <b>ExactMatch</b> is set
  31. to true, a match will occur only when <b>ValueToMatch</b> exactly
  32. matches the MDC value of the logging event. Otherwise, if the
  33. <b>ExactMatch</b> property is set to <code>false</code>, a match
  34. will occur if <b>ValueToMatch</b> is contained anywhere within the
  35. MDC value. The <b>ExactMatch</b> property is set to
  36. <code>false</code> by default.
  37. <p>Note that by default the value to match is set to
  38. <code>null</code> and will only match if the key is not contained
  39. or the value is null in the MDC.
  40. <p>For more information about how the logging event will be
  41. passed to the appender for reporting, please see
  42. the {@link MatchFilterBase} class.
  43. @author Mark Womack
  44. @since 1.3
  45. */
  46. public class MDCMatchFilter extends MatchFilterBase {
  47. /**
  48. The key to match in the MDC of the LoggingEvent. */
  49. String keyToMatch;
  50. /**
  51. The value to match in the MDC value of the LoggingEvent. */
  52. String valueToMatch;
  53. /**
  54. Do we look for an exact match or just a "contains" match? */
  55. boolean exactMatch = false;
  56. /**
  57. Sets the key to match in the MDC of the LoggingEvent.
  58. @param key The key that will be matched. */
  59. public void setKeyToMatch(String key) {
  60. keyToMatch = key;
  61. }
  62. /**
  63. Gets the key to match in the MDC of the LoggingEvent.
  64. @return String The key that will be matched. */
  65. public String getKeyToMatch() {
  66. return keyToMatch;
  67. }
  68. /**
  69. Sets the value to match in the NDC value of the LoggingEvent.
  70. @param value The value to match. */
  71. public void setValueToMatch(String value) {
  72. valueToMatch = value;
  73. }
  74. /**
  75. Gets the value to match in the NDC value of the LoggingEvent.
  76. @return String The value to match. */
  77. public String getValueToMatch() {
  78. return valueToMatch;
  79. }
  80. /**
  81. Set to true if configured value must exactly match the MDC
  82. value of the LoggingEvent. Set to false if the configured
  83. value must only be contained in the MDC value of the
  84. LoggingEvent. Default is false.
  85. @param exact True if an exact match should be checked for. */
  86. public void setExactMatch(boolean exact) {
  87. exactMatch = exact;
  88. }
  89. /**
  90. Returns the true if an exact match will be checked for.
  91. @return boolean True if an exact match will be checked for. */
  92. public boolean getExactMatch() {
  93. return exactMatch;
  94. }
  95. /**
  96. Returns true if a key to match has been configured.
  97. @return boolean True if a match can be performed. */
  98. protected boolean canMatch() {
  99. return (keyToMatch != null);
  100. }
  101. /**
  102. If <b>ExactMatch</b> is set to true, returns true only when
  103. <b>ValueToMatch</b> exactly matches the MDC value of the
  104. logging event. If the <b>ExactMatch</b> property
  105. is set to <code>false</code>, returns true when
  106. <b>ValueToMatch</b> is contained anywhere within the MDC
  107. value. Otherwise, false is returned.
  108. @param event The logging event to match against.
  109. @return boolean True if matches criteria. */
  110. protected boolean match(LoggingEvent event) {
  111. // get the mdc value for the key from the event
  112. // use the toString() value of the value object
  113. //Object mdcObject = event.getMDC(keyToMatch); //removed in Log4j-1.3
  114. Object mdcObject = MDC.get(keyToMatch);
  115. String mdcValue;
  116. if (mdcObject != null) {
  117. mdcValue = mdcObject.toString();
  118. } else {
  119. mdcValue = null;
  120. }
  121. // check for a match
  122. if (mdcValue == null) {
  123. return (valueToMatch == null);
  124. } else {
  125. if (valueToMatch != null) {
  126. if (exactMatch) {
  127. return mdcValue.equals(valueToMatch);
  128. } else {
  129. return (mdcValue.indexOf(valueToMatch) != -1);
  130. }
  131. } else {
  132. return false;
  133. }
  134. }
  135. }
  136. }