Skip to content

Commit

Permalink
Merge pull request #369 from groldan/bug/gt_log4j2_redirect
Browse files Browse the repository at this point in the history
Change GeoTools log redirection from Log4j2 to Logback
  • Loading branch information
groldan authored Nov 11, 2023
2 parents 5eeacb8 + 21ea7da commit 22a2ba0
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.geoserver.GeoserverInitStartupListener;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

/**
* {@link ApplicationContextInitializer} replacing upstream's {@link GeoserverInitStartupListener},
Expand All @@ -22,6 +23,10 @@ public class GeoToolsStaticContextInitializer

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
// run once for the webapp context, ignore the actuator context
if (!(applicationContext instanceof GenericWebApplicationContext)) {
return;
}
System.setProperty("org.geotools.referencing.forceXY", "true");

Boolean useEnvAwareHttpClient =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
package org.geoserver.cloud.autoconfigure.geotools;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertNull;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;

/**
* @since 1.0
*/
class GeoToolsHttpClientAutoConfigurationTest {

private ApplicationContextRunner runner =
new ApplicationContextRunner() //
private WebApplicationContextRunner runner =
new WebApplicationContextRunner() //
.withInitializer(new GeoToolsStaticContextInitializer()) //
.withConfiguration(
AutoConfigurations.of(GeoToolsHttpClientAutoConfiguration.class));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
public class SimpleJNDIStaticContextInitializer
implements ApplicationContextInitializer<ConfigurableApplicationContext> {

private static boolean initialized;

/**
* Register the context builder by registering it with the JNDI NamingManager. Note that once
* this has been done, {@code new InitialContext()} will always return a context from this
Expand All @@ -36,6 +38,8 @@ public class SimpleJNDIStaticContextInitializer
*/
@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
if (initialized) return;
initialized = true;
if (NamingManager.hasInitialContextFactoryBuilder()) {
log.info("JNDI InitialContextFactoryBuilder already set");
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.geoserver.GeoserverInitStartupListener;
import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.web.context.support.GenericWebApplicationContext;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
Expand All @@ -29,15 +30,22 @@ public class GeoServerContextInitializer

@Override
public void initialize(ConfigurableApplicationContext applicationContext) {
// run once for the webapp context, ignore the actuator context
if (!(applicationContext instanceof GenericWebApplicationContext)) {
return;
}

// tell geoserver not to control logging, spring-boot will do
System.setProperty("RELINQUISH_LOG4J_CONTROL", "true");
// and tell geotools not to redirect to Log4J nor any other framework, we'll use
// spring-boot's logging redirection. Use Log4J2 redirection policy, JavaLogging
// spring-boot's logging redirection. Use Logback redirection policy, JavaLogging
// will make GeoserverInitStartupListener's call to GeoTools.init() heuristically set
// Logging.ALL.setLoggerFactory("org.geotools.util.logging.LogbackLoggerFactory")
// with the caveat that it wrongly maps logging levels and hence if, for example, a logger
// with level FINE is called with INFO, it doesn't log at all
System.setProperty("GT2_LOGGING_REDIRECTION", "Log4J2");
// with level FINE is called with INFO, it doesn't log at all.
// Log4J2 redirection policy will fail when calling java.util.logging.Logger.setLevel(..)
// because our version of Log4j2 is newer than GeoTools' and the API changed
System.setProperty("GT2_LOGGING_REDIRECTION", "Logback");
ServletContext source = mockServletContext();
ServletContextEvent sce = new ServletContextEvent(source);
GeoserverInitStartupListener startupInitializer = new GeoserverInitStartupListener();
Expand Down

0 comments on commit 22a2ba0

Please sign in to comment.