Skip to content
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

Add option to provide URIs to monitor in addition to the config file #3501

Open
wants to merge 2 commits into
base: 2.x
Choose a base branch
from
Open
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
Original file line number Diff line number Diff line change
@@ -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.logging.log4j.core;

import static org.awaitility.Awaitility.waitAtMost;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Collections;
import java.util.concurrent.TimeUnit;
import org.apache.logging.log4j.core.config.Configuration;
import org.apache.logging.log4j.core.config.ConfigurationSource;
import org.apache.logging.log4j.core.config.Configurator;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilder;
import org.apache.logging.log4j.core.config.builder.api.ConfigurationBuilderFactory;
import org.apache.logging.log4j.core.config.properties.PropertiesConfiguration;
import org.apache.logging.log4j.core.util.Source;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.CleanupMode;
import org.junit.jupiter.api.io.TempDir;

public class MonitorUriTest {

private static final int MONITOR_INTERVAL = 3;

@Test
void testReconfigureOnChangeInMonitorUri(@TempDir(cleanup = CleanupMode.ON_SUCCESS) final Path tempDir)
throws IOException {
ConfigurationBuilder<PropertiesConfiguration> configBuilder =
ConfigurationBuilderFactory.newConfigurationBuilder(PropertiesConfiguration.class);
Path config = tempDir.resolve("config.xml");
Path monitorUri = tempDir.resolve("monitorUri.xml");
ConfigurationSource configSource = new ConfigurationSource(new Source(config), new byte[] {}, 0);
Configuration configuration = configBuilder
.setConfigurationSource(configSource)
.setMonitorInterval(String.valueOf(MONITOR_INTERVAL))
.add(configBuilder.newMonitorUri(monitorUri.toUri().toString()))
.build();

try (LoggerContext loggerContext = Configurator.initialize(configuration)) {
Files.write(monitorUri, Collections.singletonList("a change"));
waitAtMost(MONITOR_INTERVAL + 2, TimeUnit.SECONDS)
.until(() -> loggerContext.getConfiguration() != configuration);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
Expand Down Expand Up @@ -132,6 +133,7 @@ public abstract class AbstractConfiguration extends AbstractFilterable implement
private ConcurrentMap<String, Appender> appenders = new ConcurrentHashMap<>();
private ConcurrentMap<String, LoggerConfig> loggerConfigs = new ConcurrentHashMap<>();
private List<CustomLevelConfig> customLevels = Collections.emptyList();
private List<URI> uris = Collections.emptyList();
private final ConcurrentMap<String, String> propertyMap = new ConcurrentHashMap<>();
private final Interpolator tempLookup = new Interpolator(propertyMap);
private final StrSubstitutor runtimeStrSubstitutor = new RuntimeStrSubstitutor(tempLookup);
Expand Down Expand Up @@ -323,10 +325,19 @@ public void start() {
LOGGER.info("Starting configuration {}...", this);
this.setStarting();
if (watchManager.getIntervalSeconds() >= 0) {
LOGGER.info(
"Start watching for changes to {} every {} seconds",
getConfigurationSource(),
watchManager.getIntervalSeconds());
if (uris != null && uris.size() > 0) {
LOGGER.info(
"Start watching for changes to {} and {} every {} seconds",
getConfigurationSource(),
uris,
watchManager.getIntervalSeconds());
watchManager.addMonitorUris(configurationSource.getSource(), uris);
} else {
LOGGER.info(
"Start watching for changes to {} every {} seconds",
getConfigurationSource(),
watchManager.getIntervalSeconds());
}
watchManager.start();
}
if (hasAsyncLoggers()) {
Expand Down Expand Up @@ -729,9 +740,16 @@ protected void doConfigure() {
} else if (child.isInstanceOf(AsyncWaitStrategyFactoryConfig.class)) {
final AsyncWaitStrategyFactoryConfig awsfc = child.getObject(AsyncWaitStrategyFactoryConfig.class);
asyncWaitStrategyFactory = awsfc.createWaitStrategyFactory();
} else if (child.isInstanceOf(MonitorUris.class)) {
uris = convertToJavaNetUris(child.getObject(MonitorUris.class).getUris());
} else {
final List<String> expected = Arrays.asList(
"\"Appenders\"", "\"Loggers\"", "\"Properties\"", "\"Scripts\"", "\"CustomLevels\"");
"\"Appenders\"",
"\"Loggers\"",
"\"Properties\"",
"\"Scripts\"",
"\"CustomLevels\"",
"\"MonitorUris\"");
LOGGER.error(
"Unknown object \"{}\" of type {} is ignored: try nesting it inside one of: {}.",
child.getName(),
Expand Down Expand Up @@ -767,6 +785,18 @@ protected void doConfigure() {
setParents();
}

private List<URI> convertToJavaNetUris(final List<Uri> uris) {
final List<URI> javaNetUris = new ArrayList<>();
for (Uri uri : uris) {
try {
javaNetUris.add(new URI(uri.getUri()));
} catch (URISyntaxException e) {
LOGGER.error("Error parsing monitor URI: " + uri, e);
}
}
return javaNetUris;
}

public static Level getDefaultLevel() {
final String levelName = PropertiesUtil.getProperties()
.getStringProperty(DefaultConfiguration.DEFAULT_LEVEL, Level.ERROR.name());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@
package org.apache.logging.log4j.core.config;

import java.io.File;
import java.net.URI;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.logging.log4j.core.util.AbstractWatcher;
import org.apache.logging.log4j.core.util.FileWatcher;
import org.apache.logging.log4j.core.util.Source;
Expand All @@ -28,38 +31,57 @@
*/
public class ConfigurationFileWatcher extends AbstractWatcher implements FileWatcher {

private File file;
private long lastModifiedMillis;
private Map<File, Long> monitoredFiles = new HashMap<>();

public ConfigurationFileWatcher(
final Configuration configuration,
final Reconfigurable reconfigurable,
final List<ConfigurationListener> configurationListeners,
long lastModifiedMillis) {
super(configuration, reconfigurable, configurationListeners);
this.lastModifiedMillis = lastModifiedMillis;
}

@Override
public long getLastModified() {
return file != null ? file.lastModified() : 0;
Long lastModifiedMillis = 0L;
for (final File monitoredFile : monitoredFiles.keySet()) {
if (monitoredFile.lastModified() > lastModifiedMillis) {
lastModifiedMillis = monitoredFile.lastModified();
}
}
return lastModifiedMillis;
}

@Override
public void fileModified(final File file) {
lastModifiedMillis = file.lastModified();
monitoredFiles.entrySet().stream()
.forEach(monitoredFile ->
monitoredFile.setValue(monitoredFile.getKey().lastModified()));
}

@Override
public void watching(final Source source) {
file = source.getFile();
lastModifiedMillis = file.lastModified();
File file = source.getFile();
monitoredFiles.put(file, file.lastModified());
super.watching(source);
}

/**
* Add the given URIs to be watched.
*
* @param monitorUris URIs to also watch
*/
public void addMonitorUris(final List<URI> monitorUris) {
monitorUris.forEach(uri -> {
File additionalFile = new Source(uri).getFile();
monitoredFiles.put(additionalFile, additionalFile.lastModified());
});
}

@Override
public boolean isModified() {
return lastModifiedMillis != file.lastModified();
return monitoredFiles.entrySet().stream()
.anyMatch(file -> file.getValue() != file.getKey().lastModified());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -382,4 +382,8 @@ public String toString() {
return null;
}
}

Source getSource() {
return this.source;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginElement;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;

/**
* Container for MonitorUri objects.
*/
@Plugin(name = "MonitorUris", category = Core.CATEGORY_NAME, printObject = true)
public final class MonitorUris {

private final List<Uri> uris;

private MonitorUris(final Uri[] uris) {
this.uris = new ArrayList<>(Arrays.asList(uris));
}

/**
* Create a MonitorUris object to contain all the URIs to be monitored.
*
* @param uris An array of URIs.
* @return A MonitorUris object.
*/
@PluginFactory
public static MonitorUris createMonitorUris( //
@PluginElement("Uris") final Uri[] uris) {
return new MonitorUris(uris == null ? Uri.EMPTY_ARRAY : uris);
}

/**
* Returns a list of the {@code Uri} objects created during configuration.
* @return the URIs to be monitored
*/
public List<Uri> getUris() {
return uris;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* 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;

import java.util.Objects;
import org.apache.logging.log4j.core.Core;
import org.apache.logging.log4j.core.config.plugins.Plugin;
import org.apache.logging.log4j.core.config.plugins.PluginFactory;
import org.apache.logging.log4j.core.config.plugins.PluginValue;
import org.apache.logging.log4j.status.StatusLogger;

/**
* Descriptor of a URI object that is created via configuration.
*/
@Plugin(name = "Uri", category = Core.CATEGORY_NAME, printObject = true)
public final class Uri {

/**
* The empty array.
*/
static final Uri[] EMPTY_ARRAY = {};

private final String uri;

private Uri(final String uri) {
this.uri = Objects.requireNonNull(uri, "uri is null");
}

/**
* Creates a Uri object.
*
* @param uri the URI.
* @return A Uri object.
*/
@PluginFactory
public static Uri createUri( // @formatter:off
@PluginValue("uri") final String uri) {
// @formatter:on

StatusLogger.getLogger().debug("Creating Uri('{}')", uri);
return new Uri(uri);
}

/**
* Returns the URI.
*
* @return the URI
*/
public String getUri() {
return uri;
}

@Override
public int hashCode() {
return uri.hashCode();
}

@Override
public boolean equals(final Object object) {
if (this == object) {
return true;
}
if (!(object instanceof Uri)) {
return false;
}
final Uri other = (Uri) object;
return this.uri == other.uri;
}

@Override
public String toString() {
return "Uri[" + uri + "]";
}
}
Loading