-
-
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
Closed
Closed
Changes from 7 commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
de15d2d
Add option to provide URIs to monitor in addition to the config file
MichaelMorrisEst 02d64a3
Reworked to to make monitorUris a dedicated element instead of an att…
MichaelMorrisEst 443a932
Updated for review comments
MichaelMorrisEst bf2ad2c
Update for code review comments and added documentation
MichaelMorrisEst 23b55a7
Merge branch 'apache:2.x' into issue-3074
MichaelMorrisEst 5afac25
Rename `MonitorUris` to `MonitorResources`
vy 6f55ae7
Updated for review comments and failing test case
MichaelMorrisEst 3d16973
Updated for code review comments
MichaelMorrisEst aa6ceb3
Fix `monitor-resources.properties`
vy 3878fb6
Rename `MonitorResource.Builder::withUri` to `setUri`
vy 21fba0d
Simplify `MonitorResourcesTest`
vy 3d66a6b
Fix Spotless failures
vy ffcaf51
Removed no longer needed classes
MichaelMorrisEst 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
116 changes: 116 additions & 0 deletions
116
log4j-core-test/src/test/java/org/apache/logging/log4j/core/MonitorResourcesTest.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,116 @@ | ||
/* | ||
* 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.assertj.core.api.Assertions.assertThat; | ||
import static org.awaitility.Awaitility.waitAtMost; | ||
|
||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.nio.file.Path; | ||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.Set; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
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.test.junit.LoggerContextSource; | ||
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 MonitorResourcesTest { | ||
|
||
@Test | ||
void test_reconfiguration(@TempDir(cleanup = CleanupMode.ON_SUCCESS) final Path tempDir) throws IOException { | ||
final ConfigurationBuilder<PropertiesConfiguration> configBuilder = | ||
ConfigurationBuilderFactory.newConfigurationBuilder(PropertiesConfiguration.class); | ||
final Path configFile = tempDir.resolve("log4j.xml"); | ||
final Path externalResourceFile1 = tempDir.resolve("external-resource-1.txt"); | ||
final Path externalResourceFile2 = tempDir.resolve("external-resource-2.txt"); | ||
final ConfigurationSource configSource = new ConfigurationSource(new Source(configFile), new byte[] {}, 0); | ||
final int monitorInterval = 3; | ||
final Configuration config = configBuilder | ||
.setConfigurationSource(configSource) | ||
.setMonitorInterval(String.valueOf(monitorInterval)) | ||
.add(configBuilder.newMonitorResource( | ||
externalResourceFile1.toUri().toString())) | ||
.add(configBuilder.newMonitorResource( | ||
externalResourceFile2.toUri().toString())) | ||
.build(); | ||
|
||
try (final LoggerContext loggerContext = Configurator.initialize(config)) { | ||
assertMonitorResourceFileNames( | ||
loggerContext, | ||
configFile.getFileName().toString(), | ||
externalResourceFile1.getFileName().toString(), | ||
externalResourceFile2.getFileName().toString()); | ||
Files.write(externalResourceFile2, Collections.singletonList("a change")); | ||
waitAtMost(2 * monitorInterval, TimeUnit.SECONDS).until(() -> loggerContext.getConfiguration() != config); | ||
} | ||
} | ||
|
||
@Test | ||
@LoggerContextSource("config/MonitorResource/log4j.xml") | ||
void test_config_of_type_XML(final LoggerContext loggerContext) { | ||
assertMonitorResourceFileNames(loggerContext, "log4j.xml"); | ||
} | ||
|
||
@Test | ||
@LoggerContextSource("config/MonitorResource/log4j.json") | ||
void test_config_of_type_JSON(final LoggerContext loggerContext) { | ||
assertMonitorResourceFileNames(loggerContext, "log4j.json"); | ||
} | ||
|
||
@Test | ||
@LoggerContextSource("config/MonitorResource/log4j.yaml") | ||
void test_config_of_type_YAML(final LoggerContext loggerContext) { | ||
assertMonitorResourceFileNames(loggerContext, "log4j.yaml"); | ||
} | ||
|
||
@Test | ||
@LoggerContextSource("config/MonitorResource/log4j.properties") | ||
void test_config_of_type_properties(final LoggerContext loggerContext) { | ||
assertMonitorResourceFileNames(loggerContext, "log4j.properties"); | ||
} | ||
|
||
private static void assertMonitorResourceFileNames(final LoggerContext loggerContext, final String configFileName) { | ||
assertMonitorResourceFileNames(loggerContext, configFileName, "external-file-1.txt", "external-file-2.txt"); | ||
} | ||
|
||
private static void assertMonitorResourceFileNames( | ||
final LoggerContext loggerContext, final String configFileName, final String... externalResourceFileNames) { | ||
final Set<Source> sources = loggerContext | ||
.getConfiguration() | ||
.getWatchManager() | ||
.getConfigurationWatchers() | ||
.keySet(); | ||
final Set<String> actualFileNames = | ||
sources.stream().map(source -> source.getFile().getName()).collect(Collectors.toSet()); | ||
final Set<String> expectedFileNames = new LinkedHashSet<>(); | ||
expectedFileNames.add(configFileName); | ||
expectedFileNames.addAll(Arrays.asList(externalResourceFileNames)); | ||
assertThat(actualFileNames).as("watch manager sources: %s", sources).isEqualTo(expectedFileNames); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
log4j-core-test/src/test/resources/config/MonitorResource/log4j.json
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,15 @@ | ||
{ | ||
"Configuration": { | ||
"monitorInterval": "30", | ||
"MonitorResources": { | ||
"MonitorResource": [ | ||
{ | ||
"uri": "file://path/to/external-file-1.txt" | ||
}, | ||
{ | ||
"uri": "file://path/to/external-file-2.txt" | ||
} | ||
] | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
log4j-core-test/src/test/resources/config/MonitorResource/log4j.properties
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. | ||
# | ||
monitorInterval = 30 | ||
monitorResources.type = MonitorResources | ||
monitorResources.0.type = MonitorResource | ||
monitorResources.0.uri = file://path/to/external-file-1.txt | ||
monitorResources.1.type = MonitorResource | ||
monitorResources.1.uri = file://path/to/external-file-2.txt |
28 changes: 28 additions & 0 deletions
28
log4j-core-test/src/test/resources/config/MonitorResource/log4j.xml
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,28 @@ | ||
<?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 xmlns="https://logging.apache.org/xml/ns" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:schemaLocation=" | ||
https://logging.apache.org/xml/ns | ||
https://logging.apache.org/xml/ns/log4j-config-2.xsd" | ||
monitorInterval="30"> | ||
<MonitorResources> | ||
<MonitorResource uri="file://path/to/external-file-1.txt"/> | ||
<MonitorResource uri="file://path/to/external-file-2.txt"/> | ||
</MonitorResources> | ||
</Configuration> |
22 changes: 22 additions & 0 deletions
22
log4j-core-test/src/test/resources/config/MonitorResource/log4j.yaml
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. | ||
# | ||
Configuration: | ||
monitorInterval: '30' | ||
MonitorResources: | ||
MonitorResource: | ||
- uri: "file://path/to/external-file-1.txt" | ||
- uri: "file://path/to/external-file-2.txt" |
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
74 changes: 74 additions & 0 deletions
74
log4j-core/src/main/java/org/apache/logging/log4j/core/config/MonitorResource.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,74 @@ | ||
/* | ||
* 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 static java.util.Objects.requireNonNull; | ||
|
||
import java.net.URI; | ||
import org.apache.logging.log4j.core.Core; | ||
import org.apache.logging.log4j.core.config.plugins.Plugin; | ||
import org.apache.logging.log4j.core.config.plugins.PluginAttribute; | ||
import org.apache.logging.log4j.core.config.plugins.PluginFactory; | ||
|
||
/** | ||
* Container for the {@code MonitorResource} element. | ||
*/ | ||
@Plugin(name = "MonitorResource", category = Core.CATEGORY_NAME, printObject = true) | ||
public final class MonitorResource { | ||
|
||
private final URI uri; | ||
|
||
private MonitorResource(final URI uri) { | ||
this.uri = requireNonNull(uri, "uri"); | ||
if (!"file".equals(uri.getScheme())) { | ||
final String message = | ||
String.format("Only `file` scheme is supported in monitor resource URIs! Illegal URI: `%s`", uri); | ||
throw new IllegalArgumentException(message); | ||
} | ||
} | ||
|
||
@PluginFactory | ||
public static MonitorResource createMonitorResource(@PluginAttribute("uri") final URI uri) { | ||
return new MonitorResource(uri); | ||
} | ||
|
||
public URI 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 MonitorResource)) { | ||
return false; | ||
} | ||
final MonitorResource other = (MonitorResource) object; | ||
return this.uri == other.uri; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return String.format("MonitorResource{%s}", uri); | ||
} | ||
} |
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.
Uh oh!
There was an error while loading. Please reload this page.