From 8ac45d4479570718055f0559bb6c07e90f8a591a Mon Sep 17 00:00:00 2001 From: Gregory Chanan Date: Mon, 2 Feb 2015 22:41:00 +0000 Subject: [PATCH] SOLR-6919: Log REST info before executing git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1656596 13f79535-47bb-0310-9956-ffa450edef68 --- .../java/org/apache/solr/core/SolrCore.java | 5 ++ .../solr/handler/RequestLoggingTest.java | 80 +++++++++++++++++++ 2 files changed, 85 insertions(+) create mode 100644 solr/core/src/test/org/apache/solr/handler/RequestLoggingTest.java diff --git a/solr/core/src/java/org/apache/solr/core/SolrCore.java b/solr/core/src/java/org/apache/solr/core/SolrCore.java index 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.java new file mode 100644 index 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")); + } +}