Skip to content

Commit

Permalink
GEODE-6122: Make log4j core optional (apache#2944)
Browse files Browse the repository at this point in the history
* Add Logging ProviderAgent support for ServiceLoader
* Test for availability of Log4j Core before defaulting to Log4jAgent
* Change optional ProviderAgent methods to have default impls
* Extract LogLevelUpdateOccurs enum to top level class
* Extract LogLevelUpdateScope enum to top level class
* Move Banner to internal.logging package
* Break Banner's hard dependency on Log4J Core
* Improve javadocs
* Add field type to TestingOnly annotation
* Replace use of InternalLogWriter constants with LogWriterLevel enum
  • Loading branch information
kirklund authored Dec 6, 2018
1 parent 2c3fd5b commit a53c28a
Show file tree
Hide file tree
Showing 37 changed files with 683 additions and 330 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import org.apache.geode.internal.Banner;
import org.apache.geode.internal.logging.Banner;
import org.apache.geode.test.assertj.LogFileAssert;
import org.apache.geode.test.junit.categories.GfshTest;
import org.apache.geode.test.junit.categories.LoggingTest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import java.lang.annotation.Target;

@Documented
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD})
@Target({ElementType.TYPE, ElementType.CONSTRUCTOR, ElementType.METHOD, ElementType.FIELD})
public @interface TestingOnly {

/** Optional description */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import static org.apache.geode.distributed.ConfigurationProperties.MAX_WAIT_TIME_RECONNECT;
import static org.apache.geode.distributed.ConfigurationProperties.MEMBER_TIMEOUT;
import static org.apache.geode.distributed.internal.membership.gms.MembershipManagerHelper.getMembershipManager;
import static org.apache.geode.internal.Banner.BannerHeader.displayValues;
import static org.apache.geode.internal.logging.Banner.BannerHeader.displayValues;
import static org.apache.geode.internal.logging.Configuration.STARTUP_CONFIGURATION;
import static org.apache.geode.test.awaitility.GeodeAwaitility.await;
import static org.apache.geode.test.awaitility.GeodeAwaitility.getTimeout;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import static org.apache.commons.io.FileUtils.readLines;
import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
import static org.apache.geode.distributed.ConfigurationProperties.LOG_FILE;
import static org.apache.geode.internal.Banner.BannerHeader.displayValues;
import static org.apache.geode.internal.logging.Banner.BannerHeader.displayValues;
import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
Expand All @@ -36,7 +36,6 @@

import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.internal.Banner;
import org.apache.geode.test.assertj.LogFileAssert;
import org.apache.geode.test.junit.categories.LoggingTest;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
import org.junit.Test;
import org.junit.experimental.categories.Category;

import org.apache.geode.internal.logging.log4j.Log4jAgent;
import org.apache.geode.test.junit.categories.LoggingTest;

/**
Expand All @@ -33,10 +32,4 @@ public class ConfigurationInfoIntegrationTest {
public void getConfigurationInfoContainsLog4j2Xml() {
assertThat(getConfigurationInfo()).contains("log4j2.xml");
}

@Test
public void getConfigurationInfoMatchesLog4jAgent() {

assertThat(getConfigurationInfo()).contains(Log4jAgent.getConfigurationInfo());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
*/
package org.apache.geode.internal.logging;

import static org.apache.geode.internal.logging.Configuration.LogLevelUpdateOccurs.ALWAYS;
import static org.apache.geode.internal.logging.Configuration.LogLevelUpdateScope.ALL_LOGGERS;
import static org.apache.geode.internal.logging.Configuration.LogLevelUpdateScope.GEODE_AND_APPLICATION_LOGGERS;
import static org.apache.geode.internal.logging.Configuration.LogLevelUpdateScope.GEODE_AND_SECURITY_LOGGERS;
import static org.apache.geode.internal.logging.Configuration.LogLevelUpdateScope.GEODE_LOGGERS;
import static org.apache.geode.internal.logging.Configuration.MAIN_LOGGER_NAME;
import static org.apache.geode.internal.logging.Configuration.SECURITY_LOGGER_NAME;
import static org.apache.geode.internal.logging.LogLevelUpdateOccurs.ALWAYS;
import static org.apache.geode.internal.logging.LogLevelUpdateScope.ALL_LOGGERS;
import static org.apache.geode.internal.logging.LogLevelUpdateScope.GEODE_AND_APPLICATION_LOGGERS;
import static org.apache.geode.internal.logging.LogLevelUpdateScope.GEODE_AND_SECURITY_LOGGERS;
import static org.apache.geode.internal.logging.LogLevelUpdateScope.GEODE_LOGGERS;
import static org.apache.geode.internal.logging.LogWriterLevel.INFO;
import static org.apache.geode.internal.logging.LogWriterLevel.WARNING;
import static org.apache.geode.internal.logging.log4j.Log4jAgent.getLoggerConfig;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.
*/
package org.apache.geode.internal.logging;

import static org.assertj.core.api.Assertions.assertThat;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collection;

import org.apache.commons.io.FileUtils;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import org.junit.rules.TemporaryFolder;
import org.junit.rules.TestName;

import org.apache.geode.test.junit.categories.LoggingTest;

/**
* Integration tests to confirm that writing to file system works properly in stressTest
*/
@Category(LoggingTest.class)
public class FileSystemCanaryIntegrationTest {

private File file;
private Collection<String> lines;

@Rule
public TemporaryFolder temporaryFolder = new TemporaryFolder();

@Rule
public TestName testName = new TestName();

@Before
public void setUp() {
String name = testName.getMethodName();
file = new File(temporaryFolder.getRoot(), name + "-main.log");

lines = new ArrayList<>();
for (int i = 1; i <= 10; i++) {
lines.add("Line-" + i);
}
}

@Test
public void readLinesReturnsSameLinesThatWereJustWritten() throws IOException {
FileUtils.writeLines(file, lines);

assertThat(FileUtils.readLines(file, Charset.defaultCharset())).isEqualTo(lines);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.apache.geode.internal.logging;

import static org.apache.geode.internal.Banner.BannerHeader.displayValues;
import static org.apache.geode.internal.logging.Banner.BannerHeader.displayValues;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.After;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/
package org.apache.geode.internal.logging;

import static org.apache.geode.internal.Banner.BannerHeader.displayValues;
import static org.apache.geode.internal.logging.Banner.BannerHeader.displayValues;
import static org.assertj.core.api.Assertions.assertThat;

import org.junit.After;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.internal.DistributionConfig;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.internal.Banner;
import org.apache.geode.test.assertj.LogFileAssert;
import org.apache.geode.test.junit.categories.LoggingTest;

Expand Down Expand Up @@ -77,7 +76,7 @@ public void setUp() {

system = (InternalDistributedSystem) DistributedSystem.connect(config);

banner = Banner.getString(null);
banner = new Banner().getString();

DistributionConfig distributionConfig = system.getConfig();
startupConfiguration = StringUtils
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@
*/
package org.apache.geode.internal.logging.log4j;

import static org.apache.geode.internal.logging.Configuration.DEFAULT_LOGWRITER_LEVEL;
import static org.apache.geode.internal.logging.Configuration.create;
import static org.apache.geode.internal.logging.InternalLogWriter.FINE_LEVEL;
import static org.apache.geode.internal.logging.InternalLogWriter.WARNING_LEVEL;
import static org.apache.geode.internal.logging.LogWriterLevel.CONFIG;
import static org.apache.geode.internal.logging.LogWriterLevel.FINE;
import static org.apache.geode.internal.logging.LogWriterLevel.WARNING;
import static org.apache.geode.test.util.ResourceUtils.createFileFromResource;
import static org.apache.geode.test.util.ResourceUtils.getResource;
import static org.assertj.core.api.Assertions.assertThat;
Expand All @@ -41,10 +41,10 @@
import org.junit.rules.TestName;

import org.apache.geode.internal.logging.Configuration;
import org.apache.geode.internal.logging.Configuration.LogLevelUpdateOccurs;
import org.apache.geode.internal.logging.Configuration.LogLevelUpdateScope;
import org.apache.geode.internal.logging.LogConfig;
import org.apache.geode.internal.logging.LogConfigSupplier;
import org.apache.geode.internal.logging.LogLevelUpdateOccurs;
import org.apache.geode.internal.logging.LogLevelUpdateScope;
import org.apache.geode.internal.logging.LogService;
import org.apache.geode.test.junit.categories.LoggingTest;

Expand Down Expand Up @@ -88,8 +88,8 @@ public static void setUpLogConfigFile() throws Exception {
@Before
public void setUp() throws Exception {
config = mock(LogConfig.class);
when(config.getLogLevel()).thenReturn(DEFAULT_LOGWRITER_LEVEL);
when(config.getSecurityLogLevel()).thenReturn(DEFAULT_LOGWRITER_LEVEL);
when(config.getLogLevel()).thenReturn(CONFIG.intLevel());
when(config.getSecurityLogLevel()).thenReturn(CONFIG.intLevel());

LogConfigSupplier logConfigSupplier = mock(LogConfigSupplier.class);
when(logConfigSupplier.getLogConfig()).thenReturn(config);
Expand Down Expand Up @@ -124,7 +124,7 @@ public void geodeLoggerDebugNotLoggedByDefault() {
@Test
public void geodeLoggerDebugLoggedAfterLoweringLogLevelToFine() {
// arrange
when(config.getLogLevel()).thenReturn(FINE_LEVEL);
when(config.getLogLevel()).thenReturn(FINE.intLevel());
configuration.configChanged();

// act
Expand All @@ -141,12 +141,12 @@ public void geodeLoggerDebugLoggedAfterLoweringLogLevelToFine() {
@Test
public void geodeLoggerDebugNotLoggedAfterRestoringLogLevelToDefault() {
// arrange
when(config.getLogLevel()).thenReturn(FINE_LEVEL);
when(config.getLogLevel()).thenReturn(FINE.intLevel());
configuration.configChanged();

// re-arrange
geodeConsoleAppender.clearLogEvents();
when(config.getLogLevel()).thenReturn(DEFAULT_LOGWRITER_LEVEL);
when(config.getLogLevel()).thenReturn(CONFIG.intLevel());
configuration.configChanged();

// act
Expand All @@ -159,7 +159,7 @@ public void geodeLoggerDebugNotLoggedAfterRestoringLogLevelToDefault() {
@Test
public void applicationLoggerBelowLevelUnaffectedByLoweringLogLevelChanges() {
// arrange
when(config.getLogLevel()).thenReturn(FINE_LEVEL);
when(config.getLogLevel()).thenReturn(FINE.intLevel());
configuration.configChanged();

// act
Expand All @@ -186,7 +186,7 @@ public void applicationLoggerInfoLoggedByDefault() {
public void applicationLoggerAboveLevelUnaffectedByLoweringLogLevelChanges() {
// arrange
geodeConsoleAppender.clearLogEvents();
when(config.getLogLevel()).thenReturn(FINE_LEVEL);
when(config.getLogLevel()).thenReturn(FINE.intLevel());
configuration.configChanged();

// act
Expand All @@ -200,7 +200,7 @@ public void applicationLoggerAboveLevelUnaffectedByLoweringLogLevelChanges() {
public void applicationLoggerAboveLevelUnaffectedByRaisingLogLevelChanges() {
// arrange
geodeConsoleAppender.clearLogEvents();
when(config.getLogLevel()).thenReturn(WARNING_LEVEL);
when(config.getLogLevel()).thenReturn(WARNING.intLevel());
configuration.configChanged();

// act
Expand All @@ -214,7 +214,7 @@ public void applicationLoggerAboveLevelUnaffectedByRaisingLogLevelChanges() {
public void infoStatementNotLoggedAfterRaisingLogLevelToWarning() {
// arrange
geodeConsoleAppender.clearLogEvents();
when(config.getLogLevel()).thenReturn(WARNING_LEVEL);
when(config.getLogLevel()).thenReturn(WARNING.intLevel());
configuration.configChanged();

// act
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@
package org.apache.geode.internal.logging.log4j;

import static org.apache.geode.distributed.ConfigurationProperties.LOCATORS;
import static org.apache.geode.internal.logging.Configuration.DEFAULT_LOGWRITER_LEVEL;
import static org.apache.geode.internal.logging.Configuration.LOG_LEVEL_UPDATE_OCCURS_PROPERTY;
import static org.apache.geode.internal.logging.InternalLogWriter.FINE_LEVEL;
import static org.apache.geode.internal.logging.InternalLogWriter.WARNING_LEVEL;
import static org.apache.geode.internal.logging.LogWriterLevel.CONFIG;
import static org.apache.geode.internal.logging.LogWriterLevel.FINE;
import static org.apache.geode.internal.logging.LogWriterLevel.WARNING;
import static org.apache.geode.test.util.ResourceUtils.createFileFromResource;
import static org.apache.geode.test.util.ResourceUtils.getResource;
import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -46,7 +46,7 @@
import org.apache.geode.distributed.DistributedSystem;
import org.apache.geode.distributed.internal.DistributionConfig;
import org.apache.geode.distributed.internal.InternalDistributedSystem;
import org.apache.geode.internal.logging.Configuration.LogLevelUpdateOccurs;
import org.apache.geode.internal.logging.LogLevelUpdateOccurs;
import org.apache.geode.internal.logging.LogService;
import org.apache.geode.test.junit.categories.LoggingTest;

Expand Down Expand Up @@ -122,7 +122,7 @@ public void debugNotLoggedByDefault() {

@Test
public void debugLoggedAfterLoweringLogLevelToFine() {
distributionConfig.setLogLevel(FINE_LEVEL);
distributionConfig.setLogLevel(FINE.intLevel());

geodeLogger.debug(logMessage);

Expand All @@ -131,9 +131,9 @@ public void debugLoggedAfterLoweringLogLevelToFine() {

@Test
public void debugNotLoggedAfterRestoringLogLevelToDefault() {
distributionConfig.setLogLevel(FINE_LEVEL);
distributionConfig.setLogLevel(FINE.intLevel());

system.getConfig().setLogLevel(DEFAULT_LOGWRITER_LEVEL);
system.getConfig().setLogLevel(CONFIG.intLevel());
geodeLogger.debug(logMessage);

assertThatLogEventsDoesNotContain(logMessage, geodeLogger.getName(), Level.DEBUG);
Expand All @@ -148,7 +148,7 @@ public void applicationLoggerInfoLoggedByDefault() {

@Test
public void applicationLoggerBelowLevelUnaffectedByLoweringLogLevelChanges() {
distributionConfig.setLogLevel(FINE_LEVEL);
distributionConfig.setLogLevel(FINE.intLevel());

applicationLogger.debug(logMessage);

Expand All @@ -157,7 +157,7 @@ public void applicationLoggerBelowLevelUnaffectedByLoweringLogLevelChanges() {

@Test
public void applicationLoggerAboveLevelUnaffectedByLoweringLogLevelChanges() {
distributionConfig.setLogLevel(FINE_LEVEL);
distributionConfig.setLogLevel(FINE.intLevel());

applicationLogger.info(logMessage);

Expand All @@ -166,7 +166,7 @@ public void applicationLoggerAboveLevelUnaffectedByLoweringLogLevelChanges() {

@Test
public void applicationLoggerAboveLevelUnaffectedByRaisingLogLevelChanges() {
distributionConfig.setLogLevel(WARNING_LEVEL);
distributionConfig.setLogLevel(WARNING.intLevel());

applicationLogger.info(logMessage);

Expand All @@ -175,7 +175,7 @@ public void applicationLoggerAboveLevelUnaffectedByRaisingLogLevelChanges() {

@Test
public void infoStatementNotLoggedAfterRaisingLogLevelToWarning() {
distributionConfig.setLogLevel(WARNING_LEVEL);
distributionConfig.setLogLevel(WARNING.intLevel());

geodeLogger.info(logMessage);

Expand Down
Loading

0 comments on commit a53c28a

Please sign in to comment.