This is an automated email from the ASF dual-hosted git repository.

pkarwasz pushed a commit to branch 2.x
in repository https://gitbox.apache.org/repos/asf/logging-log4j2.git

commit e0fd5cc245e3fd8325fcbd73cca83022aec84add
Author: Charles Leclerc <[email protected]>
AuthorDate: Wed Jan 5 15:58:53 2022 +0100

    Added arbiter from environment variables
---
 log4j-core-test/pom.xml                            |  7 ++
 .../config/arbiters/EnvironmentArbiterTest.java    | 66 +++++++++++++++
 .../config/arbiters/SystemPropertyArbiterTest.java | 73 +++++++++++++++++
 .../test/resources/log4j2-environmentArbiters.xml  | 40 ++++++++++
 .../resources/log4j2-systemPropertyArbiters.xml    | 48 +++++++++++
 .../core/config/arbiters/EnvironmentArbiter.java   | 93 ++++++++++++++++++++++
 6 files changed, 327 insertions(+)

diff --git a/log4j-core-test/pom.xml b/log4j-core-test/pom.xml
index 3cb01f9049..2e5b3a4e35 100644
--- a/log4j-core-test/pom.xml
+++ b/log4j-core-test/pom.xml
@@ -329,6 +329,13 @@
       <artifactId>xz</artifactId>
       <scope>test</scope>
     </dependency>
+    <!-- Used for testing environment variables arbiter -->
+    <dependency>
+      <groupId>com.github.stefanbirkner</groupId>
+      <artifactId>system-lambda</artifactId>
+      <version>1.2.1</version>
+      <scope>test</scope>
+    </dependency>
   </dependencies>
   <build>
     <plugins>
diff --git 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/arbiters/EnvironmentArbiterTest.java
 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/arbiters/EnvironmentArbiterTest.java
new file mode 100644
index 0000000000..d25caa6e6c
--- /dev/null
+++ 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/arbiters/EnvironmentArbiterTest.java
@@ -0,0 +1,66 @@
+/*
+ * 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.logging.log4j.core.config.arbiters;
+
+import com.github.stefanbirkner.systemlambda.SystemLambda;
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.ConsoleAppender;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.test.appender.ListAppender;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * Tests system property condition processing.
+ */
+public class EnvironmentArbiterTest {
+
+    static final String CONFIG = "log4j2-environmentArbiters.xml";
+    static LoggerContext loggerContext = null;
+
+    @AfterEach
+    public void after() {
+        loggerContext.stop();
+        loggerContext = null;
+    }
+
+    @Test
+    public void prodTest() throws Exception {
+        Appender app = SystemLambda.withEnvironmentVariable("ENV", 
"prod").execute(() -> {
+            loggerContext = Configurator.initialize(null, CONFIG);
+            assertNotNull(loggerContext);
+            return loggerContext.getConfiguration().getAppender("Out");
+        });
+        assertNotNull(app);
+        assertTrue(app instanceof ListAppender);
+    }
+
+    @Test
+    public void devTest() throws Exception {
+        Appender app = SystemLambda.withEnvironmentVariable("ENV", 
"dev").execute(() -> {
+            loggerContext = Configurator.initialize(null, CONFIG);
+            assertNotNull(loggerContext);
+            return loggerContext.getConfiguration().getAppender("Out");
+        });
+        assertNotNull(app);
+        assertTrue(app instanceof ConsoleAppender);
+    }
+}
diff --git 
a/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/arbiters/SystemPropertyArbiterTest.java
 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/arbiters/SystemPropertyArbiterTest.java
new file mode 100644
index 0000000000..0a7cc6ee58
--- /dev/null
+++ 
b/log4j-core-test/src/test/java/org/apache/logging/log4j/core/config/arbiters/SystemPropertyArbiterTest.java
@@ -0,0 +1,73 @@
+/*
+ * 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.logging.log4j.core.config.arbiters;
+
+import org.apache.logging.log4j.core.Appender;
+import org.apache.logging.log4j.core.LoggerContext;
+import org.apache.logging.log4j.core.appender.ConsoleAppender;
+import org.apache.logging.log4j.core.config.Configurator;
+import org.apache.logging.log4j.core.test.appender.ListAppender;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.*;
+
+/**
+ * Tests system property condition processing.
+ */
+public class SystemPropertyArbiterTest {
+
+    static final String CONFIG = "log4j2-systemPropertyArbiters.xml";
+    static LoggerContext loggerContext = null;
+
+    @AfterEach
+    public void after() {
+        loggerContext.stop();
+        loggerContext = null;
+        System.clearProperty("env");
+    }
+
+    @Test
+    public void prodTest() {
+        System.setProperty("env", "prod");
+        loggerContext = Configurator.initialize(null, CONFIG);
+        assertNotNull(loggerContext);
+        final Appender app = 
loggerContext.getConfiguration().getAppender("Out");
+        assertNotNull(app);
+        assertTrue(app instanceof ListAppender);
+    }
+
+    @Test
+    public void devTest() {
+        System.setProperty("env", "dev");
+        loggerContext = Configurator.initialize(null, CONFIG);
+        assertNotNull(loggerContext);
+        final Appender app = 
loggerContext.getConfiguration().getAppender("Out");
+        assertNotNull(app);
+        assertTrue(app instanceof ConsoleAppender);
+    }
+
+    @Test void classArbiterTest() {
+        loggerContext = Configurator.initialize(null, CONFIG);
+        assertNotNull(loggerContext);
+        Appender app = 
loggerContext.getConfiguration().getAppender("ShouldExist");
+        assertNotNull(app);
+        assertTrue(app instanceof ListAppender);
+        app = loggerContext.getConfiguration().getAppender("ShouldNotExist");
+        assertNull(app);
+    }
+}
diff --git a/log4j-core-test/src/test/resources/log4j2-environmentArbiters.xml 
b/log4j-core-test/src/test/resources/log4j2-environmentArbiters.xml
new file mode 100644
index 0000000000..6333c93de2
--- /dev/null
+++ b/log4j-core-test/src/test/resources/log4j2-environmentArbiters.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<Configuration name="ConfigTest" status="ERROR" monitorInterval="5">
+  <Appenders>
+
+    <EnvironmentArbiter propertyName="ENV" propertyValue="dev">
+      <Console name="Out">
+        <PatternLayout pattern="%m%n"/>
+      </Console>
+    </EnvironmentArbiter>
+    <EnvironmentArbiter propertyName="ENV" propertyValue="prod">
+      <List name="Out">
+      </List>
+    </EnvironmentArbiter>
+
+  </Appenders>
+  <Loggers>
+    <Logger name="org.apache.test" level="trace" additivity="false">
+      <AppenderRef ref="Out"/>
+    </Logger>
+    <Root level="error">
+      <AppenderRef ref="Out"/>
+    </Root>
+  </Loggers>
+</Configuration>
diff --git 
a/log4j-core-test/src/test/resources/log4j2-systemPropertyArbiters.xml 
b/log4j-core-test/src/test/resources/log4j2-systemPropertyArbiters.xml
new file mode 100644
index 0000000000..0c350af825
--- /dev/null
+++ b/log4j-core-test/src/test/resources/log4j2-systemPropertyArbiters.xml
@@ -0,0 +1,48 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ~ 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.
+  -->
+<Configuration name="ConfigTest" status="ERROR" monitorInterval="5">
+  <Appenders>
+
+    <SystemPropertyArbiter propertyName="env" propertyValue="dev">
+      <Console name="Out">
+        <PatternLayout pattern="%m%n"/>
+      </Console>
+    </SystemPropertyArbiter>
+    <SystemPropertyArbiter propertyName="env" propertyValue="prod">
+      <List name="Out">
+      </List>
+    </SystemPropertyArbiter>
+    <ClassArbiter 
className="org.apache.logging.log4j.core.config.arbiters.ClassArbiter">
+      <List name="ShouldExist">
+      </List>
+    </ClassArbiter>
+    <ClassArbiter className="org.apache.logging.log4j.core.DoesNotExist">
+      <List name="ShouldNotExist">
+      </List>
+    </ClassArbiter>
+
+  </Appenders>
+  <Loggers>
+    <Logger name="org.apache.test" level="trace" additivity="false">
+      <AppenderRef ref="Out"/>
+    </Logger>
+    <Root level="error">
+      <AppenderRef ref="Out"/>
+    </Root>
+  </Loggers>
+</Configuration>
diff --git 
a/log4j-core/src/main/java/org/apache/logging/log4j/core/config/arbiters/EnvironmentArbiter.java
 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/arbiters/EnvironmentArbiter.java
new file mode 100644
index 0000000000..36c9c701b3
--- /dev/null
+++ 
b/log4j-core/src/main/java/org/apache/logging/log4j/core/config/arbiters/EnvironmentArbiter.java
@@ -0,0 +1,93 @@
+/*
+ * 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.logging.log4j.core.config.arbiters;
+
+import org.apache.logging.log4j.core.config.Node;
+import org.apache.logging.log4j.core.config.plugins.Plugin;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderAttribute;
+import org.apache.logging.log4j.core.config.plugins.PluginBuilderFactory;
+
+/**
+ * Condition that determines if the specified environment variable is set.
+ */
+@Plugin(name = "EnvironmentArbiter", category = Node.CATEGORY, elementType = 
Arbiter.ELEMENT_TYPE,
+        deferChildren = true, printObject = true)
+public class EnvironmentArbiter implements Arbiter {
+
+    private final String propertyName;
+    private final String propertyValue;
+
+    private EnvironmentArbiter(final String propertyName, final String 
propertyValue) {
+        this.propertyName = propertyName;
+        this.propertyValue = propertyValue;
+    }
+
+
+    /**
+     * Returns true if either the environment variable is defined (it has any 
value) or the property value
+     * matches the requested value.
+     */
+    @Override
+    public boolean isCondition() {
+        String value = System.getenv(propertyName);
+        return value != null && (propertyValue == null || 
value.equals(propertyValue));
+    }
+
+    @PluginBuilderFactory
+    public static Builder newBuilder() {
+        return new Builder();
+    }
+
+    public static class Builder implements 
org.apache.logging.log4j.core.util.Builder<EnvironmentArbiter> {
+
+        public static final String ATTR_PROPERTY_NAME = "propertyName";
+        public static final String ATTR_PROPERTY_VALUE = "propertyValue";
+
+        @PluginBuilderAttribute(ATTR_PROPERTY_NAME)
+        private String propertyName;
+
+        @PluginBuilderAttribute(ATTR_PROPERTY_VALUE)
+        private String propertyValue;
+        /**
+         * Sets the Property Name.
+         * @param propertyName the property name.
+         * @return this
+         */
+        public Builder setPropertyName(final String propertyName) {
+            this.propertyName = propertyName;
+            return asBuilder();
+        }
+
+        /**
+         * Sets the Property Value.
+         * @param propertyValue the property value.
+         * @return this
+         */
+        public Builder setPropertyValue(final String propertyValue) {
+            this.propertyValue = propertyValue;
+            return asBuilder();
+        }
+
+        public Builder asBuilder() {
+            return this;
+        }
+
+        public EnvironmentArbiter build() {
+            return new EnvironmentArbiter(propertyName, propertyValue);
+        }
+    }
+}

Reply via email to