Skip to content

Commit

Permalink
add global log4j initial
Browse files Browse the repository at this point in the history
  • Loading branch information
spbolton committed Aug 21, 2024
1 parent 386274d commit bb53cf5
Show file tree
Hide file tree
Showing 13 changed files with 194 additions and 125 deletions.
7 changes: 7 additions & 0 deletions bom/application/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1433,6 +1433,13 @@

<!-- Apache Tomcat -->

<dependency>
<groupId>com.dotcms</groupId>
<artifactId>tomcat-redis-session-manager</artifactId>
<version>1.3</version>
<scope>provided</scope>
</dependency>

<dependency>
<!-- Jasper is the Tomcat JSP Engine -->
<groupId>org.apache.tomcat</groupId>
Expand Down
43 changes: 30 additions & 13 deletions bom/logging/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
<properties>
<maven.deploy.skip>false</maven.deploy.skip>
<flatten.mode>bom</flatten.mode>
<log4j.version>2.20.0</log4j.version>
<slf4j.version>2.0.7</slf4j.version>
<log4j.version>2.23.1</log4j.version>
<slf4j.version>2.0.9</slf4j.version>
</properties>

<dependencyManagement>
Expand All @@ -25,52 +25,69 @@
<!-- Logging framework support Log4j2 as base logger
very specific artifacts are required to support other frameworks.
Do not include incompatible artifacts e.g. -->

<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<artifactId>log4j-api</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<!-- Support log4j1.2.x API in Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<artifactId>log4j-iostreams</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<!-- Support Java Commons Logging API in Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<artifactId>log4j-core</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<!-- Support SLF4j2 API in Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<artifactId>log4j-appserver</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- Support log4j1.2.x API in Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-1.2-api</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- Support Java Commons Logging API in Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${slf4j.version}</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<version>${log4j.version}</version>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- Servlet container initialization of Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<version>${log4j.version}</version>
<scope>compile</scope>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- Async support for Log4J 2.x -->
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<version>3.3.4</version>
<scope>compile</scope>
<scope>runtime</scope>
</dependency>

</dependencies>
Expand Down
94 changes: 68 additions & 26 deletions dotCMS/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
<palantirJavaFormat.version>2.29.0</palantirJavaFormat.version>
<src.dir>${project.basedir}/src/main/java</src.dir>
<test.src.dir>${project.basedir}/src/test/java</test.src.dir>
<version.spotless.plugin>2.36.0</version.spotless.plugin>
<batik.version>1.16</batik.version>
<glowroot.version>0.14.1</glowroot.version>
<version.cargo.plugin>1.10.6</version.cargo.plugin>
<assembly-directory>${basedir}/target/dist</assembly-directory>
Expand All @@ -27,6 +25,8 @@
<buildDocker>true</buildDocker>
<tomcat-dist-folder>dotserver/tomcat-${tomcat.version}</tomcat-dist-folder>
<tomcat-lib-folder>${assembly-directory}/${tomcat-dist-folder}/lib</tomcat-lib-folder>
<tomcat-log4j-lib-folder>${assembly-directory}/${tomcat-dist-folder}/log4j2/lib</tomcat-log4j-lib-folder>
<session-manager-lib-folder>${assembly-directory}/${tomcat-dist-folder}/session-manager/lib</session-manager-lib-folder>
<tomcat9-overrides>${project.basedir}/src/main/resources/container/tomcat9</tomcat9-overrides>
<exploded-webapp-dir>${assembly-directory}/${tomcat-dist-folder}/webapps/ROOT</exploded-webapp-dir>
<war.output.file>${project.build.directory}/${project.build.finalName}-war.war</war.output.file>
Expand Down Expand Up @@ -63,10 +63,6 @@
<test.felix.undeployed.folder>felix/core/undeployed</test.felix.undeployed.folder>
<test.felix.system.undeployed.folder>felix/system/undeployed</test.felix.system.undeployed.folder>
<!-- <docker.platforms>linux/arm64,linux/amd64</docker.platforms>-->
<tomcat.redis-session-manager.version>1.3</tomcat.redis-session-manager.version>
<commons-pool2.version>2.11.1</commons-pool2.version>
<org.slfj4.version>1.7.36</org.slfj4.version>
<jedis.version>4.4.6</jedis.version>
<tomcat.run.data.folder>${project.build.directory}/tomcat-run-data/${context.name}</tomcat.run.data.folder>
<cleanup.data.folder>false</cleanup.data.folder>
<!-- Add enterprise to sonar sources -->
Expand All @@ -88,6 +84,11 @@
<type>zip</type>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.dotcms</groupId>
<artifactId>tomcat-redis-session-manager</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>com.dotcms.plugins</groupId>
<artifactId>dotcms-system-bundles</artifactId>
Expand Down Expand Up @@ -1351,21 +1352,30 @@
<!-- Support Java Commons Logging API in Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-jcl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<!-- Support SLF4j2 API in Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- Servlet container initialization of Log4j2 -->
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-web</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<!-- Async support for Log4J 2.x -->
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<scope>runtime</scope>
</dependency>

<!-- Graalvm Js Engine -->
Expand Down Expand Up @@ -1402,6 +1412,12 @@
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-catalina</artifactId>
<version>${tomcat.version}</version>
<scope>provided</scope>
</dependency>

</dependencies>
<dependencyManagement>
Expand Down Expand Up @@ -1535,37 +1551,69 @@
<configuration>
<artifactItems>
<artifactItem>
<groupId>com.dotcms</groupId>
<artifactId>tomcat-redis-session-manager</artifactId>
<version>${tomcat.redis-session-manager.version}</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${tomcat-lib-folder}</outputDirectory>
<overWrite>false</overWrite>
<outputDirectory>${tomcat-log4j-lib-folder}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<version>${commons-pool2.version}</version>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${tomcat-lib-folder}</outputDirectory>
<overWrite>false</overWrite>
<outputDirectory>${tomcat-log4j-lib-folder}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-appserver</artifactId>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${tomcat-log4j-lib-folder}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j2-impl</artifactId>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${tomcat-log4j-lib-folder}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>${org.slfj4.version}</version>
<type>jar</type>
<overWrite>false</overWrite>
<outputDirectory>${tomcat-log4j-lib-folder}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>com.dotcms</groupId>
<artifactId>tomcat-redis-session-manager</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${tomcat-lib-folder}</outputDirectory>
<outputDirectory>${session-manager-lib-folder}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${session-manager-lib-folder}</outputDirectory>
</artifactItem>
<artifactItem>
<groupId>redis.clients</groupId>
<artifactId>jedis</artifactId>
<version>${jedis.version}</version>
<type>jar</type>
<overWrite>true</overWrite>
<outputDirectory>${tomcat-lib-folder}</outputDirectory>
<outputDirectory>${session-manager-lib-folder}</outputDirectory>
</artifactItem>

<dependency>
<!-- Async support for Log4J 2.x -->
<groupId>com.lmax</groupId>
<artifactId>disruptor</artifactId>
<overWrite>true</overWrite>
<outputDirectory>${tomcat-log4j-lib-folder}</outputDirectory>
</dependency>
</artifactItems>
</configuration>
</execution>
Expand Down Expand Up @@ -1913,12 +1961,6 @@
<file>${tomcat9-overrides}/bin/setenv.sh</file>
<todir>bin</todir>
</configfile>
<!--
<configfile>
<file>${tomcat9-overrides}/conf/catalina.properties</file>
<todir>conf</todir>
</configfile>
-->
<configfile>
<file>${tomcat9-overrides}/conf/server.xml</file>
<todir>conf</todir>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,15 @@
import com.dotcms.util.AsciiArt;
import com.dotmarketing.business.CacheLocator;
import com.dotmarketing.common.reindex.ReindexThread;
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 javax.servlet.ServletContext;
import javax.servlet.ServletContextEvent;
import javax.servlet.ServletContextListener;
import javax.websocket.server.ServerContainer;
import java.io.File;

/**
*
Expand Down Expand Up @@ -62,25 +59,6 @@ public void contextInitialized(ServletContextEvent arg0) {

Config.setMyApp(arg0.getServletContext());


String path = null;
try {

String contextPath = Config.CONTEXT.getRealPath("/");
if ( !contextPath.endsWith( File.separator ) ) {
contextPath += File.separator;
}
File file = new File(contextPath + "WEB-INF" + File.separator + "log4j" + File.separator + "log4j2.xml");
path = file.toURI().toString();

} catch (Exception e) {
Logger.error(this,e.getMessage(),e);
}

//Initialises/reconfigures log4j based on a given log4j configuration file
Log4jUtil.initializeFromPath(path);


installWebSocket(arg0.getServletContext());
}

Expand Down
36 changes: 0 additions & 36 deletions dotCMS/src/main/java/com/dotmarketing/loggers/Log4jUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,6 @@ public class Log4jUtil {

private final static String LOG4J_CONTEXT_SELECTOR = "Log4jContextSelector";

/**
* Configure default system properties
*/
public static void configureDefaultSystemProperties () {
if(!UtilMethods.isSet(System.getProperty(LOG4J_CONTEXT_SELECTOR))) {
System.setProperty(LOG4J_CONTEXT_SELECTOR, "org.apache.logging.log4j.core.async.AsyncLoggerContextSelector");
}
}

/**
* Creates a ConsoleAppender in order to add it to the root logger
*/
Expand Down Expand Up @@ -128,33 +119,6 @@ public static void shutdown(LoggerContext context) {
Configurator.shutdown(context);
}

/**
* Initialises/reconfigures log4j based on a given log4j configuration file
*
* @param log4jConfigFilePath
*/
public static void initializeFromPath ( String log4jConfigFilePath ) {

if ( log4jConfigFilePath != null ) {

try {

configureDefaultSystemProperties();
LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false);

if ( !loggerContext.isInitialized() || loggerContext.isStopped() ) {
Configurator.initialize(null, log4jConfigFilePath);
} else {
loggerContext.setConfigLocation(URI.create(log4jConfigFilePath));
loggerContext.reconfigure();
}
} catch ( Exception e ) {
LogManager.getLogger().error("Error initializing log for " + log4jConfigFilePath + " configuration file.", e);
}
LogManager.getLogger().info("Async Logger enabled: "+ AsyncLoggerContextSelector.isSelected());
}
}

/**
* Returns the current dotCMS logger context
*
Expand Down
Loading

0 comments on commit bb53cf5

Please sign in to comment.