1 16 17 package org.apache.log4j.varia; 18 19 import org.apache.log4j.spi.Filter; 20 import org.apache.log4j.spi.LoggingEvent; 21 import org.apache.log4j.helpers.OptionConverter; 22 23 47 public class StringMatchFilter extends Filter { 48 49 54 public static final String STRING_TO_MATCH_OPTION = "StringToMatch"; 55 56 61 public static final String ACCEPT_ON_MATCH_OPTION = "AcceptOnMatch"; 62 63 boolean acceptOnMatch = true; 64 String stringToMatch; 65 66 70 public 71 String [] getOptionStrings() { 72 return new String [] {STRING_TO_MATCH_OPTION, ACCEPT_ON_MATCH_OPTION}; 73 } 74 75 79 public 80 void setOption(String key, String value) { 81 82 if(key.equalsIgnoreCase(STRING_TO_MATCH_OPTION)) { 83 stringToMatch = value; 84 } else if (key.equalsIgnoreCase(ACCEPT_ON_MATCH_OPTION)) { 85 acceptOnMatch = OptionConverter.toBoolean(value, acceptOnMatch); 86 } 87 } 88 89 public 90 void setStringToMatch(String s) { 91 stringToMatch = s; 92 } 93 94 public 95 String getStringToMatch() { 96 return stringToMatch; 97 } 98 99 public 100 void setAcceptOnMatch(boolean acceptOnMatch) { 101 this.acceptOnMatch = acceptOnMatch; 102 } 103 104 public 105 boolean getAcceptOnMatch() { 106 return acceptOnMatch; 107 } 108 109 112 public 113 int decide(LoggingEvent event) { 114 String msg = event.getRenderedMessage(); 115 116 if(msg == null || stringToMatch == null) 117 return Filter.NEUTRAL; 118 119 120 if( msg.indexOf(stringToMatch) == -1 ) { 121 return Filter.NEUTRAL; 122 } else { if(acceptOnMatch) { 124 return Filter.ACCEPT; 125 } else { 126 return Filter.DENY; 127 } 128 } 129 } 130 } 131 | Popular Tags |