You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-dev@logging.apache.org by yo...@apache.org on 2003/09/25 01:19:09 UTC
cvs commit: jakarta-log4j-sandbox/src/java/org/apache/log4j/filter ThrowableClassMatchFilter.java
yoavs 2003/09/24 16:19:09
Modified: src/java/org/apache/log4j/filter
ThrowableClassMatchFilter.java
Log:
Added String ThrowableClassName property to allow
setting via property files. Added activateOptions
method to load the throwableClass as specified by
the ThrowableClassName property if the throwableClass
is not already loaded. Patch suggested by mailing
list user.
Revision Changes Path
1.2 +127 -83 jakarta-log4j-sandbox/src/java/org/apache/log4j/filter/ThrowableClassMatchFilter.java
Index: ThrowableClassMatchFilter.java
===================================================================
RCS file: /home/cvs/jakarta-log4j-sandbox/src/java/org/apache/log4j/filter/ThrowableClassMatchFilter.java,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -r1.1 -r1.2
--- ThrowableClassMatchFilter.java 7 Jun 2003 02:31:49 -0000 1.1
+++ ThrowableClassMatchFilter.java 24 Sep 2003 23:19:09 -0000 1.2
@@ -49,6 +49,7 @@
package org.apache.log4j.filter;
+import org.apache.log4j.helpers.LogLog;
import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.ThrowableInformation;
@@ -74,90 +75,133 @@
* @since 1.3
*/
public class ThrowableClassMatchFilter extends MatchFilterBase {
- /**
- * The throwable class to match.
- */
- private Class throwableClass;
+ /**
+ * The throwable class fully-qualified
+ * class name.
+ */
+ private String throwableClassName;
+
+ /**
+ * The throwable class to match.
+ */
+ private Class throwableClass;
- /**
- * Whether subclasses of ThrowableClass
- * should count as a match.
- */
- private boolean includeSubclasses = true;
-
- /**
- * Sets the throwable class to match.
- *
- * @param throwableClass The throwable class
- */
- public void setThrowableClass(Class throwableClass) {
- this.throwableClass = throwableClass;
- }
-
- /**
- * Returns the throwable class
- * to match.
- *
- * @return Class
- */
- public Class getThrowableClass() {
- return throwableClass;
- }
-
- /**
- * Sets whether subclasses of the throwableClass
- * will match the filter or not.
- *
- * @param includeSubclasses True if subclasses match
- */
- public void setIncludeSubclasses(boolean includeSubclasses) {
- this.includeSubclasses = includeSubclasses;
- }
-
- /**
- * Returns whether subclasses of the throwableClass
- * are included as a match.
- *
- * @return True if subclasses match
- */
- public boolean getIncludeSubclasses() {
- return includeSubclasses;
- }
-
- /**
- * Checks the given event to see if it has
- * a throwable. If a throwable is present and its
- * class exactly matches that of the <b>throwableClass</b>
- * property, this method returns true. If
- * <b>includeSubclasses</b> is set to true and the
- * throwable is a subclass of <b>throwableClass<b>, then
- * this method also returns true.
- *
- * @param event The logging event
- * @return boolean True if matches criteria
- */
- protected boolean match(LoggingEvent event) {
- ThrowableInformation ti = event.getThrowableInformation();
-
- if(ti == null) {
- return false;
- } else {
- Throwable throwable = ti.getThrowable();
-
- if(throwable == null) {
- return false;
- } else {
- if(throwable.getClass().equals(getThrowableClass())) {
- return true;
- } else if(getIncludeSubclasses()) {
- return (getThrowableClass().isInstance(throwable));
- } else {
- return false;
- }
- }
- }
- }
+ /**
+ * Whether subclasses of ThrowableClass
+ * should count as a match.
+ */
+ private boolean includeSubclasses = true;
+
+ /**
+ * Sets the throwable class name to match.
+ *
+ * @param fqcn The class name
+ */
+ public void setThrowableClassName(String fqcn) {
+ this.throwableClassName = fqcn;
+ }
+
+ /**
+ * Sets the throwable class to match.
+ *
+ * @param throwableClass The throwable class
+ */
+ public void setThrowableClass(Class throwableClass) {
+ this.throwableClass = throwableClass;
+ }
+
+ /**
+ * Returns the throwable class
+ * to match.
+ *
+ * @return Class
+ */
+ public Class getThrowableClass() {
+ return throwableClass;
+ }
+
+ /**
+ * Returns the throwable class name to match.
+ *
+ * @return String
+ */
+ public String getThrowableClassName() {
+ return throwableClassName;
+ }
+
+ /**
+ * Sets whether subclasses of the throwableClass
+ * will match the filter or not.
+ *
+ * @param includeSubclasses True if subclasses match
+ */
+ public void setIncludeSubclasses(boolean includeSubclasses) {
+ this.includeSubclasses = includeSubclasses;
+ }
+
+ /**
+ * Returns whether subclasses of the throwableClass
+ * are included as a match.
+ *
+ * @return True if subclasses match
+ */
+ public boolean getIncludeSubclasses() {
+ return includeSubclasses;
+ }
+
+ /**
+ * Checks the given event to see if it has
+ * a throwable. If a throwable is present and its
+ * class exactly matches that of the <b>throwableClass</b>
+ * property, this method returns true. If
+ * <b>includeSubclasses</b> is set to true and the
+ * throwable is a subclass of <b>throwableClass<b>, then
+ * this method also returns true.
+ *
+ * @param event The logging event
+ * @return boolean True if matches criteria
+ */
+ protected boolean match(LoggingEvent event) {
+ ThrowableInformation ti = event.getThrowableInformation();
+
+ if(ti == null) {
+ return false;
+ } else {
+ Throwable throwable = ti.getThrowable();
+
+ if(throwable == null) {
+ return false;
+ } else {
+ if(throwable.getClass().equals(getThrowableClass())) {
+ return true;
+ } else if(getIncludeSubclasses()) {
+ return (getThrowableClass().isInstance(throwable));
+ } else {
+ return false;
+ }
+ }
+ }
+ }
+
+ /**
+ * Actives the options for this filter.
+ * Loads the class specified by the
+ * ThrowableClassName attribute if not already
+ * loaded.
+ */
+ public void activateOptions() {
+ super.activateOptions();
+
+ if(getThrowableClass() == null) {
+ try {
+ setThrowableClass(Class.forName(getThrowableClassName()));
+ } catch (ClassNotFoundException cnfe) {
+ LogLog.warn("Could not load throwable class" +
+ getThrowableClassName() + ": ", cnfe);
+ }
+ }
+ }
}
-// End of class: ThrowableClassMatchFilter.java
\ No newline at end of file
+// End of class: ThrowableClassMatchFilter.java
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: log4j-dev-help@jakarta.apache.org