Skip to content

Commit

Permalink
BUGFIX: Observers configured from environment should read lower-case …
Browse files Browse the repository at this point in the history
…property

* Resolves #54
  • Loading branch information
jhannes committed Nov 7, 2021
1 parent 865f113 commit e1fc75c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.Optional;
import java.util.Properties;
import java.util.Set;
import java.util.TreeMap;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import java.util.stream.Stream;
Expand Down Expand Up @@ -102,9 +103,15 @@ public class DefaultLogEventConfigurator implements LogEventConfigurator {
private final Path propertiesDir;
private Thread configurationWatcher;
private boolean runningInsideJunit;
private final Map<String, String> environment;

public DefaultLogEventConfigurator(Path propertiesDir) {
this(propertiesDir, System.getenv());
}

public DefaultLogEventConfigurator(Path propertiesDir, Map<String, String> environment) {
this.propertiesDir = propertiesDir;
this.environment = environment;
}

public DefaultLogEventConfigurator() {
Expand Down Expand Up @@ -243,7 +250,7 @@ protected List<String> getProfiles() {
* @return Properties with the configuration of all files merged together
*/
protected Map<String, String> loadPropertiesFromFiles(List<String> configurationFileNames) {
Map<String, String> properties = new HashMap<>();
Map<String, String> properties = new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
LogEventStatus.getInstance().addConfig(this, "Loading configuration from " + configurationFileNames);
for (String filename : configurationFileNames) {
loadConfigResource(filename)
Expand Down Expand Up @@ -322,10 +329,6 @@ protected Properties loadConfigResource(String resourceName) {
* @param properties The merged configuration that should be applied to the factory
*/
public void applyConfigurationProperties(LogEventFactory factory, Map<String, String> properties) {
applyConfigurationProperties(factory, properties, System.getenv());
}

protected void applyConfigurationProperties(LogEventFactory factory, Map<String, String> properties, Map<String, String> environment) {
Configuration logeventsConfig = new Configuration(properties, "logevents", environment);
LogEventStatus.getInstance().configure(logeventsConfig);
showWelcomeMessage(properties);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -159,16 +159,26 @@ public void shouldSetLoggerObserverFromProperties() {
}

@Test
public void shouldSetLoggerAndObserverFromEnvironment() {
public void shouldSetLoggerAndObserverFromEnvironment() throws IOException {
Map<String, String> environment = new HashMap<>();
environment.put("LOGEVENTS_OBSERVER_BUFFER", "CircularBufferLogEventObserver");
environment.put("LOGEVENTS_LOGGER_ORG_EXAMPLE_DEMO", "DEBUG buffer");
environment.put("LOGEVENTS_INCLUDEPARENT_ORG_EXAMPLE_DEMO", "false");
configurator.applyConfigurationProperties(factory, configuration, environment);

Properties properties = new Properties();
properties.put("observer.buffer.capacity", "15");
deleteConfigFiles();
writeProps(propertiesDir.resolve("logevents.properties"), properties);

configurator = new DefaultLogEventConfigurator(propertiesDir, environment);
configurator.configure(factory);

CircularBufferLogEventObserver buffer = (CircularBufferLogEventObserver)factory.getObserver("buffer");
assertEquals("CircularBufferLogEventObserver{size=0,capacity=15}", buffer.toString());

LoggerConfiguration logger = factory.getLogger("org.example.demo");
assertEquals("LevelThresholdFilter{DEBUG}", logger.getOwnFilter().toString());
assertEquals("CircularBufferLogEventObserver{size=0,capacity=15}", logger.getObserver());

logger.info("Hello");
assertEquals("Hello", buffer.singleMessage());
Expand Down Expand Up @@ -350,7 +360,6 @@ public void shouldRecoverToDefaultConfigurationOnInvalidConfigurationFile() thro
LogEventStatus.getInstance().setThreshold(StatusEvent.StatusLevel.NONE);
propertiesDir = Paths.get("target", "test-data", "invalid", "properties");
deleteConfigFiles();
Files.createDirectories(propertiesDir);

Properties defaultProperties = new Properties();
defaultProperties.setProperty("logevents.what", "This should throw an error");
Expand Down Expand Up @@ -423,7 +432,6 @@ public void shouldScanPropertiesFilesWhenFileIsChanged() throws IOException, Int
LogEventStatus.getInstance().setThreshold(StatusEvent.StatusLevel.NONE);
propertiesDir = Paths.get("target", "test-data", "scan-change", "properties");
deleteConfigFiles();
Files.createDirectories(propertiesDir);

Properties defaultProperties = new Properties();
defaultProperties.setProperty("root", "DEBUG");
Expand Down Expand Up @@ -457,7 +465,6 @@ public void shouldWriteStatusLogIfConfigFileIsLocked() throws IOException {

propertiesDir = Paths.get("target", "test-data", "faulty" + System.currentTimeMillis());
deleteConfigFiles();
Files.createDirectories(propertiesDir);
Path propsFile = propertiesDir.resolve("logevents-faultyconfig.properties");
writeProps(propsFile, new Properties());

Expand Down Expand Up @@ -497,7 +504,6 @@ public void shouldScanPropertiesFilesWhenHigherPriorityFileIsAdded() throws IOEx
LogEventStatus.getInstance().setThreshold(StatusEvent.StatusLevel.NONE);
propertiesDir = Paths.get("target", "test-data", "scan-new", "properties");
deleteConfigFiles();
Files.createDirectories(propertiesDir);

Properties defaultProperties = new Properties();
defaultProperties.setProperty("root", "DEBUG");
Expand Down Expand Up @@ -550,6 +556,7 @@ public void shouldInstallDefaultExceptionHandler() throws InterruptedException {


public static void writeProps(Path file, Properties defaultProperties) throws IOException {
Files.createDirectories(file.getParent());
try (FileWriter writer = new FileWriter(file.toFile())) {
defaultProperties.store(writer, "Default configuration file");
}
Expand Down

0 comments on commit e1fc75c

Please sign in to comment.