Skip to content

Commit

Permalink
global log4j config (#23631)
Browse files Browse the repository at this point in the history
  • Loading branch information
dotcmsbuild committed Jan 31, 2023
1 parent 6590a84 commit f4dcc83
Show file tree
Hide file tree
Showing 8 changed files with 305 additions and 12 deletions.
104 changes: 104 additions & 0 deletions bin/setclasspath.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
@echo off
rem Licensed to the Apache Software Foundation (ASF) under one or more
rem contributor license agreements. See the NOTICE file distributed with
rem this work for additional information regarding copyright ownership.
rem The ASF licenses this file to You under the Apache License, Version 2.0
rem (the "License"); you may not use this file except in compliance with
rem the License. You may obtain a copy of the License at
rem
rem http://www.apache.org/licenses/LICENSE-2.0
rem
rem Unless required by applicable law or agreed to in writing, software
rem distributed under the License is distributed on an "AS IS" BASIS,
rem WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
rem See the License for the specific language governing permissions and
rem limitations under the License.

rem ---------------------------------------------------------------------------
rem Set JAVA_HOME or JRE_HOME if not already set, ensure any provided settings
rem are valid and consistent with the selected start-up options and set up the
rem endorsed directory.
rem ---------------------------------------------------------------------------

rem Make sure prerequisite environment variables are set

rem In debug mode we need a real JDK (JAVA_HOME)
if ""%1"" == ""debug"" goto needJavaHome

rem Otherwise either JRE or JDK are fine
if not "%JRE_HOME%" == "" goto gotJreHome
if not "%JAVA_HOME%" == "" goto gotJavaHome
echo Neither the JAVA_HOME nor the JRE_HOME environment variable is defined
echo At least one of these environment variable is needed to run this program
goto exit

:needJavaHome
rem Check if we have a usable JDK
if "%JAVA_HOME%" == "" goto noJavaHome
if not exist "%JAVA_HOME%\bin\java.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\jdb.exe" goto noJavaHome
if not exist "%JAVA_HOME%\bin\javac.exe" goto noJavaHome
set "JRE_HOME=%JAVA_HOME%"
goto okJava

:noJavaHome
echo The JAVA_HOME environment variable is not defined correctly.
echo It is needed to run this program in debug mode.
echo NB: JAVA_HOME should point to a JDK not a JRE.
goto exit

:gotJavaHome
rem No JRE given, use JAVA_HOME as JRE_HOME
set "JRE_HOME=%JAVA_HOME%"

:gotJreHome
rem Check if we have a usable JRE
if not exist "%JRE_HOME%\bin\java.exe" goto noJreHome
goto okJava

:noJreHome
rem Needed at least a JRE
echo The JRE_HOME environment variable is not defined correctly
echo This environment variable is needed to run this program
goto exit

:okJava
rem Don't override the endorsed dir if the user has set it previously
if not "%JAVA_ENDORSED_DIRS%" == "" goto gotEndorseddir
rem Java 9 no longer supports the java.endorsed.dirs
rem system property. Only try to use it if
rem CATALINA_HOME/endorsed exists.
if not exist "%CATALINA_HOME%\endorsed" goto gotEndorseddir
set "JAVA_ENDORSED_DIRS=%CATALINA_HOME%\endorsed"
:gotEndorseddir

rem Don't override _RUNJAVA if the user has set it previously
if not "%_RUNJAVA%" == "" goto gotRunJava
rem Set standard command for invoking Java.
rem Also note the quoting as JRE_HOME may contain spaces.
set _RUNJAVA="%JRE_HOME%\bin\java.exe"
:gotRunJava

rem Don't override _RUNJDB if the user has set it previously
rem Also note the quoting as JAVA_HOME may contain spaces.
if not "%_RUNJDB%" == "" goto gotRunJdb
set _RUNJDB="%JAVA_HOME%\bin\jdb.exe"
:gotRunJdb

if not "%USE_GLOBAL_LOG4J" == "" goto gotGlobalLog4j
if [ "$USE_GLOBAL_LOG4J" = true ]; then
echo Using Global Log4j
set CLASSPATH="%CATALINA_HOME%\log4j2\lib\*;%CATALINA_HOME%\log4j2\conf$CLASSPATH"
set JAVA_OPTS=" -DLog4jContextSelector=org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector %JAVA_OPTS%"
set JAVA_OPTS=" -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager %JAVA_OPTS%"
fi
:gotGlobalLog4j


goto end

:exit
exit /b 1

:end
exit /b 0
105 changes: 105 additions & 0 deletions bin/setclasspath.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
#!/bin/sh

# 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.

# -----------------------------------------------------------------------------
# Set JAVA_HOME or JRE_HOME if not already set, ensure any provided settings
# are valid and consistent with the selected start-up options and set up the
# endorsed directory.
# -----------------------------------------------------------------------------

# Make sure prerequisite environment variables are set
if [ -z "$JAVA_HOME" ] && [ -z "$JRE_HOME" ]; then
if $darwin; then
# Bugzilla 54390
if [ -x '/usr/libexec/java_home' ] ; then
export JAVA_HOME=`/usr/libexec/java_home`
# Bugzilla 37284 (reviewed).
elif [ -d "/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home" ]; then
export JAVA_HOME="/System/Library/Frameworks/JavaVM.framework/Versions/CurrentJDK/Home"
fi
else
JAVA_PATH=`which java 2>/dev/null`
if [ "x$JAVA_PATH" != "x" ]; then
JAVA_PATH=`dirname "$JAVA_PATH" 2>/dev/null`
JRE_HOME=`dirname "$JAVA_PATH" 2>/dev/null`
fi
if [ "x$JRE_HOME" = "x" ]; then
# XXX: Should we try other locations?
if [ -x /usr/bin/java ]; then
JRE_HOME=/usr
fi
fi
fi
if [ -z "$JAVA_HOME" ] && [ -z "$JRE_HOME" ]; then
echo "Neither the JAVA_HOME nor the JRE_HOME environment variable is defined"
echo "At least one of these environment variable is needed to run this program"
exit 1
fi
fi
if [ -z "$JAVA_HOME" ] && [ "$1" = "debug" ]; then
echo "JAVA_HOME should point to a JDK in order to run in debug mode."
exit 1
fi
if [ -z "$JRE_HOME" ]; then
JRE_HOME="$JAVA_HOME"
fi

# If we're running under jdb, we need a full jdk.
if [ "$1" = "debug" ] ; then
if [ "$os400" = "true" ]; then
if [ ! -x "$JAVA_HOME"/bin/java ] || [ ! -x "$JAVA_HOME"/bin/javac ]; then
echo "The JAVA_HOME environment variable is not defined correctly"
echo "This environment variable is needed to run this program"
echo "NB: JAVA_HOME should point to a JDK not a JRE"
exit 1
fi
else
if [ ! -x "$JAVA_HOME"/bin/java ] || [ ! -x "$JAVA_HOME"/bin/jdb ] || [ ! -x "$JAVA_HOME"/bin/javac ]; then
echo "The JAVA_HOME environment variable is not defined correctly"
echo "This environment variable is needed to run this program"
echo "NB: JAVA_HOME should point to a JDK not a JRE"
exit 1
fi
fi
fi

# Don't override the endorsed dir if the user has set it previously
if [ -z "$JAVA_ENDORSED_DIRS" ]; then
# Java 9 no longer supports the java.endorsed.dirs
# system property. Only try to use it if
# CATALINA_HOME/endorsed exists.
if [ -d "$CATALINA_HOME"/endorsed ]; then
JAVA_ENDORSED_DIRS="$CATALINA_HOME"/endorsed
fi
fi

# Set standard commands for invoking Java, if not already set.
if [ -z "$_RUNJAVA" ]; then
_RUNJAVA="$JRE_HOME"/bin/java
fi
if [ "$os400" != "true" ]; then
if [ -z "$_RUNJDB" ]; then
_RUNJDB="$JAVA_HOME"/bin/jdb
fi
fi
echo In set classpath
if [ "$USE_GLOBAL_LOG4J" = true ]; then
echo Using Global Log4j
CLASSPATH="$CATALINA_HOME/log4j2/lib/*:$CATALINA_HOME/log4j2/conf$CLASSPATH"
JAVA_OPTS=" -DLog4jContextSelector=org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector $JAVA_OPTS"
JAVA_OPTS=" -Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager $JAVA_OPTS"
fi
33 changes: 32 additions & 1 deletion dotCMS/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -493,12 +493,37 @@ class DeployWarTomcatTask extends DefaultTask{
}

project.copy{
from '../libs/buildlibs'
from project.file("../libs/buildlibs")
into buildLibLocation + '/lib'
include 'mail.jar'
include 'javax.activation-1.2.0.jar'
}


project.copy {
from "$project.buildDir/resources/main"
into buildLibLocation + '/log4j2/conf'
include "log4j2-tomcat*.xml"
}

project.copy{
from project.configurations.compileClasspath
into buildLibLocation + '/log4j2/lib'
include 'commons-logging-*.jar'
include 'log4j-*.jar'
include 'disruptor-*.jar'
include 'slf4j-api-*.jar'
exclude 'log4j-web-*.jar'
}

project.copy{
from project.file("../bin")
into buildLibLocation + '/bin'
include 'setclasspath.sh'
include 'setclasspath.bat'
}


}
}

Expand Down Expand Up @@ -904,6 +929,12 @@ task createDistPrep(dependsOn: ['clonePullTomcatDist', 'deployWarTomcatDist']) {
exclude "com", "org", "release.properties"
}

copy {
from "$rootDir/../conf/log4j"
into "$distLocation$log4j2ConfDistLocation"
include "log4j*.xml"
}

copy {
into "$distLocation$pluginsDistLocation"
from "src/main/plugins"
Expand Down
20 changes: 13 additions & 7 deletions dotCMS/dependencies.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ dependencies {

def immutablesVersion = "2.9.1"
def byteBuddyVersion = "1.12.6"
def log4jVersion = "2.19.0"

annotationProcessor "org.immutables:value:$immutablesVersion" // <--- this is important

implementation('junit:junit:4.13.2') {
Expand Down Expand Up @@ -49,15 +51,19 @@ dependencies {
implementation group: 'com.dotcms.lib', name: 'dot.counter-ejb', version:'ukv_2'
implementation group: 'com.dotcms.lib', name: 'dot.dwr', version:'3rc2modified_3'

implementation group: 'com.lmax', name: 'disruptor', version: '3.3.4'

implementation group: 'org.apache.commons', name: 'commons-math3', version: '3.6.1'
implementation group: 'org.apache.commons', name: 'commons-numbers-gamma', version: '1.0'
implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: '2.17.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: '2.17.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: '2.17.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: '2.17.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-web', version: '2.17.1'
implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: '2.17.1'

implementation group: 'org.apache.logging.log4j', name: 'log4j-api', version: log4jVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-core', version: log4jVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-1.2-api', version: log4jVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-jcl', version: log4jVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-web', version: log4jVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-slf4j-impl', version: log4jVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-jul', version: log4jVersion
implementation group: 'org.apache.logging.log4j', name: 'log4j-appserver', version: log4jVersion
implementation group: 'com.lmax', name: 'disruptor', version: '3.3.4'

implementation group: 'com.dotcms.lib', name: 'dot.fileupload-ext', version:'ukv_3'
implementation group: 'fop', name: 'fop', version:'0.20.3'
Expand Down
1 change: 1 addition & 0 deletions dotCMS/gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ distBinLocation=/bin
pluginsDistLocation=/plugins
docsDistLocation=/docs
confDistLocation=/bin/system/src-conf
log4j2ConfDistLocation=/log4j2/conf
tomcatDistInstallLocation=dotserver/tomcat
outputDistLocation=../dist-output/
distUpdate=false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import com.dotmarketing.loggers.Log4jUtil;
import com.dotmarketing.quartz.QuartzUtils;
import com.dotmarketing.util.Config;
import com.dotmarketing.util.ConfigUtils;
import com.dotmarketing.util.Logger;
import io.vavr.control.Try;
import org.apache.logging.log4j.core.async.BasicAsyncLoggerContextSelector;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
Expand Down Expand Up @@ -77,8 +77,12 @@ public void contextInitialized(ServletContextEvent arg0) {
Logger.error(this,e.getMessage(),e);
}

//Initialises/reconfigures log4j based on a given log4j configuration file
Log4jUtil.initializeFromPath(path);
// Do not reconfigure if using global configuration. Remove this if we move
// a full global configuration
if (System.getProperty("Log4jContextSelector").equals(BasicAsyncLoggerContextSelector.class.getName()))
Log4jUtil.initializeFromPath(path);
else
Logger.debug(this, "Reinitializing configuration from "+path);


installWebSocket(arg0.getServletContext());
Expand Down
3 changes: 2 additions & 1 deletion dotCMS/src/main/java/com/dotmarketing/loggers/Log4jUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public static void createAndAddConsoleAppender () {
Configuration configuration = loggerContext.getConfiguration();

//Init log4j to see the messages in ant's output
if ( !appenderMap.isEmpty() ) {
if ( appenderMap.isEmpty() ) {

//Create a simple layout for our appender
Layout simpleLayout = PatternLayout.newBuilder()
Expand All @@ -61,6 +61,7 @@ public static void createAndAddConsoleAppender () {
//Create and add a console appender to the configuration
ConsoleAppender consoleAppender = ConsoleAppender.createDefaultAppenderForLayout(simpleLayout);
configuration.addAppender(consoleAppender);
LogManager.getLogger().debug("Added a new ConsoleAppender to the log4j configuration");
}
}

Expand Down
41 changes: 41 additions & 0 deletions dotCMS/src/main/resources/log4j2-tomcat.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- ================================================ -->
<!-- Tomcat log4j2 logger -->
<!-- ================================================ -->
<Configuration status="INFO" monitorInterval="60">
<Properties>
<Property name="logsdir">${sys:catalina.base}/logs</Property>
<Property name="layout">%d %highlight{[%-6p]} [%t] %c{1.} - %m%n</Property>
</Properties>
<Appenders>
<Console name="DEFAULT" target="SYSTEM_OUT">
<PatternLayout pattern="${layout}"/>
</Console>
<!--
<RollingFile name="FILE"
fileName="${logsdir}/worker.log"
filePattern="${logsdir}/catalina.%d{yyyy-MM-dd}-%i.log" immediateFlush="false">
<PatternLayout pattern="${layout}"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="10 MB"/>
</Policies>
<DefaultRolloverStrategy max="100"/>
</RollingFile>
-->
</Appenders>
<Loggers>

<Root level="INFO" includeLocation="true">
<AppenderRef ref="DEFAULT"/>
</Root>

<logger name="org.apache.catalina" level="INFO"/>
<logger name="org.apache.catalina.core" level="INFO"/>
<logger name="org.apache.tomcat" level="INFO"/>
<logger name="org.apache.coyote" level="INFO"/>
<logger name="org.apache.jasper" level="INFO"/>


</Loggers>
</Configuration>

0 comments on commit f4dcc83

Please sign in to comment.