SOLR-9534: You can now set Solr's log level through environment variable SOLR_LOG_LEVEL and -q and -v options to bin/solr

This commit is contained in:
Jan Høydahl 2016-09-22 21:05:28 +02:00
parent 5183b67e94
commit 73c2edddf0
8 changed files with 84 additions and 6 deletions

View File

@ -92,6 +92,9 @@ New Features
* SOLR-8487: Adds CommitStream to support sending commits to a collection being updated. (Dennis Gove) * SOLR-8487: Adds CommitStream to support sending commits to a collection being updated. (Dennis Gove)
* SOLR-9534: You can now set Solr's log level through environment variable SOLR_LOG_LEVEL
Also adds conveience arguments -q (quiet: WARN) and -v (verbose: DEBUG) to bin/solr (janhoy)
Bug Fixes Bug Fixes
---------------------- ----------------------

View File

@ -258,7 +258,9 @@ function print_usage() {
echo "" echo ""
echo " -noprompt Don't prompt for input; accept all defaults when running examples that accept user input" echo " -noprompt Don't prompt for input; accept all defaults when running examples that accept user input"
echo "" echo ""
echo " -V Verbose messages from this script" echo " -v and -q Verbose (-v) or quiet (-q) logging. Sets default log level to DEBUG or WARN instead of INFO"
echo ""
echo " -V or -version Verbose messages from this script"
echo "" echo ""
elif [ "$CMD" == "stop" ]; then elif [ "$CMD" == "stop" ]; then
echo "" echo ""
@ -1160,6 +1162,14 @@ if [ $# -gt 0 ]; then
PASS_TO_RUN_EXAMPLE+=" --verbose" PASS_TO_RUN_EXAMPLE+=" --verbose"
shift shift
;; ;;
-v)
SOLR_LOG_LEVEL=DEBUG
shift
;;
-q)
SOLR_LOG_LEVEL=WARN
shift
;;
-all) -all)
stop_all=true stop_all=true
shift shift
@ -1187,6 +1197,10 @@ if [ $# -gt 0 ]; then
done done
fi fi
if [[ $SOLR_LOG_LEVEL ]] ; then
SOLR_LOG_LEVEL_OPT="-Dsolr.log.level=$SOLR_LOG_LEVEL"
fi
if [ -z "$SOLR_SERVER_DIR" ]; then if [ -z "$SOLR_SERVER_DIR" ]; then
SOLR_SERVER_DIR="$DEFAULT_SERVER_DIR" SOLR_SERVER_DIR="$DEFAULT_SERVER_DIR"
fi fi
@ -1518,6 +1532,11 @@ function launch_solr() {
echo -e " RMI_PORT = $RMI_PORT" echo -e " RMI_PORT = $RMI_PORT"
echo -e " REMOTE_JMX_OPTS = ${REMOTE_JMX_OPTS[@]}" echo -e " REMOTE_JMX_OPTS = ${REMOTE_JMX_OPTS[@]}"
fi fi
if [ "$SOLR_LOG_LEVEL" != "" ]; then
echo -e " SOLR_LOG_LEVEL = $SOLR_LOG_LEVEL"
fi
echo -e "\n" echo -e "\n"
fi fi
@ -1530,7 +1549,7 @@ function launch_solr() {
fi fi
SOLR_START_OPTS=('-server' "${JAVA_MEM_OPTS[@]}" "${GC_TUNE[@]}" "${GC_LOG_OPTS[@]}" \ SOLR_START_OPTS=('-server' "${JAVA_MEM_OPTS[@]}" "${GC_TUNE[@]}" "${GC_LOG_OPTS[@]}" \
"${REMOTE_JMX_OPTS[@]}" "${CLOUD_MODE_OPTS[@]}" \ "${REMOTE_JMX_OPTS[@]}" "${CLOUD_MODE_OPTS[@]}" $SOLR_LOG_LEVEL_OPT \
"-Djetty.port=$SOLR_PORT" "-DSTOP.PORT=$stop_port" "-DSTOP.KEY=$STOP_KEY" \ "-Djetty.port=$SOLR_PORT" "-DSTOP.PORT=$stop_port" "-DSTOP.KEY=$STOP_KEY" \
"${SOLR_HOST_ARG[@]}" "-Duser.timezone=$SOLR_TIMEZONE" \ "${SOLR_HOST_ARG[@]}" "-Duser.timezone=$SOLR_TIMEZONE" \
"-Djetty.home=$SOLR_SERVER_DIR" "-Dsolr.solr.home=$SOLR_HOME" "-Dsolr.install.dir=$SOLR_TIP" \ "-Djetty.home=$SOLR_SERVER_DIR" "-Dsolr.solr.home=$SOLR_HOME" "-Dsolr.install.dir=$SOLR_TIP" \

View File

@ -222,6 +222,8 @@ goto done
@echo. @echo.
@echo -noprompt Don't prompt for input; accept all defaults when running examples that accept user input @echo -noprompt Don't prompt for input; accept all defaults when running examples that accept user input
@echo. @echo.
@echo -v and -q Verbose (-v) or quiet (-q) logging. Sets default log level to DEBUG or WARN instead of INFO
@echo.
@echo -V Verbose messages from this script @echo -V Verbose messages from this script
@echo. @echo.
goto done goto done
@ -439,6 +441,8 @@ IF "%1"=="-f" goto set_foreground_mode
IF "%1"=="-foreground" goto set_foreground_mode IF "%1"=="-foreground" goto set_foreground_mode
IF "%1"=="-V" goto set_verbose IF "%1"=="-V" goto set_verbose
IF "%1"=="-verbose" goto set_verbose IF "%1"=="-verbose" goto set_verbose
IF "%1"=="-v" goto set_debug
IF "%1"=="-q" goto set_warn
IF "%1"=="-c" goto set_cloud_mode IF "%1"=="-c" goto set_cloud_mode
IF "%1"=="-cloud" goto set_cloud_mode IF "%1"=="-cloud" goto set_cloud_mode
IF "%1"=="-d" goto set_server_dir IF "%1"=="-d" goto set_server_dir
@ -481,6 +485,16 @@ set "PASS_TO_RUN_EXAMPLE=--verbose !PASS_TO_RUN_EXAMPLE!"
SHIFT SHIFT
goto parse_args goto parse_args
:set_debug
set SOLR_LOG_LEVEL=DEBUG
SHIFT
goto parse_args
:set_warn
set SOLR_LOG_LEVEL=WARN
SHIFT
goto parse_args
:set_cloud_mode :set_cloud_mode
set SOLR_MODE=solrcloud set SOLR_MODE=solrcloud
SHIFT SHIFT
@ -931,6 +945,10 @@ IF "%verbose%"=="1" (
@echo REMOTE_JMX_OPTS = %REMOTE_JMX_OPTS% @echo REMOTE_JMX_OPTS = %REMOTE_JMX_OPTS%
) )
IF NOT "%SOLR_LOG_LEVEL%"=="" (
@echo SOLR_LOG_LEVEL = !SOLR_LOG_LEVEL!
)
@echo. @echo.
) )
@ -945,6 +963,7 @@ IF NOT "%SOLR_SSL_OPTS%"=="" (
set "SSL_PORT_PROP=-Dsolr.jetty.https.port=%SOLR_PORT%" set "SSL_PORT_PROP=-Dsolr.jetty.https.port=%SOLR_PORT%"
set "START_OPTS=%START_OPTS% %SOLR_SSL_OPTS% !SSL_PORT_PROP!" set "START_OPTS=%START_OPTS% %SOLR_SSL_OPTS% !SSL_PORT_PROP!"
) )
IF NOT "%SOLR_LOG_LEVEL%"=="" set "START_OPTS=%START_OPTS% -Dsolr.log.level=%SOLR_LOG_LEVEL%"
IF NOT DEFINED LOG4J_CONFIG set "LOG4J_CONFIG=file:%SOLR_SERVER_DIR%\resources\log4j.properties" IF NOT DEFINED LOG4J_CONFIG set "LOG4J_CONFIG=file:%SOLR_SERVER_DIR%\resources\log4j.properties"

View File

@ -27,6 +27,10 @@ set SOLR_JAVA_MEM=-Xms512m -Xmx512m
REM Enable verbose GC logging REM Enable verbose GC logging
set GC_LOG_OPTS=-verbose:gc -XX:+PrintHeapAtGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime set GC_LOG_OPTS=-verbose:gc -XX:+PrintHeapAtGC -XX:+PrintGCDetails -XX:+PrintGCDateStamps -XX:+PrintGCTimeStamps -XX:+PrintTenuringDistribution -XX:+PrintGCApplicationStoppedTime
REM Changes the logging level. Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. Default is INFO
REM This is an alternative to changing the rootLogger in log4j.properties
REM set SOLR_LOG_LEVEL=INFO
REM These GC settings have shown to work well for a number of common Solr workloads REM These GC settings have shown to work well for a number of common Solr workloads
set GC_TUNE=-XX:NewRatio=3 ^ set GC_TUNE=-XX:NewRatio=3 ^
-XX:SurvivorRatio=4 ^ -XX:SurvivorRatio=4 ^

View File

@ -91,6 +91,10 @@ SOLR_OPTS="$SOLR_OPTS -Xss256k"
# so you can point the script to use a different log4j.properties file # so you can point the script to use a different log4j.properties file
#LOG4J_PROPS=/var/solr/log4j.properties #LOG4J_PROPS=/var/solr/log4j.properties
# Changes the logging level. Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF. Default is INFO
# This is an alternative to changing the rootLogger in log4j.properties
#SOLR_LOG_LEVEL=INFO
# Location where Solr should write logs to; should agree with the file appender # Location where Solr should write logs to; should agree with the file appender
# settings in server/resources/log4j.properties # settings in server/resources/log4j.properties
#SOLR_LOGS_DIR= #SOLR_LOGS_DIR=

View File

@ -114,6 +114,8 @@ public class SolrDispatchFilter extends BaseSolrFilter {
public static final String SOLR_LOG_MUTECONSOLE = "solr.log.muteconsole"; public static final String SOLR_LOG_MUTECONSOLE = "solr.log.muteconsole";
public static final String SOLR_LOG_LEVEL = "solr.log.level";
@Override @Override
public void init(FilterConfig config) throws ServletException public void init(FilterConfig config) throws ServletException
{ {
@ -122,7 +124,10 @@ public class SolrDispatchFilter extends BaseSolrFilter {
if (muteConsole != null && !Arrays.asList("false","0","off","no").contains(muteConsole.toLowerCase(Locale.ROOT))) { if (muteConsole != null && !Arrays.asList("false","0","off","no").contains(muteConsole.toLowerCase(Locale.ROOT))) {
StartupLoggingUtils.muteConsole(); StartupLoggingUtils.muteConsole();
} }
log.info("SolrDispatchFilter.init(): {}", this.getClass().getClassLoader()); String logLevel = System.getProperty(SOLR_LOG_LEVEL);
if (logLevel != null) {
StartupLoggingUtils.changeLogLevel(logLevel);
}
String exclude = config.getInitParameter("excludePatterns"); String exclude = config.getInitParameter("excludePatterns");
if(exclude != null) { if(exclude != null) {

View File

@ -22,6 +22,7 @@ import java.util.Enumeration;
import org.apache.log4j.Appender; import org.apache.log4j.Appender;
import org.apache.log4j.ConsoleAppender; import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Level;
import org.apache.log4j.LogManager; import org.apache.log4j.LogManager;
import org.apache.solr.common.util.SuppressForbidden; import org.apache.solr.common.util.SuppressForbidden;
import org.slf4j.Logger; import org.slf4j.Logger;
@ -67,6 +68,27 @@ final class StartupLoggingUtils {
} }
} }
/**
* Dynamically change log4j log level through property solr.log.level
* @param logLevel String with level, should be one of the supported, e.g. TRACE, DEBUG, INFO, WARN, ERROR...
* @return true if ok or else false if something happened, e.g. log4j classes were not in classpath
*/
@SuppressForbidden(reason = "Legitimate log4j access")
static boolean changeLogLevel(String logLevel) {
try {
if (!isLog4jActive()) {
logNotSupported("Could not mute logging to console.");
return false;
}
log.info("Log level override, property solr.log.level=" + logLevel);
LogManager.getRootLogger().setLevel(Level.toLevel(logLevel, Level.INFO));
return true;
} catch (Exception e) {
logNotSupported("Could not change log level.");
return false;
}
}
private static boolean isLog4jActive() { private static boolean isLog4jActive() {
try { try {
// Make sure we have log4j LogManager in classpath // Make sure we have log4j LogManager in classpath

View File

@ -1,4 +1,5 @@
# Logging level # Default Solr log4j config
# rootLogger log level may be programmatically overridden by -Dsolr.log.level
solr.log=logs solr.log=logs
log4j.rootLogger=INFO, file, CONSOLE log4j.rootLogger=INFO, file, CONSOLE
@ -17,11 +18,12 @@ log4j.appender.file.File=${solr.log}/solr.log
log4j.appender.file.layout=org.apache.log4j.EnhancedPatternLayout log4j.appender.file.layout=org.apache.log4j.EnhancedPatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p (%t) [%X{collection} %X{shard} %X{replica} %X{core}] %c{1.} %m%n log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss.SSS} %-5p (%t) [%X{collection} %X{shard} %X{replica} %X{core}] %c{1.} %m%n
# Adjust logging levels that should differ from root logger
log4j.logger.org.apache.zookeeper=WARN log4j.logger.org.apache.zookeeper=WARN
log4j.logger.org.apache.hadoop=WARN log4j.logger.org.apache.hadoop=WARN
log4j.logger.org.eclipse.jetty=WARN log4j.logger.org.eclipse.jetty=WARN
log4j.logger.org.eclipse.jetty.server=INFO log4j.logger.org.eclipse.jetty.server.Server=INFO
log4j.logger.org.eclipse.jetty.server.handler=WARN log4j.logger.org.eclipse.jetty.server.ServerConnector=INFO
# set to INFO to enable infostream log messages # set to INFO to enable infostream log messages
log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF log4j.logger.org.apache.solr.update.LoggingInfoStream=OFF