Skip to content

Add environment variable arbiter #1312

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions log4j-core-test/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,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>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* 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.Rule;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.Test;

import com.github.stefanbirkner.systemlambda.SystemLambda;

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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
import static org.junit.jupiter.api.Assertions.*;

/**
* Tests basic condition processing.
* Tests system property condition processing.
*/
public class BasicArbiterTest {
public class SystemPropertyArbiterTest {

static final String CONFIG = "log4j2-arbiters.xml";
static final String CONFIG = "log4j2-systemPropertyArbiters.xml";
static LoggerContext loggerContext = null;

@AfterEach
Expand Down
41 changes: 41 additions & 0 deletions log4j-core-test/src/test/resources/log4j2-environmentArbiters.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?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>
Original file line number Diff line number Diff line change
@@ -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);
}
}
}