Skip to content

Commit

Permalink
GEODE-232: Convert resource string to URI string for log4j2-cli.xml
Browse files Browse the repository at this point in the history
Add lots of new unit and integration tests for LogService.
  • Loading branch information
kirklund committed Aug 21, 2015
1 parent 936065f commit 19732b2
Show file tree
Hide file tree
Showing 8 changed files with 385 additions and 131 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,15 @@ private static final boolean setLog4jConfigFileProperty() {
else {
//If the resource can be found and in cases where the resource is in gemfire jar,
//we set the log location to the file that was found
configFileInformation = "Using log4j configuration file specified by " + ConfigurationFactory.CONFIGURATION_FILE_PROPERTY + ": '" + configFileName + "'";

// must change from Java resource syntax to Java URL syntax (GEODE-232)
// jar:file:/export/latvia1/users/klund/dev/asf-geode/gemfire-assembly/build/install/apache-geode/lib/gemfire-core-1.0.0-incubating-SNAPSHOT.jar!/com/gemstone/gemfire/internal/logging/log4j/log4j2-cli.xml

String configFilePropertyValue = configUrl.toString();
// configFileName is Java resource syntax, configFilePropertyValue is URL syntax as required by log4j2

System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, configFilePropertyValue);
configFileInformation = "Using log4j configuration file specified by " + ConfigurationFactory.CONFIGURATION_FILE_PROPERTY + ": '" + configFilePropertyValue + "'";
StatusLogger.getLogger().info(configFileInformation);
return true;
}
Expand Down Expand Up @@ -218,6 +226,7 @@ public static Logger getLogger(final String name) {
public static LogWriterLogger createLogWriterLogger(final String name, final String connectionName, final boolean isSecure) {
return LogWriterLogger.create(name, connectionName, isSecure);
}

/**
* Return the Log4j Level associated with the int level.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ public class Configurator {
//context.reconfigure();
}*/

public static void shutdown() {
//LoggerContext context = (LoggerContext)LogManager.getContext(false);
final LoggerContext context = ((org.apache.logging.log4j.core.Logger)LogManager.getRootLogger()).getContext();
context.stop();
org.apache.logging.log4j.core.config.Configurator.shutdown(context);
}


public static void setLevel(String name, Level level) {
LoggerContext context = (LoggerContext)LogManager.getContext(false);
LoggerConfig logConfig = getLoggerConfig(name);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,209 @@
package com.gemstone.gemfire.internal.logging;

import static com.gemstone.gemfire.internal.logging.LogServiceIntegrationTestSupport.*;
import static org.assertj.core.api.Assertions.*;

import java.io.File;
import java.net.URL;

import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.core.Appender;
import org.apache.logging.log4j.core.Logger;
import org.apache.logging.log4j.core.config.ConfigurationFactory;
import org.apache.logging.log4j.status.StatusLogger;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.ExternalResource;
import org.junit.rules.TemporaryFolder;

import com.gemstone.gemfire.internal.ClassPathLoader;
import com.gemstone.gemfire.internal.logging.log4j.Configurator;
import com.gemstone.gemfire.test.junit.categories.IntegrationTest;

/**
* Integration tests for LogService and how it configures and uses log4j2
*
* @author Kirk Lund
*/
@Category(IntegrationTest.class)
public class LogServiceIntegrationJUnitTest {

private String beforeConfigFileProp;
private Level beforeLevel;

@Rule
public final TemporaryFolder temporaryFolder = new TemporaryFolder();

@Rule
public final ExternalResource externalResource = new ExternalResource() {
@Override
protected void before() {
beforeConfigFileProp = System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
beforeLevel = StatusLogger.getLogger().getLevel();

System.clearProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY);
StatusLogger.getLogger().setLevel(Level.OFF);

Configurator.shutdown();
}
@Override
protected void after() {
Configurator.shutdown();

if (beforeConfigFileProp != null) {
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, beforeConfigFileProp);
}
StatusLogger.getLogger().setLevel(beforeLevel);

LogService.reconfigure();
assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isTrue();
}
};

private URL defaultConfigUrl;
private URL cliConfigUrl;

@Before
public void setUp() {
this.defaultConfigUrl = LogService.class.getResource(LogService.DEFAULT_CONFIG);
this.cliConfigUrl = LogService.class.getResource(LogService.CLI_CONFIG);
}

@Test
public void shouldPreferConfigInConfigurationFilePropertyIfSet() throws Exception {
final File configFile = this.temporaryFolder.newFile("log4j2.xml");
final String configFileName = configFile.toURI().toString();
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, configFileName);
writeConfigFile(configFile, Level.DEBUG);

LogService.reconfigure();

assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isFalse();
assertThat(System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).isEqualTo(configFileName);
assertThat(LogService.getLogger().getName()).isEqualTo(getClass().getName());
}

@Test
public void shouldUseDefaultConfigIfNotConfigured() throws Exception {
LogService.reconfigure();

assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isTrue();
assertThat(System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).isEqualTo(this.defaultConfigUrl.toString());
}

@Test
public void defaultConfigShouldBeLoadableAsResource() {
final URL configUrlFromLogService = LogService.class.getResource(LogService.DEFAULT_CONFIG);
final URL configUrlFromClassLoader = getClass().getClassLoader().getResource(LogService.DEFAULT_CONFIG.substring(1));
final URL configUrlFromClassPathLoader = ClassPathLoader.getLatest().getResource(LogService.DEFAULT_CONFIG.substring(1));

assertThat(configUrlFromLogService).isNotNull();
assertThat(configUrlFromClassLoader).isNotNull();
assertThat(configUrlFromClassPathLoader).isNotNull();
assertThat(configUrlFromLogService)
.isEqualTo(configUrlFromClassLoader)
.isEqualTo(configUrlFromClassPathLoader);
}

@Test
public void defaultConfigShouldIncludeStdout() {
LogService.reconfigure();
final Logger rootLogger = (Logger) LogService.getRootLogger();

assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isTrue();
assertThat(rootLogger.getAppenders().get(LogService.STDOUT)).isNotNull();
}

@Test
public void removeConsoleAppenderShouldRemoveStdout() {
LogService.reconfigure();
final Logger rootLogger = (Logger) LogService.getRootLogger();

LogService.removeConsoleAppender();

assertThat(rootLogger.getAppenders().get(LogService.STDOUT)).isNull();
}

@Test
public void restoreConsoleAppenderShouldRestoreStdout() {
LogService.reconfigure();
final Logger rootLogger = (Logger) LogService.getRootLogger();

LogService.removeConsoleAppender();

assertThat(rootLogger.getAppenders().get(LogService.STDOUT)).isNull();

LogService.restoreConsoleAppender();

assertThat(rootLogger.getAppenders().get(LogService.STDOUT)).isNotNull();
}

@Test
public void removeAndRestoreConsoleAppenderShouldAffectRootLogger() {
LogService.reconfigure();

assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isTrue();

final Logger rootLogger = (Logger) LogService.getRootLogger();

// assert "Console" is present for ROOT
Appender appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNotNull();

LogService.removeConsoleAppender();

// assert "Console" is not present for ROOT
appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNull();

LogService.restoreConsoleAppender();

// assert "Console" is present for ROOT
appender = rootLogger.getAppenders().get(LogService.STDOUT);
assertThat(appender).isNotNull();
}

@Test
public void intializeAfterUsingLoggerShouldReconfigure() {
assertThat(System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).as("log4j.configurationFile="+System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).isNullOrEmpty();

Configurator.shutdown();

LogManager.getRootLogger();

assertThat(System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).as("log4j.configurationFile="+System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).isNullOrEmpty();

LogService.reconfigure();
LogService.initialize();

assertThat(System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).as("log4j.configurationFile="+System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).contains(LogService.DEFAULT_CONFIG);
assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isTrue();
}

@Test
public void cliConfigLoadsAsResource() {
assertThat(this.cliConfigUrl).isNotNull();
assertThat(this.cliConfigUrl.toString()).contains(LogService.CLI_CONFIG);
}

@Test
public void defaultConfigLoadsAsResource() {
assertThat(this.defaultConfigUrl).isNotNull();
assertThat(this.defaultConfigUrl.toString()).contains(LogService.DEFAULT_CONFIG);
}

@Test
public void shouldConvertConfigurationFilePropertyValueToURL() throws Exception {
System.setProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY, LogService.CLI_CONFIG);

LogService.reconfigure();

assertThat(LogService.isUsingGemFireDefaultConfig()).as(LogService.getConfigInformation()).isFalse();
assertThat(this.cliConfigUrl.toString()).contains(LogService.CLI_CONFIG);
assertThat(System.getProperty(ConfigurationFactory.CONFIGURATION_FILE_PROPERTY)).isEqualTo(this.cliConfigUrl.toString());
assertThat(LogService.getLogger().getName()).isEqualTo(getClass().getName());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.gemstone.gemfire.internal.logging;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import org.apache.logging.log4j.Level;

public class LogServiceIntegrationTestSupport {

public static void writeConfigFile(final File configFile, final Level level) throws IOException {
final BufferedWriter writer = new BufferedWriter(new FileWriter(configFile));
writer.write(
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<Configuration>" +
"<Loggers>" +
"<Root level=\"" + level.name() + "\"/>" +
"</Loggers>" +
"</Configuration>"
);
writer.close();
}
}
Loading

0 comments on commit 19732b2

Please sign in to comment.