[jboss-svn-commits] JBoss Common SVN: r3208 - in jboss-logmanager/trunk/src/main/java/org/jboss/logmanager: filters and 1 other directory.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Tue Jun 2 19:52:32 EDT 2009


Author: david.lloyd at jboss.com
Date: 2009-06-02 19:52:32 -0400 (Tue, 02 Jun 2009)
New Revision: 3208

Added:
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AcceptAllFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AllFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AnyFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/DenyAllFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelChangingFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelExcludingFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelIncludingFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelRangeFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/RegexFilter.java
   jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/SubstituteFilter.java
Log:
Add a bunch of basic filters

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AcceptAllFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AcceptAllFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AcceptAllFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.logging.Filter;
+import java.util.logging.LogRecord;
+
+/**
+ * An accept-all filter.
+ */
+public final class AcceptAllFilter implements Filter {
+    private AcceptAllFilter() {}
+
+    private static final AcceptAllFilter INSTANCE = new AcceptAllFilter();
+
+    /**
+     * Always returns {@code true}.
+     *
+     * @param record ignored
+     * @return {@code true}
+     */
+    public boolean isLoggable(final LogRecord record) {
+        return false;
+    }
+
+    /**
+     * Get the filter instance.
+     *
+     * @return the filter instance
+     */
+    public static AcceptAllFilter getInstance() {
+        return INSTANCE;
+    }
+}
\ No newline at end of file

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AllFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AllFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AllFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,92 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.Iterator;
+
+import java.util.logging.Filter;
+import java.util.logging.LogRecord;
+
+/**
+ * A filter consisting of several filters in a chain.  If any filter finds the log message to be unloggable,
+ * the message will not be logged and subsequent filters will not be checked.
+ */
+public final class AllFilter implements Filter {
+    private final Filter[] filters;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param filters the constituent filters
+     */
+    public AllFilter(final Filter[] filters) {
+        this.filters = filters;
+    }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param filters the constituent filters
+     */
+    public AllFilter(final Iterable<Filter> filters) {
+        this(filters.iterator());
+    }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param filters the constituent filters
+     */
+    public AllFilter(final Iterator<Filter> filters) {
+        this.filters = unroll(filters, 0);
+    }
+
+    private static Filter[] unroll(Iterator<Filter> iter, int cnt) {
+        if (iter.hasNext()) {
+            final Filter filter = iter.next();
+            if (filter == null) {
+                throw new NullPointerException("filter at index " + cnt + " is null");
+            }
+            final Filter[] filters = unroll(iter, cnt + 1);
+            filters[cnt] = filter;
+            return filters;
+        } else {
+            return new Filter[cnt];
+        }
+    }
+
+    /**
+     * Determine whether the record is loggable.
+     *
+     * @param record the log record
+     * @return {@code true} if all the constituent filters return {@code true}
+     */
+    public boolean isLoggable(final LogRecord record) {
+        for (Filter filter : filters) {
+            if (! filter.isLoggable(record)) {
+                return false;
+            }
+        }
+        return true;
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AnyFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AnyFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/AnyFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,92 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.Iterator;
+
+import java.util.logging.Filter;
+import java.util.logging.LogRecord;
+
+/**
+ * A filter consisting of several filters in a chain.  If any filter finds the log message to be loggable,
+ * the message will be logged and subsequent filters will not be checked.
+ */
+public final class AnyFilter implements Filter {
+    private final Filter[] filters;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param filters the constituent filters
+     */
+    public AnyFilter(final Filter[] filters) {
+        this.filters = filters;
+    }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param filters the constituent filters
+     */
+    public AnyFilter(final Iterable<Filter> filters) {
+        this(filters.iterator());
+    }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param filters the constituent filters
+     */
+    public AnyFilter(final Iterator<Filter> filters) {
+        this.filters = unroll(filters, 0);
+    }
+
+    private static Filter[] unroll(Iterator<Filter> iter, int cnt) {
+        if (iter.hasNext()) {
+            final Filter filter = iter.next();
+            if (filter == null) {
+                throw new NullPointerException("filter at index " + cnt + " is null");
+            }
+            final Filter[] filters = unroll(iter, cnt + 1);
+            filters[cnt] = filter;
+            return filters;
+        } else {
+            return new Filter[cnt];
+        }
+    }
+
+    /**
+     * Determine whether the record is loggable.
+     *
+     * @param record the log record
+     * @return {@code true} if any of the constituent filters return {@code true}
+     */
+    public boolean isLoggable(final LogRecord record) {
+        for (Filter filter : filters) {
+            if (filter.isLoggable(record)) {
+                return true;
+            }
+        }
+        return false;
+    }
+}
\ No newline at end of file

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/DenyAllFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/DenyAllFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/DenyAllFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,54 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.logging.Filter;
+import java.util.logging.LogRecord;
+
+/**
+ * A deny-all filter.
+ */
+public final class DenyAllFilter implements Filter {
+    private DenyAllFilter() {}
+
+    private static final DenyAllFilter INSTANCE = new DenyAllFilter();
+
+    /**
+     * Always returns {@code false}.
+     *
+     * @param record ignored
+     * @return {@code false}
+     */
+    public boolean isLoggable(final LogRecord record) {
+        return false;
+    }
+
+    /**
+     * Get the filter instance.
+     *
+     * @return the filter instance
+     */
+    public static DenyAllFilter getInstance() {
+        return INSTANCE;
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelChangingFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelChangingFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelChangingFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,61 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.logging.Filter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ * A filter which modifies the log record with a new level if the nested filter evaluates {@code true} for that
+ * record.
+ */
+public final class LevelChangingFilter implements Filter {
+
+    private final Filter matchFilter;
+    private final Level newLevel;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param matchFilter the filter which is checked to see whether the level should be changed
+     * @param newLevel the level to change to
+     */
+    public LevelChangingFilter(final Filter matchFilter, final Level newLevel) {
+        this.matchFilter = matchFilter;
+        this.newLevel = newLevel;
+    }
+
+    /**
+     * Apply the filter to this log record.
+     *
+     * @param record the record to inspect and possibly update
+     * @return {@code true} always
+     */
+    public boolean isLoggable(final LogRecord record) {
+        if (matchFilter.isLoggable(record)) {
+            record.setLevel(newLevel);
+        }
+        return true;
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelExcludingFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelExcludingFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelExcludingFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import java.util.logging.Filter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ * A filter which excludes messages of a certain level or levels
+ */
+public final class LevelExcludingFilter implements Filter {
+    private final Set<Level> excludedLevels;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param excludedLevel the level to exclude
+     */
+    public LevelExcludingFilter(final Level excludedLevel) {
+        excludedLevels = Collections.singleton(excludedLevel);
+    }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param excludedLevels the levels to exclude
+     */
+    public LevelExcludingFilter(final Collection<Level> excludedLevels) {
+        this.excludedLevels = new HashSet<Level>(excludedLevels);
+    }
+
+    /**
+     * Determine whether the message is loggable.
+     *
+     * @param record the log record
+     * @return {@code true} if the level is not in the exclusion list
+     */
+    public boolean isLoggable(final LogRecord record) {
+        return ! excludedLevels.contains(record.getLevel());
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelIncludingFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelIncludingFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelIncludingFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,67 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.Collection;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.Set;
+
+import java.util.logging.Filter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ * A filter which excludes messages of a certain level or levels
+ */
+public final class LevelIncludingFilter implements Filter {
+    private final Set<Level> includedLevels;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param includedLevel the level to include
+     */
+    public LevelIncludingFilter(final Level includedLevel) {
+        includedLevels = Collections.singleton(includedLevel);
+    }
+
+    /**
+     * Construct a new instance.
+     *
+     * @param includedLevels the levels to exclude
+     */
+    public LevelIncludingFilter(final Collection<Level> includedLevels) {
+        this.includedLevels = new HashSet<Level>(includedLevels);
+    }
+
+    /**
+     * Determine whether the message is loggable.
+     *
+     * @param record the log record
+     * @return {@code true} if the level is in the inclusion list
+     */
+    public boolean isLoggable(final LogRecord record) {
+        return includedLevels.contains(record.getLevel());
+    }
+}
\ No newline at end of file

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelRangeFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelRangeFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/LevelRangeFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,60 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.logging.Filter;
+import java.util.logging.Level;
+import java.util.logging.LogRecord;
+
+/**
+ * Log only messages that fall within a level range.
+ */
+public final class LevelRangeFilter implements Filter {
+    private final int min;
+    private final int max;
+
+    /**
+     * Create a new instance.
+     *
+     * @param min the minimum (least severe) level, inclusive
+     * @param max the maximum (most severe) level, inclusive
+     */
+    public LevelRangeFilter(final Level min, final Level max) {
+        this.min = min.intValue();
+        this.max = max.intValue();
+        if (this.max > this.min) {
+            throw new IllegalArgumentException("Max level cannot be greater than min level");
+        }
+    }
+
+    /**
+     * Determine if a record is loggable.
+     *
+     * @param record the log record
+     * @return {@code true} if the record's level falls within the range specified for this instance
+     */
+    public boolean isLoggable(final LogRecord record) {
+        final int iv = record.getLevel().intValue();
+        return min <= iv && iv <= max;
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/RegexFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/RegexFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/RegexFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,68 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.regex.Pattern;
+
+import java.util.logging.Filter;
+import java.util.logging.LogRecord;
+
+/**
+ * A regular-expression-based filter.  Used to exclude log records which match or don't match the expression.  The
+ * regular expression is checked against the raw (unformatted) message.
+ */
+public final class RegexFilter implements Filter {
+    private final Pattern pattern;
+    private final boolean exclude;
+
+    /**
+     * Create a new instance.
+     *
+     * @param pattern the pattern to match
+     * @param exclude {@code true} if matching records should be excluded, {@code false} if matching records should be included
+     */
+    public RegexFilter(final Pattern pattern, final boolean exclude) {
+        this.pattern = pattern;
+        this.exclude = exclude;
+    }
+
+    /**
+     * Create a new instance.
+     *
+     * @param patternString the pattern string to match
+     * @param exclude {@code true} if matching records should be excluded, {@code false} if matching records should be included
+     */
+    public RegexFilter(final String patternString, final boolean exclude) {
+        this(Pattern.compile(patternString), exclude);
+    }
+
+    /**
+     * Determine if this log record is loggable.
+     *
+     * @param record the log record
+     * @return {@code true} if the log record is loggable
+     */
+    public boolean isLoggable(final LogRecord record) {
+        return pattern.matcher(record.getMessage()).matches() != exclude;
+    }
+}

Added: jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/SubstituteFilter.java
===================================================================
--- jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/SubstituteFilter.java	                        (rev 0)
+++ jboss-logmanager/trunk/src/main/java/org/jboss/logmanager/filters/SubstituteFilter.java	2009-06-02 23:52:32 UTC (rev 3208)
@@ -0,0 +1,73 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2009, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+
+package org.jboss.logmanager.filters;
+
+import java.util.regex.Pattern;
+import java.util.regex.Matcher;
+
+import java.util.logging.Filter;
+import java.util.logging.LogRecord;
+
+/**
+ * A filter which applies a text substitution on the message if the nested filter matches.
+ */
+public final class SubstituteFilter implements Filter {
+    private final Filter matchFilter;
+    private final Pattern pattern;
+    private final String replacement;
+    private final boolean replaceAll;
+
+    /**
+     * Construct a new instance.
+     *
+     * @param matchFilter the filter to check
+     * @param pattern the pattern to match
+     * @param replacement the string replacement
+     * @param replaceAll {@code true} if all occurrances should be replaced; {@code false} if only the first occurrance
+     *      should be replaced
+     */
+    public SubstituteFilter(final Filter matchFilter, final Pattern pattern, final String replacement, final boolean replaceAll) {
+        this.matchFilter = matchFilter;
+        this.pattern = pattern;
+        this.replacement = replacement;
+        this.replaceAll = replaceAll;
+    }
+
+    /**
+     * Apply the filter to the given log record.
+     *
+     * @param record the log record to inspect and modify
+     * @return {@code true} always
+     */
+    public boolean isLoggable(final LogRecord record) {
+        if (matchFilter.isLoggable(record)) {
+            final Matcher matcher = pattern.matcher(record.getMessage());
+            if (replaceAll) {
+                record.setMessage(matcher.replaceAll(replacement));
+            } else {
+                record.setMessage(matcher.replaceFirst(replacement));
+            }
+        }
+        return true;
+    }
+}




More information about the jboss-svn-commits mailing list