This is an automated email from the ASF dual-hosted git repository. klund pushed a commit to branch develop in repository https://gitbox.apache.org/repos/asf/geode.git
commit f8bb38acf0109e9fe1711189167c1363b4c21053 Author: Kirk Lund <[email protected]> AuthorDate: Fri Oct 5 12:57:51 2018 -0700 GEODE-2644: Extract AlertLevel from AlertAppender --- .../internal/logging/log4j/AlertAppender.java | 48 +------- .../geode/internal/logging/log4j/AlertLevel.java | 61 +++++++++ .../internal/logging/log4j/AlertLevelTest.java | 137 +++++++++++++++++++++ 3 files changed, 202 insertions(+), 44 deletions(-) diff --git a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java index 9c68de9..a102f82 100644 --- a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java +++ b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertAppender.java @@ -14,6 +14,9 @@ */ package org.apache.geode.internal.logging.log4j; +import static org.apache.geode.internal.logging.log4j.AlertLevel.alertLevelToLogLevel; +import static org.apache.geode.internal.logging.log4j.AlertLevel.logLevelToAlertLevel; + import java.beans.PropertyChangeEvent; import java.beans.PropertyChangeListener; import java.util.Date; @@ -134,7 +137,7 @@ public class AlertAppender extends AbstractAppender implements PropertyChangeLis } ClusterDistributionManager distMgr = (ClusterDistributionManager) ds.getDistributionManager(); - final int intLevel = logLevelToAlertLevel(event.getLevel().intLevel()); + final int intLevel = logLevelToAlertLevel(event.getLevel()); final Date date = new Date(event.getTimeMillis()); final String threadName = event.getThreadName(); final String logMessage = event.getMessage().getFormattedMessage(); @@ -273,49 +276,6 @@ public class AlertAppender extends AbstractAppender implements PropertyChangeLis this.listeners.add(listener); } - /** - * Converts an int alert level to an int log level. - * - * @param alertLevel The int value for the alert level - * @return The int value for the matching log level - * @throws java.lang.IllegalArgumentException If there is no matching log level - */ - public static int alertLevelToLogLevel(final int alertLevel) { - switch (alertLevel) { - case Alert.SEVERE: - return Level.FATAL.intLevel(); - case Alert.ERROR: - return Level.ERROR.intLevel(); - case Alert.WARNING: - return Level.WARN.intLevel(); - case Alert.OFF: - return Level.OFF.intLevel(); - } - - throw new IllegalArgumentException("Unknown Alert level [" + alertLevel + "]."); - } - - /** - * Converts an int log level to an int alert level. - * - * @param logLevel The int value for the log level - * @return The int value for the matching alert level - * @throws java.lang.IllegalArgumentException If there is no matching log level - */ - public static int logLevelToAlertLevel(final int logLevel) { - if (logLevel == Level.FATAL.intLevel()) { - return Alert.SEVERE; - } else if (logLevel == Level.ERROR.intLevel()) { - return Alert.ERROR; - } else if (logLevel == Level.WARN.intLevel()) { - return Alert.WARNING; - } else if (logLevel == Level.OFF.intLevel()) { - return Alert.OFF; - } - - throw new IllegalArgumentException("Unknown Log level [" + logLevel + "]."); - } - public synchronized void shuttingDown() { this.listeners.clear(); this.appenderContext.getLoggerContext().removePropertyChangeListener(this); diff --git a/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertLevel.java b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertLevel.java new file mode 100644 index 0000000..5ffe5a4 --- /dev/null +++ b/geode-core/src/main/java/org/apache/geode/internal/logging/log4j/AlertLevel.java @@ -0,0 +1,61 @@ +/* + * 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.log4j; + +import org.apache.logging.log4j.Level; + +import org.apache.geode.internal.admin.Alert; + +public class AlertLevel { + + /** + * Converts an {@link Alert} level to a LOG4J2 {@code Level}. + * + * @throws IllegalArgumentException if there is no matching LOG4J2 Level + */ + static int alertLevelToLogLevel(final int alertLevel) { + switch (alertLevel) { + case Alert.SEVERE: + return Level.FATAL.intLevel(); + case Alert.ERROR: + return Level.ERROR.intLevel(); + case Alert.WARNING: + return Level.WARN.intLevel(); + case Alert.OFF: + return Level.OFF.intLevel(); + } + + throw new IllegalArgumentException("Unknown Alert level [" + alertLevel + "]."); + } + + /** + * Converts a LOG4J2 {@code Level} to an {@link Alert} level. + * + * @throws IllegalArgumentException if there is no matching Alert level + */ + static int logLevelToAlertLevel(final Level logLevel) { + if (logLevel == Level.FATAL) { + return Alert.SEVERE; + } else if (logLevel == Level.ERROR) { + return Alert.ERROR; + } else if (logLevel == Level.WARN) { + return Alert.WARNING; + } else if (logLevel == Level.OFF) { + return Alert.OFF; + } + + throw new IllegalArgumentException("Unknown LOG4J2 Level [" + logLevel + "]."); + } +} diff --git a/geode-core/src/test/java/org/apache/geode/internal/logging/log4j/AlertLevelTest.java b/geode-core/src/test/java/org/apache/geode/internal/logging/log4j/AlertLevelTest.java new file mode 100644 index 0000000..06b7b9c --- /dev/null +++ b/geode-core/src/test/java/org/apache/geode/internal/logging/log4j/AlertLevelTest.java @@ -0,0 +1,137 @@ +/* + * 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.log4j; + +import static org.apache.geode.internal.logging.log4j.AlertLevel.alertLevelToLogLevel; +import static org.apache.geode.internal.logging.log4j.AlertLevel.logLevelToAlertLevel; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +import org.apache.logging.log4j.Level; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.geode.internal.admin.Alert; +import org.apache.geode.test.junit.categories.LoggingTest; + +/** + * Unit tests for {@link AlertLevel}. + */ +@Category(LoggingTest.class) +public class AlertLevelTest { + + @Test + public void alertLevelToLogLevel_AlertSevere_returnsLevelFatal() { + assertThat(alertLevelToLogLevel(Alert.SEVERE)).isEqualTo(Level.FATAL.intLevel()); + } + + @Test + public void alertLevelToLogLevel_AlertError_returnsLevelFatal() { + assertThat(alertLevelToLogLevel(Alert.ERROR)).isEqualTo(Level.ERROR.intLevel()); + } + + @Test + public void alertLevelToLogLevel_AlertWarning_returnsLevelFatal() { + assertThat(alertLevelToLogLevel(Alert.WARNING)).isEqualTo(Level.WARN.intLevel()); + } + + @Test + public void alertLevelToLogLevel_AlertOff_returnsLevelFatal() { + assertThat(alertLevelToLogLevel(Alert.OFF)).isEqualTo(Level.OFF.intLevel()); + } + + @Test + public void alertLevelToLogLevel_AlertAll_throwsIllegalArgumentException() { + assertThatThrownBy(() -> alertLevelToLogLevel(Alert.ALL)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown Alert level [" + Alert.ALL + "]."); + } + + @Test + public void alertLevelToLogLevel_AlertFinest_throwsIllegalArgumentException() { + assertThatThrownBy(() -> alertLevelToLogLevel(Alert.FINEST)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown Alert level [" + Alert.FINEST + "]."); + } + + @Test + public void alertLevelToLogLevel_AlertFine_throwsIllegalArgumentException() { + assertThatThrownBy(() -> alertLevelToLogLevel(Alert.FINE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown Alert level [" + Alert.FINE + "]."); + } + + @Test + public void alertLevelToLogLevel_AlertConfig_throwsIllegalArgumentException() { + assertThatThrownBy(() -> alertLevelToLogLevel(Alert.CONFIG)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown Alert level [" + Alert.CONFIG + "]."); + } + + @Test + public void alertLevelToLogLevel_AlertInfo_throwsIllegalArgumentException() { + assertThatThrownBy(() -> alertLevelToLogLevel(Alert.INFO)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown Alert level [" + Alert.INFO + "]."); + } + + @Test + public void logLevelToAlertLevel_LevelFatal_returnsAlertSevere() { + assertThat(logLevelToAlertLevel(Level.FATAL)).isEqualTo(Alert.SEVERE); + } + + @Test + public void logLevelToAlertLevel_LevelError_returnsAlertError() { + assertThat(logLevelToAlertLevel(Level.ERROR)).isEqualTo(Alert.ERROR); + } + + @Test + public void logLevelToAlertLevel_LevelWarn_returnsAlertWarning() { + assertThat(logLevelToAlertLevel(Level.WARN)).isEqualTo(Alert.WARNING); + } + + @Test + public void logLevelToAlertLevel_LevelOff_returnsAlertOff() { + assertThat(logLevelToAlertLevel(Level.OFF)).isEqualTo(Alert.OFF); + } + + @Test + public void logLevelToAlertLevel_LevelAll_throwsIllegalArgumentException() { + assertThatThrownBy(() -> logLevelToAlertLevel(Level.ALL)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown LOG4J2 Level [" + Level.ALL + "]."); + } + + @Test + public void logLevelToAlertLevel_LevelTrace_throwsIllegalArgumentException() { + assertThatThrownBy(() -> logLevelToAlertLevel(Level.TRACE)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown LOG4J2 Level [" + Level.TRACE + "]."); + } + + @Test + public void logLevelToAlertLevel_LevelDebug_throwsIllegalArgumentException() { + assertThatThrownBy(() -> logLevelToAlertLevel(Level.DEBUG)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown LOG4J2 Level [" + Level.DEBUG + "]."); + } + + @Test + public void logLevelToAlertLevel_LevelInfo_throwsIllegalArgumentException() { + assertThatThrownBy(() -> logLevelToAlertLevel(Level.INFO)) + .isInstanceOf(IllegalArgumentException.class) + .hasMessage("Unknown LOG4J2 Level [" + Level.INFO + "]."); + } +}
