-
-
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 #3703
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
Merged
Merged
Changes from 1 commit
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
123 changes: 123 additions & 0 deletions
123
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,123 @@ | ||
| /* | ||
| * 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.ComponentBuilder; | ||
| 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 ComponentBuilder<?> monitorResourcesComponent = configBuilder.newComponent("MonitorResources"); | ||
| monitorResourcesComponent.addComponent(configBuilder | ||
| .newComponent("MonitorResource") | ||
| .addAttribute("uri", externalResourceFile1.toUri().toString())); | ||
| monitorResourcesComponent.addComponent(configBuilder | ||
| .newComponent("MonitorResource") | ||
| .addAttribute("uri", externalResourceFile2.toUri().toString())); | ||
|
|
||
| final Configuration config = configBuilder | ||
| .setConfigurationSource(configSource) | ||
| .setMonitorInterval(String.valueOf(monitorInterval)) | ||
| .addComponent(monitorResourcesComponent) | ||
| .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); | ||
| } | ||
| } | ||
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
19 changes: 19 additions & 0 deletions
19
log4j-core-test/src/test/resources/AsyncWaitStrategyFactoryConfigTest.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,19 @@ | ||
| # | ||
| # 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. | ||
| # | ||
|
|
||
| strategy.type = AsyncWaitStrategyFactory | ||
| strategy.class = org.apache.logging.log4j.core.async.AsyncWaitStrategyFactoryConfigTest$YieldingWaitStrategyFactory |
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
Oops, something went wrong.
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.