Apache Lucene open-source search software
SOLR-6919: Log REST info before executing
Gregory Chanan8ac45d447957
diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.javaindex 6a21c502f8a..0779f5c8a5d 100644--- a/solr/core/src/java/org/apache/solr/core/SolrCore.java+++ b/solr/core/src/java/org/apache/solr/core/SolrCore.java@@ -1998,6 +1998,11 @@ public final class SolrCore implements SolrInfoMBean, Closeable {preDecorateResponse(req, rsp);+ if (log.isDebugEnabled() && rsp.getToLog().size() > 0) {+ // log request at debug in case something goes wrong and we aren't able to log later+ log.debug(rsp.getToLogAsString(logid));+ }+// TODO: this doesn't seem to be working correctly and causes problems with the example server and distrib (for example /spell)// if (req.getParams().getBool(ShardParams.IS_SHARD,false) && !(handler instanceof SearchHandler))// throw new SolrException(SolrException.ErrorCode.BAD_REQUEST,"isShard is only acceptable with search handlers");diff --git a/solr/core/src/test/org/apache/solr/handler/RequestLoggingTest.java b/solr/core/src/test/org/apache/solr/handler/RequestLoggingTest.javanew file mode 100644index 00000000000..1abca506e58--- /dev/null+++ b/solr/core/src/test/org/apache/solr/handler/RequestLoggingTest.java@@ -0,0 +1,80 @@+package org.apache.solr.handler;++/*+ * Licensed to the Apache Software Foundation (ASF) under one or more+ * contributor license agreements. See the NOTICE file distributed with+ * this work for additional information regarding copyright ownership.+ * The ASF licenses this file to You under the Apache License, Version 2.0+ * (the "License"); you may not use this file except in compliance with+ * the License. You may obtain a copy of the License at+ *+ * http://www.apache.org/licenses/LICENSE-2.0+ *+ * Unless required by applicable law or agreed to in writing, software+ * distributed under the License is distributed on an "AS IS" BASIS,+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.+ * See the License for the specific language governing permissions and+ * limitations under the License.+ */++import java.io.StringWriter;+import java.util.regex.Matcher;+import java.util.regex.Pattern;++import org.apache.log4j.Appender;+import org.apache.log4j.Level;+import org.apache.log4j.Logger;+import org.apache.log4j.SimpleLayout;+import org.apache.log4j.WriterAppender;+import org.apache.solr.SolrTestCaseJ4;+import org.apache.solr.core.SolrCore;+import org.junit.After;+import org.junit.Before;+import org.junit.BeforeClass;+import org.junit.Test;++public class RequestLoggingTest extends SolrTestCaseJ4 {+ private static final Logger solrLogger = Logger.getLogger(SolrCore.class);++ private Level oldLevel;++ private Appender appender;++ private StringWriter writer;++ @BeforeClass+ public static void beforeClass() throws Exception {+ initCore("solrconfig.xml", "schema.xml");+ }++ @Before+ public void setupLogger() {+ oldLevel = solrLogger.getLevel();+ solrLogger.setLevel(Level.DEBUG);++ writer = new StringWriter();+ appender = new WriterAppender(new SimpleLayout(), writer);++ solrLogger.addAppender(appender);+ }++ @After+ public void resetLogger() {+ solrLogger.setLevel(oldLevel);+ solrLogger.removeAppender(appender);+ }++ @Test+ public void testLogBeforeExecute() {+ assertQ(req("q", "*:*"));++ String output = writer.toString();+ Matcher matcher = Pattern.compile("DEBUG.*q=\\*:\\*.*").matcher(output);+ assertTrue(matcher.find());+ final String group = matcher.group();+ final String msg = "Should not have post query information";+ assertFalse(msg, group.contains("hits"));+ assertFalse(msg, group.contains("status"));+ assertFalse(msg, group.contains("QTime"));+ }+}