Apache Lucene open-source search software
SOLR-1835: add ability to not log exceptions matching a certain pattern
Yonik Seeleyace867f60b02
diff --git a/solr/src/common/org/apache/solr/common/SolrException.java b/solr/src/common/org/apache/solr/common/SolrException.javaindex 4e7acc328b4..7853edf764c 100644--- a/solr/src/common/org/apache/solr/common/SolrException.java+++ b/solr/src/common/org/apache/solr/common/SolrException.java@@ -18,15 +18,18 @@package org.apache.solr.common;import org.slf4j.Logger;-import org.slf4j.LoggerFactory;+import java.io.CharArrayWriter;import java.io.PrintWriter;+import java.util.Set;+import java.util.regex.Matcher;+import java.util.regex.Pattern;/*** @version $Id$*/public class SolrException extends RuntimeException {-+/*** @since solr 1.2*/@@ -136,17 +139,30 @@ public class SolrException extends RuntimeException {public void log(Logger log) { log(log,this); }public static void log(Logger log, Throwable e) {- log.error(toStr(e));if (e instanceof SolrException) {((SolrException)e).logged = true;}+ String stackTrace = toStr(e);+ String ignore = doIgnore(stackTrace);+ if (ignore != null) {+ log.info(ignore);+ return;+ }+ log.error(stackTrace);+}public static void log(Logger log, String msg, Throwable e) {- log.error(msg + ':' + toStr(e));if (e instanceof SolrException) {((SolrException)e).logged = true;}+ String stackTrace = msg + ':' + toStr(e);+ String ignore = doIgnore(stackTrace);+ if (ignore != null) {+ log.info(ignore);+ return;+ }+ log.error(stackTrace);}public static void logOnce(Logger log, String msg, Throwable e) {@@ -179,4 +195,22 @@ public class SolrException extends RuntimeException {**/}++ /** For test code - do not log exceptions that match any of the regular expressions in ignorePatterns */+ public static Set<String> ignorePatterns;++ /** Returns null if this exception does not match any ignore patterns, or a message string to use if it does. */+ public static String doIgnore(String m) {+ if (ignorePatterns == null || m == null) return null;++ for (String regex : ignorePatterns) {+ Pattern pattern = Pattern.compile(regex);+ Matcher matcher = pattern.matcher(m);+ if (matcher.find()) return "Ignoring exception matching " + regex;+ }++ return null;+ }++}diff --git a/solr/src/test/org/apache/solr/BasicFunctionalityTest.java b/solr/src/test/org/apache/solr/BasicFunctionalityTest.javaindex 93f9c61e628..390bd1f6a11 100644--- a/solr/src/test/org/apache/solr/BasicFunctionalityTest.java+++ b/solr/src/test/org/apache/solr/BasicFunctionalityTest.java@@ -65,7 +65,7 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 {public String getCoreName() { return "basic"; }@BeforeClass- public static void beforeClass() throws Exception {+ public static void beforeTests() throws Exception {initCore("solrconfig.xml","schema.xml");}// tests the performance of dynamic field creation and@@ -224,7 +224,7 @@ public class BasicFunctionalityTest extends SolrTestCaseJ4 {@Testpublic void testRequestHandlerBaseException() {- final String tmp = "BOO!";+ final String tmp = "BOO! ignore_exception";SolrRequestHandler handler = new RequestHandlerBase() {public String getDescription() { return tmp; }public String getSourceId() { return tmp; }diff --git a/solr/src/test/org/apache/solr/ConvertedLegacyTest.java b/solr/src/test/org/apache/solr/ConvertedLegacyTest.javaindex 728070c4b51..8dd279e4235 100644--- a/solr/src/test/org/apache/solr/ConvertedLegacyTest.java+++ b/solr/src/test/org/apache/solr/ConvertedLegacyTest.java@@ -19,6 +19,8 @@ package org.apache.solr;import org.apache.solr.request.*;import org.apache.solr.util.*;+import org.junit.BeforeClass;+import org.junit.Test;import java.util.*;@@ -29,11 +31,14 @@ import java.util.*;* it does not represent the best practices that should be used when* writing Solr JUnit tests*/-public class ConvertedLegacyTest extends AbstractSolrTestCase {+public class ConvertedLegacyTest extends SolrTestCaseJ4 {- public String getSchemaFile() { return "schema.xml"; }- public String getSolrConfigFile() { return "solrconfig.xml"; }+ @BeforeClass+ public static void beforeTests() throws Exception {+ initCore("solrconfig.xml","schema.xml");+ }+ @Testpublic void testABunchOfConvertedStuff() {// these may be reused by things that need a special querySolrQueryRequest req = null;@@ -780,7 +785,12 @@ public class ConvertedLegacyTest extends AbstractSolrTestCase {,"//doc[2]/int[.='1000'] ","//doc[3]/int[.='1001']");-++ ignoreException("shouldbeunindexed");+ ignoreException("nullfirst");+ ignoreException("abcde12345");+ ignoreException("aaa");+// Sort parsing exception tests. (SOLR-6, SOLR-99)assertQEx( "can not sort unindexed fields",req( "id_i:1000; shouldbeunindexed asc" ), 400 );@@ -792,7 +802,10 @@ public class ConvertedLegacyTest extends AbstractSolrTestCase {req( "id_i:1000; abcde12345 asc" ), 400 );assertQEx( "unknown sort order",- req( "id_i:1000; nullfirst aaa" ), 400 );+ req( "id_i:1000; nullfirst aaa" ), 400 );++ resetExceptionIgnores();+// test prefix querydiff --git a/solr/src/test/org/apache/solr/SolrTestCaseJ4.java b/solr/src/test/org/apache/solr/SolrTestCaseJ4.javaindex b2c3aa3cbf1..7b7cba230de 100755--- a/solr/src/test/org/apache/solr/SolrTestCaseJ4.java+++ b/solr/src/test/org/apache/solr/SolrTestCaseJ4.java@@ -38,6 +38,7 @@ import java.io.File;import java.io.IOException;import java.io.StringWriter;import java.util.ArrayList;+import java.util.HashSet;import java.util.List;import static org.junit.Assert.assertEquals;@@ -51,12 +52,14 @@ import static org.junit.Assert.fail;public class SolrTestCaseJ4 extends LuceneTestCaseJ4 {@BeforeClass- public static void beforeClass() throws Exception {+ public static void beforeClassSolrTestCase() throws Exception {+ ignoreException("ignore_exception");}@AfterClass- public static void afterClass() throws Exception {+ public static void afterClassSolrTestCase() throws Exception {deleteCore();+ resetExceptionIgnores();}@Override@@ -74,11 +77,25 @@ public class SolrTestCaseJ4 extends LuceneTestCaseJ4 {/** Call initCore in @BeforeClass to instantiate a solr core in your test class.* deleteCore will be called for you via SolrTestCaseJ4 @AfterClass */public static void initCore(String config, String schema) throws Exception {+ //ignoreException("ignore_exception");configString = config;schemaString = schema;initCore();}+ /** Causes an exception matching the regex pattern to not be logged. */+ public static void ignoreException(String pattern) {+ if (SolrException.ignorePatterns == null)+ SolrException.ignorePatterns = new HashSet<String>();+ SolrException.ignorePatterns.add(pattern);+ }++ public static void resetExceptionIgnores() {+ SolrException.ignorePatterns = null;+ ignoreException("ignore_exception"); // always ignore "ignore_exception"+ }++protected static String configString;protected static String schemaString;