-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Add option to provide URIs to monitor in addition to the config file #3501
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
Open
MichaelMorrisEst
wants to merge
3
commits into
apache:2.x
Choose a base branch
from
Nordix:issue-3074
base: 2.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
log4j-core-test/src/test/java/org/apache/logging/log4j/core/MonitorUriTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
log4j-core/src/main/java/org/apache/logging/log4j/core/config/MonitorUris.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
log4j-core/src/main/java/org/apache/logging/log4j/core/config/Uri.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 + "]"; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
...ain/java/org/apache/logging/log4j/core/config/builder/api/MonitorUriComponentBuilder.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/* | ||
* 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.builder.api; | ||
|
||
/** | ||
* Assembler for constructing MonitorUri Components. | ||
*/ | ||
public interface MonitorUriComponentBuilder extends ComponentBuilder<MonitorUriComponentBuilder> {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we sure we want to proceed with (re)configuration obtained from an invalid configuration? I think we should propagate the exception and let it fail.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My thinking here was to be consistent with other error scenarios, for example lines 726, 764, 790.
Also to let the exception propagate we would need to declare it on the doConfigure() method which could cause an impact on anyone with their own custom implementation which extends AbstractConfiguration