From 33db4de4d7d5e325f8bfd886d3957735b33310a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jan=20H=C3=B8ydahl?= Date: Fri, 14 Oct 2016 23:19:09 +0200 Subject: [PATCH] SOLR-9325: solr.log is now written to $SOLR_LOGS_DIR without changing log4j.properties --- solr/CHANGES.txt | 2 ++ solr/bin/solr | 18 +++++++++-- solr/bin/solr.cmd | 31 +++++++++++++++---- solr/bin/solr.in.cmd | 3 ++ solr/bin/solr.in.sh | 5 ++- .../solr/servlet/SolrDispatchFilter.java | 1 + .../solr/servlet/StartupLoggingUtils.java | 9 ++++++ solr/server/resources/log4j.properties | 2 +- 8 files changed, 59 insertions(+), 12 deletions(-) diff --git a/solr/CHANGES.txt b/solr/CHANGES.txt index 328077861a6..2bee12ec1b0 100644 --- a/solr/CHANGES.txt +++ b/solr/CHANGES.txt @@ -171,6 +171,8 @@ Bug Fixes * SOLR-9604: Pooled SSL connections were not being re-used (Alan Woodward, Mikhail Khludnev, hossman) +* SOLR-9325: solr.log is now written to $SOLR_LOGS_DIR without changing log4j.properties (janhoy) + Optimizations ---------------------- diff --git a/solr/bin/solr b/solr/bin/solr index f753d7da8cb..90fffa15251 100755 --- a/solr/bin/solr +++ b/solr/bin/solr @@ -1568,7 +1568,7 @@ function launch_solr() { fi SOLR_START_OPTS=('-server' "${JAVA_MEM_OPTS[@]}" "${GC_TUNE[@]}" "${GC_LOG_OPTS[@]}" \ - "${REMOTE_JMX_OPTS[@]}" "${CLOUD_MODE_OPTS[@]}" $SOLR_LOG_LEVEL_OPT \ + "${REMOTE_JMX_OPTS[@]}" "${CLOUD_MODE_OPTS[@]}" $SOLR_LOG_LEVEL_OPT -Dsolr.log.dir="$SOLR_LOGS_DIR" \ "-Djetty.port=$SOLR_PORT" "-DSTOP.PORT=$stop_port" "-DSTOP.KEY=$STOP_KEY" \ "${SOLR_HOST_ARG[@]}" "-Duser.timezone=$SOLR_TIMEZONE" \ "-Djetty.home=$SOLR_SERVER_DIR" "-Dsolr.solr.home=$SOLR_HOME" "-Dsolr.install.dir=$SOLR_TIP" \ @@ -1578,7 +1578,21 @@ function launch_solr() { IN_CLOUD_MODE=" in SolrCloud mode" fi - mkdir -p "$SOLR_LOGS_DIR" + mkdir -p "$SOLR_LOGS_DIR" 2>/dev/null + if [ $? -ne 0 ]; then + echo -e "\nERROR: Logs directory $SOLR_LOGS_DIR could not be created. Exiting" + exit 1 + fi + if [ ! -w "$SOLR_LOGS_DIR" ]; then + echo -e "\nERROR: Logs directory $SOLR_LOGS_DIR is not writable. Exiting" + exit 1 + fi + case "$SOLR_LOGS_DIR" in + contexts|etc|lib|modules|resources|scripts|solr|solr-webapp) + echo -e "\nERROR: Logs directory $SOLR_LOGS_DIR is invalid. Reserved for the system. Exiting" + exit 1 + ;; + esac if [ "$run_in_foreground" == "true" ]; then exec "$JAVA" "${SOLR_START_OPTS[@]}" $SOLR_ADDL_ARGS -jar start.jar "${SOLR_JETTY_CONFIG[@]}" diff --git a/solr/bin/solr.cmd b/solr/bin/solr.cmd index 0bfb77348a3..53761436657 100644 --- a/solr/bin/solr.cmd +++ b/solr/bin/solr.cmd @@ -729,7 +729,13 @@ IF "%STOP_KEY%"=="" set STOP_KEY=solrrocks @REM This is quite hacky, but examples rely on a different log4j.properties @REM so that we can write logs for examples to %SOLR_HOME%\..\logs -set "SOLR_LOGS_DIR=%SOLR_SERVER_DIR%\logs" +IF [%SOLR_LOGS_DIR%] == [] ( + set "SOLR_LOGS_DIR=%SOLR_SERVER_DIR%\logs" +) ELSE ( + set SOLR_LOGS_DIR=%SOLR_LOGS_DIR:"=% +) +set SOLR_LOGS_DIR_QUOTED="%SOLR_LOGS_DIR%" + set "EXAMPLE_DIR=%SOLR_TIP%\example" set TMP=!SOLR_HOME:%EXAMPLE_DIR%=! IF NOT "%TMP%"=="%SOLR_HOME%" ( @@ -965,13 +971,26 @@ IF NOT "%SOLR_SSL_OPTS%"=="" ( 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%" - +set "START_OPTS=%START_OPTS% -Dsolr.log.dir=%SOLR_LOGS_DIR_QUOTED%" IF NOT DEFINED LOG4J_CONFIG set "LOG4J_CONFIG=file:%SOLR_SERVER_DIR%\resources\log4j.properties" cd /d "%SOLR_SERVER_DIR%" -IF NOT EXIST "!SOLR_LOGS_DIR!" ( - mkdir "!SOLR_LOGS_DIR!" +IF NOT EXIST "%SOLR_LOGS_DIR%" ( + mkdir "%SOLR_LOGS_DIR%" +) +copy /Y NUL "%SOLR_LOGS_DIR%\.writable" > NUL 2>&1 && set WRITEOK=1 +IF DEFINED WRITEOK ( + del "%SOLR_LOGS_DIR%\.writable" +) else ( + echo "ERROR: Logs directory %SOLR_LOGS_DIR% is not writable or could not be created. Exiting" + GOTO :eof +) +echo " contexts etc lib modules resources scripts solr solr-webapp " > "%TEMP%\solr-pattern.txt" +findstr /i /C:" %SOLR_LOGS_DIR% " "%TEMP%\solr-pattern.txt" 1>nul +if %ERRORLEVEL% == 0 ( + echo "ERROR: Logs directory %SOLR_LOGS_DIR% is invalid. Reserved for the system. Exiting" + GOTO :eof ) IF NOT EXIST "%SOLR_SERVER_DIR%\tmp" ( @@ -988,13 +1007,13 @@ IF "%FG%"=="1" ( REM run solr in the foreground title "Solr-%SOLR_PORT%" echo %SOLR_PORT%>"%SOLR_TIP%"\bin\solr-%SOLR_PORT%.port - "%JAVA%" %SERVEROPT% %SOLR_JAVA_MEM% %START_OPTS% %GCLOG_OPT%:"!SOLR_LOGS_DIR!"/solr_gc.log ^ + "%JAVA%" %SERVEROPT% %SOLR_JAVA_MEM% %START_OPTS% %GCLOG_OPT%:"!SOLR_LOGS_DIR!/solr_gc.log" ^ -Dlog4j.configuration="%LOG4J_CONFIG%" -DSTOP.PORT=!STOP_PORT! -DSTOP.KEY=%STOP_KEY% ^ -Djetty.port=%SOLR_PORT% -Dsolr.solr.home="%SOLR_HOME%" -Dsolr.install.dir="%SOLR_TIP%" ^ -Djetty.home="%SOLR_SERVER_DIR%" -Djava.io.tmpdir="%SOLR_SERVER_DIR%\tmp" -jar start.jar "%SOLR_JETTY_CONFIG%" ) ELSE ( START /B "Solr-%SOLR_PORT%" /D "%SOLR_SERVER_DIR%" "%JAVA%" %SERVEROPT% %SOLR_JAVA_MEM% %START_OPTS% ^ - %GCLOG_OPT%:"!SOLR_LOGS_DIR!"/solr_gc.log -Dlog4j.configuration="%LOG4J_CONFIG%" -DSTOP.PORT=!STOP_PORT! ^ + %GCLOG_OPT%:"!SOLR_LOGS_DIR!/solr_gc.log" -Dlog4j.configuration="%LOG4J_CONFIG%" -DSTOP.PORT=!STOP_PORT! ^ -Dsolr.log.muteconsole ^ -DSTOP.KEY=%STOP_KEY% -Djetty.port=%SOLR_PORT% -Dsolr.solr.home="%SOLR_HOME%" -Dsolr.install.dir="%SOLR_TIP%" ^ -Djetty.home="%SOLR_SERVER_DIR%" -Djava.io.tmpdir="%SOLR_SERVER_DIR%\tmp" -jar start.jar ^ diff --git a/solr/bin/solr.in.cmd b/solr/bin/solr.in.cmd index 07bffccd19e..5a2d00f2735 100644 --- a/solr/bin/solr.in.cmd +++ b/solr/bin/solr.in.cmd @@ -31,6 +31,9 @@ REM Changes the logging level. Valid values: ALL, TRACE, DEBUG, INFO, WARN, ERRO REM This is an alternative to changing the rootLogger in log4j.properties REM set SOLR_LOG_LEVEL=INFO +REM Location where Solr should write logs to. Absolute or relative to solr start dir +REM set SOLR_LOGS_DIR=logs + REM These GC settings have shown to work well for a number of common Solr workloads set GC_TUNE=-XX:NewRatio=3 ^ -XX:SurvivorRatio=4 ^ diff --git a/solr/bin/solr.in.sh b/solr/bin/solr.in.sh index 999e6731bfe..dd41f46385e 100644 --- a/solr/bin/solr.in.sh +++ b/solr/bin/solr.in.sh @@ -96,9 +96,8 @@ SOLR_OPTS="$SOLR_OPTS -Xss256k" # 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 -# settings in server/resources/log4j.properties -#SOLR_LOGS_DIR= +# Location where Solr should write logs to. Absolute or relative to solr start dir +#SOLR_LOGS_DIR=logs # Sets the port Solr binds to, default is 8983 #SOLR_PORT=8983 diff --git a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java index 241749a3868..b9d37292ca8 100644 --- a/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java +++ b/solr/core/src/java/org/apache/solr/servlet/SolrDispatchFilter.java @@ -123,6 +123,7 @@ public class SolrDispatchFilter extends BaseSolrFilter { { log.trace("SolrDispatchFilter.init(): {}", this.getClass().getClassLoader()); + StartupLoggingUtils.checkLogDir(); logWelcomeBanner(); String muteConsole = System.getProperty(SOLR_LOG_MUTECONSOLE); if (muteConsole != null && !Arrays.asList("false","0","off","no").contains(muteConsole.toLowerCase(Locale.ROOT))) { diff --git a/solr/core/src/java/org/apache/solr/servlet/StartupLoggingUtils.java b/solr/core/src/java/org/apache/solr/servlet/StartupLoggingUtils.java index feb88e97a8f..3da0841d4d5 100644 --- a/solr/core/src/java/org/apache/solr/servlet/StartupLoggingUtils.java +++ b/solr/core/src/java/org/apache/solr/servlet/StartupLoggingUtils.java @@ -40,6 +40,15 @@ final class StartupLoggingUtils { private static final Logger log = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass()); private final static StaticLoggerBinder binder = StaticLoggerBinder.getSingleton(); + /** + * Checks whether mandatory log dir is given + */ + static void checkLogDir() { + if (System.getProperty("solr.log.dir") == null) { + log.error("Missing Java Option solr.log.dir. Logging may be missing or incomplete."); + } + } + /** * Disables all log4j ConsoleAppender's by modifying log4j configuration dynamically. * Must only be used during early startup diff --git a/solr/server/resources/log4j.properties b/solr/server/resources/log4j.properties index 9a5e3fdea15..9f9c4a0f749 100644 --- a/solr/server/resources/log4j.properties +++ b/solr/server/resources/log4j.properties @@ -1,6 +1,6 @@ # Default Solr log4j config # rootLogger log level may be programmatically overridden by -Dsolr.log.level -solr.log=logs +solr.log=${solr.log.dir} log4j.rootLogger=INFO, file, CONSOLE # Console appender will be programmatically disabled when Solr is started with option -Dsolr.log.muteconsole