lucene-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From no...@apache.org
Subject svn commit: r1668992 [2/2] - in /lucene/dev/trunk/solr: ./ core/src/java/org/apache/solr/cloud/ core/src/java/org/apache/solr/logging/ core/src/java/org/apache/solr/servlet/ core/src/java/org/apache/solr/util/ server/resources/ solrj/src/java/org/apach...
Date Tue, 24 Mar 2015 21:01:45 GMT
Added: lucene/dev/trunk/solr/core/src/java/org/apache/solr/logging/MDCUtils.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/logging/MDCUtils.java?rev=1668992&view=auto
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/logging/MDCUtils.java (added)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/logging/MDCUtils.java Tue Mar 24 21:01:44
2015
@@ -0,0 +1,69 @@
+package org.apache.solr.logging;
+
+import java.util.Map;
+
+import org.apache.solr.common.cloud.ZkStateReader;
+import org.slf4j.MDC;
+
+import static org.apache.solr.common.cloud.ZkStateReader.COLLECTION_PROP;
+import static org.apache.solr.common.cloud.ZkStateReader.CORE_NAME_PROP;
+import static org.apache.solr.common.cloud.ZkStateReader.REPLICA_PROP;
+import static org.apache.solr.common.cloud.ZkStateReader.SHARD_ID_PROP;
+
+/*
+ * 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.
+ */
+
+public class MDCUtils {
+  public static void cleanupMDC(Map previousMDCContext) {
+    if (previousMDCContext != null)
+      MDC.setContextMap(previousMDCContext);
+  }
+
+  public static void setMDC (String collection, String shard, String replica, String core)
{
+    setCollection(collection);
+    setShard(shard);
+    setReplica(replica);
+    setCore(core);
+  }
+
+  public static void setCollection(String collection) {
+    if (collection != null)
+      MDC.put(COLLECTION_PROP, collection);
+  }
+
+  public static void setShard(String shard) {
+    if (shard != null)
+      MDC.put(SHARD_ID_PROP, shard);
+  }
+
+  public static void setReplica(String replica) {
+    if (replica != null)
+      MDC.put(REPLICA_PROP, replica);
+  }
+
+  public static void setCore(String core) {
+    if (core != null)
+      MDC.put(CORE_NAME_PROP, core);
+  }
+
+  public static void clearMDC() {
+    MDC.remove(COLLECTION_PROP);
+    MDC.remove(CORE_NAME_PROP);
+    MDC.remove(REPLICA_PROP);
+    MDC.remove(SHARD_ID_PROP);
+  }
+}

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java?rev=1668992&r1=1668991&r2=1668992&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java Tue
Mar 24 21:01:44 2015
@@ -60,6 +60,7 @@ import org.apache.solr.core.SolrCore;
 import org.apache.solr.core.SolrResourceLoader;
 import org.apache.solr.core.SolrXmlConfig;
 import org.apache.solr.handler.ContentStreamHandlerBase;
+import org.apache.solr.logging.MDCUtils;
 import org.apache.solr.request.SolrQueryRequest;
 import org.apache.solr.request.SolrQueryRequestBase;
 import org.apache.solr.request.SolrRequestHandler;
@@ -73,7 +74,6 @@ import org.apache.solr.update.processor.
 import org.apache.solr.util.RTimer;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
-
 import javax.servlet.FilterChain;
 import javax.servlet.FilterConfig;
 import javax.servlet.ServletException;
@@ -81,6 +81,7 @@ import javax.servlet.ServletRequest;
 import javax.servlet.ServletResponse;
 import javax.servlet.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
+
 import java.io.ByteArrayInputStream;
 import java.io.EOFException;
 import java.io.IOException;
@@ -219,6 +220,7 @@ public class SolrDispatchFilter extends
   }
   
   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain,
boolean retry) throws IOException, ServletException {
+    MDCUtils.clearMDC();
 
     if (abortErrorMessage != null) {
       sendError((HttpServletResponse) response, 500, abortErrorMessage);
@@ -305,11 +307,14 @@ public class SolrDispatchFilter extends
 
             if (core != null) {
               path = path.substring( idx );
+              MDCUtils.setCore(core.getName());
             }
           }
           if (core == null) {
             if (!cores.isZooKeeperAware() ) {
               core = cores.getCore("");
+              if (core != null)
+                MDCUtils.setCore(core.getName());
             }
           }
         }
@@ -321,6 +326,7 @@ public class SolrDispatchFilter extends
           if (core != null) {
             // we found a core, update the path
             path = path.substring( idx );
+            MDCUtils.setCore(core.getName());
           }
           
           // if we couldn't find it locally, look on other nodes
@@ -355,6 +361,7 @@ public class SolrDispatchFilter extends
           // try the default core
           if (core == null) {
             core = cores.getCore("");
+            MDCUtils.setCore(core.getName());
           }
         }
 

Modified: lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SolrLogLayout.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SolrLogLayout.java?rev=1668992&r1=1668991&r2=1668992&view=diff
==============================================================================
--- lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SolrLogLayout.java (original)
+++ lucene/dev/trunk/solr/core/src/java/org/apache/solr/util/SolrLogLayout.java Tue Mar 24
21:01:44 2015
@@ -11,13 +11,18 @@ import org.apache.log4j.spi.LoggingEvent
 import org.apache.log4j.spi.ThrowableInformation;
 import org.apache.solr.cloud.ZkController;
 import org.apache.solr.common.SolrException;
-import org.apache.solr.common.cloud.ClusterState;
-import org.apache.solr.common.cloud.DocCollection;
 import org.apache.solr.common.cloud.Replica;
-import org.apache.solr.common.cloud.Slice;
+import org.apache.solr.common.cloud.ZkStateReader;
 import org.apache.solr.core.SolrCore;
+import org.apache.solr.logging.MDCUtils;
 import org.apache.solr.request.SolrQueryRequest;
 import org.apache.solr.request.SolrRequestInfo;
+import org.slf4j.MDC;
+
+import static org.apache.solr.common.cloud.ZkStateReader.COLLECTION_PROP;
+import static org.apache.solr.common.cloud.ZkStateReader.CORE_NAME_PROP;
+import static org.apache.solr.common.cloud.ZkStateReader.REPLICA_PROP;
+import static org.apache.solr.common.cloud.ZkStateReader.SHARD_ID_PROP;
 
 /*
  * Licensed to the Apache Software Foundation (ASF) under one or more
@@ -107,7 +112,7 @@ public class SolrLogLayout extends Layou
     sb.append(" T");
     sb.append(th.getId());
   }
-  
+
   @Override
   public String format(LoggingEvent event) {
     return _format(event);
@@ -183,7 +188,9 @@ public class SolrLogLayout extends Layou
     // useful for sequencing when looking at multiple parts of a log file, but
     // ms since start should be fine.
     appendThread(sb, event);
-    
+
+    appendMDC(sb);
+
     if (info != null) {
       sb.append(' ').append(info.shortId); // core
     }
@@ -361,4 +368,17 @@ public class SolrLogLayout extends Layou
   public boolean ignoresThrowable() {
     return false;
   }
+
+
+  private void appendMDC(StringBuilder sb) {
+    sb.append(" [" + getMDCValueOrEmpty(COLLECTION_PROP) + "] ");
+    sb.append("[" + getMDCValueOrEmpty(SHARD_ID_PROP) + "] ");
+    sb.append("[" + getMDCValueOrEmpty(REPLICA_PROP) + "] ");
+    sb.append("[" + getMDCValueOrEmpty(CORE_NAME_PROP)+"] ");
+  }
+
+  private String getMDCValueOrEmpty(String key) {
+    String val = MDC.get(key);
+    return val==null? "": val;
+  }
 }

Modified: lucene/dev/trunk/solr/server/resources/log4j.properties
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/server/resources/log4j.properties?rev=1668992&r1=1668991&r2=1668992&view=diff
==============================================================================
--- lucene/dev/trunk/solr/server/resources/log4j.properties (original)
+++ lucene/dev/trunk/solr/server/resources/log4j.properties Tue Mar 24 21:01:44 2015
@@ -5,7 +5,7 @@ log4j.rootLogger=INFO, file, CONSOLE
 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
 
 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
-log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x \u2013 %m%n
+log4j.appender.CONSOLE.layout.ConversionPattern=%-4r [%t] %-5p %c %x [%X{collection}] [%X{shard}]
[%X{replica}] [%X{core}] \u2013 %m%n
 
 #- size rotation with log cleanup.
 log4j.appender.file=org.apache.log4j.RollingFileAppender

Modified: lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrException.java
URL: http://svn.apache.org/viewvc/lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrException.java?rev=1668992&r1=1668991&r2=1668992&view=diff
==============================================================================
--- lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrException.java (original)
+++ lucene/dev/trunk/solr/solrj/src/java/org/apache/solr/common/SolrException.java Tue Mar
24 21:01:44 2015
@@ -19,18 +19,21 @@ package org.apache.solr.common;
 
 import java.io.CharArrayWriter;
 import java.io.PrintWriter;
+import java.util.Map;
 import java.util.Set;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
-
 import org.apache.solr.common.util.NamedList;
 import org.slf4j.Logger;
+import org.slf4j.MDC;
 
 /**
  *
  */
 public class SolrException extends RuntimeException {
 
+  final private Map mdcContext;
+
   /**
    * This list of valid HTTP Status error codes that Solr may return in 
    * the case of a "Server Side" error.
@@ -65,15 +68,18 @@ public class SolrException extends Runti
   public SolrException(ErrorCode code, String msg) {
     super(msg);
     this.code = code.code;
+    this.mdcContext = MDC.getCopyOfContextMap();
   }
   public SolrException(ErrorCode code, String msg, Throwable th) {
     super(msg, th);
     this.code = code.code;
+    this.mdcContext = MDC.getCopyOfContextMap();
   }
 
   public SolrException(ErrorCode code, Throwable th) {
     super(th);
     this.code = code.code;
+    this.mdcContext = MDC.getCopyOfContextMap();
   }
 
   /**
@@ -84,6 +90,7 @@ public class SolrException extends Runti
   protected SolrException(int code, String msg, Throwable th) {
     super(msg, th);
     this.code = code;
+    this.mdcContext = MDC.getCopyOfContextMap();
   }
   
   int code=0;
@@ -205,4 +212,34 @@ public class SolrException extends Runti
     return t;
   }
 
+  public void logInfoWithMdc(Logger logger, String msg) {
+    Map previousMdcContext = MDC.getCopyOfContextMap();
+    MDC.setContextMap(mdcContext);
+    try {
+      logger.info(msg);
+    } finally{
+      MDC.setContextMap(previousMdcContext);
+    }
+  }
+
+  public void logDebugWithMdc(Logger logger, String msg) {
+    Map previousMdcContext = MDC.getCopyOfContextMap();
+    MDC.setContextMap(mdcContext);
+    try {
+      logger.debug(msg);
+    } finally{
+      MDC.setContextMap(previousMdcContext);
+    }
+  }
+
+  public void logWarnWithMdc(Logger logger, String msg) {
+    Map previousMdcContext = MDC.getCopyOfContextMap();
+    MDC.setContextMap(mdcContext);
+    try {
+      logger.warn(msg);
+    } finally{
+      MDC.setContextMap(previousMdcContext);
+    }
+  }
+
 }



Mime
View raw message