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
This commit is contained in:
Gregory Chanan 2015-02-02 22:41:00 +00:00
parent 4ee47bdcde
commit 8ac45d4479
2 changed files with 85 additions and 0 deletions

View File

@ -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");

View File

@ -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"));
}
}