From 24599336aeb238e546635b899e1647e711484605 Mon Sep 17 00:00:00 2001 From: Toshiya Kobayashi Date: Mon, 5 Mar 2018 13:59:20 +0900 Subject: [PATCH] [LOGMGR-191] Amend NullPointerException in RegexFilter.isLoggable() --- .../jboss/logmanager/filters/RegexFilter.java | 6 ++++++ .../java/org/jboss/logmanager/FilterTests.java | 17 +++++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/src/main/java/org/jboss/logmanager/filters/RegexFilter.java b/src/main/java/org/jboss/logmanager/filters/RegexFilter.java index ee4e71a3..d55267e1 100644 --- a/src/main/java/org/jboss/logmanager/filters/RegexFilter.java +++ b/src/main/java/org/jboss/logmanager/filters/RegexFilter.java @@ -60,8 +60,14 @@ public RegexFilter(final String patternString) { @Override public boolean isLoggable(final LogRecord record) { if (record instanceof ExtLogRecord) { + if (((ExtLogRecord) record).getFormattedMessage() == null) { + return false; + } return pattern.matcher(((ExtLogRecord) record).getFormattedMessage()).find(); } + if (record.getMessage() == null) { + return false; + } return pattern.matcher(record.getMessage()).find(); } } diff --git a/src/test/java/org/jboss/logmanager/FilterTests.java b/src/test/java/org/jboss/logmanager/FilterTests.java index b51a1f97..29ce44ee 100644 --- a/src/test/java/org/jboss/logmanager/FilterTests.java +++ b/src/test/java/org/jboss/logmanager/FilterTests.java @@ -436,6 +436,23 @@ public void testRegexFilter3() { assertFalse("Handler was run", ran.get()); } + @Test + public void testRegexFilter4() { + final Filter filter = new RegexFilter("pest"); + final AtomicBoolean ran = new AtomicBoolean(); + final Handler handler = new CheckingHandler(ran); + final Logger logger = Logger.getLogger("filterTest"); + final ExtLogRecord record = new ExtLogRecord(Level.ERROR, null, FormatStyle.MESSAGE_FORMAT, "filterTest"); + record.setThrown(new Exception()); + logger.setUseParentHandlers(false); + logger.addHandler(handler); + logger.setLevel(Level.INFO); + logger.setFilter(filter); + handler.setLevel(Level.INFO); + logger.log(record); + assertFalse("Handler was run", ran.get()); + } + @Test public void testSubstitueFilter0() { final Filter filter = new SubstituteFilter(Pattern.compile("test"), "lunch", true);