Skip to content

Commit bf2ad2c

Browse files
Update for code review comments and added documentation
Signed-off-by: MichaelMorris <michael.morris@est.tech>
1 parent 443a932 commit bf2ad2c

File tree

6 files changed

+155
-15
lines changed

6 files changed

+155
-15
lines changed

log4j-core/src/main/java/org/apache/logging/log4j/core/config/AbstractConfiguration.java

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ public void initialize() {
269269
setup();
270270
setupAdvertisement();
271271
doConfigure();
272+
watchMonitorUris();
272273
setState(State.INITIALIZED);
273274
LOGGER.debug("Configuration {} initialized", this);
274275
}
@@ -325,19 +326,10 @@ public void start() {
325326
LOGGER.info("Starting configuration {}...", this);
326327
this.setStarting();
327328
if (watchManager.getIntervalSeconds() >= 0) {
328-
if (uris != null && uris.size() > 0) {
329-
LOGGER.info(
330-
"Start watching for changes to {} and {} every {} seconds",
331-
getConfigurationSource(),
332-
uris,
333-
watchManager.getIntervalSeconds());
334-
watchMonitorUris();
335-
} else {
336-
LOGGER.info(
337-
"Start watching for changes to {} every {} seconds",
338-
getConfigurationSource(),
339-
watchManager.getIntervalSeconds());
340-
}
329+
LOGGER.info(
330+
"Start watching for changes to {} every {} seconds",
331+
watchManager.getConfigurationWatchers().keySet(),
332+
watchManager.getIntervalSeconds());
341333
watchManager.start();
342334
}
343335
if (hasAsyncLoggers()) {
@@ -359,7 +351,7 @@ public void start() {
359351
}
360352

361353
private void watchMonitorUris() {
362-
if (this instanceof Reconfigurable) {
354+
if (this instanceof Reconfigurable && watchManager.getIntervalSeconds() >= 0) {
363355
uris.stream().forEach(uri -> {
364356
Source source = new Source(uri);
365357
final ConfigurationFileWatcher watcher = new ConfigurationFileWatcher(
@@ -802,7 +794,7 @@ private List<URI> convertToJavaNetUris(final List<Uri> uris) {
802794
try {
803795
javaNetUris.add(new URI(uri.getUri()));
804796
} catch (URISyntaxException e) {
805-
LOGGER.error("Error parsing monitor URI: " + uri, e);
797+
throw new ConfigurationException("Invalid URI provided for MonitorUri " + uri);
806798
}
807799
}
808800
return javaNetUris;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
{
2+
"Configuration": {
3+
"monitorInterval": "30",
4+
"MonitorUris": {
5+
"Uri": [
6+
{
7+
"uri": "to-be-monitored-1.file"
8+
},
9+
{
10+
"uri": "to-be-monitored-2.file"
11+
}
12+
]
13+
}
14+
}
15+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to you under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
##
18+
monitorInterval = 30
19+
monitorUri.0.uri = to-be-monitored-1.file
20+
monitorUri.1.uri = to-be-monitored-2.file
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Licensed to the Apache Software Foundation (ASF) under one or more
4+
~ contributor license agreements. See the NOTICE file distributed with
5+
~ this work for additional information regarding copyright ownership.
6+
~ The ASF licenses this file to you under the Apache License, Version 2.0
7+
~ (the "License"); you may not use this file except in compliance with
8+
~ the License. You may obtain a copy of the License at
9+
~
10+
~ http://www.apache.org/licenses/LICENSE-2.0
11+
~
12+
~ Unless required by applicable law or agreed to in writing, software
13+
~ distributed under the License is distributed on an "AS IS" BASIS,
14+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
~ See the License for the specific language governing permissions and
16+
~ limitations under the License.
17+
-->
18+
<Configuration xmlns="https://logging.apache.org/xml/ns"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xsi:schemaLocation="
21+
https://logging.apache.org/xml/ns
22+
https://logging.apache.org/xml/ns/log4j-config-2.xsd"
23+
monitorInterval="30">
24+
<MonitorUris>
25+
<Uri uri="to-be-monitored-1.file"/>
26+
<Uri uri="to-be-monitored-2.file"/>
27+
</MonitorUris>
28+
</Configuration>
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#
2+
# Licensed to the Apache Software Foundation (ASF) under one or more
3+
# contributor license agreements. See the NOTICE file distributed with
4+
# this work for additional information regarding copyright ownership.
5+
# The ASF licenses this file to you under the Apache License, Version 2.0
6+
# (the "License"); you may not use this file except in compliance with
7+
# the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
#
17+
Configuration:
18+
monitorInterval: '30'
19+
MonitorUris:
20+
Uri:
21+
- uri: to-be-monitored-1.file
22+
- uri: to-be-monitored-2.file

src/site/antora/modules/ROOT/pages/manual/configuration.adoc

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,69 @@ include::example$manual/configuration/routing.properties[tag=appender]
988988
Therefore, the dollar `$` sign needs to be escaped.
989989
<2> All the attributes of children of the `Route` element have a **deferred** evaluation. Therefore, they need only one `$` sign.
990990
991+
[#monitorUris]
992+
== Monitor URIs
993+
994+
Log4j can be configured to poll for changes to resources (in addition to the configuration file) using the `MonitorUris` element of the configuration. If a change is detected in any of the specifed resources, Log4j automatically reconfigures the logger context.
995+
996+
The polling interval is determined by the value of the <<configuration-attribute-monitorInterval>> attribute. If set to 0, polling is disabled. See <<configuration-attribute-monitorInterval>> for further details.
997+
998+
This may be useful where the configuration is dependent on a resource that is external to the configuration file.
999+
1000+
The MonitorUris component supports the following nested element:
1001+
1002+
[#MonitorUris-elements]
1003+
[cols="1m,1,4"]
1004+
|===
1005+
| Type | Multiplicity | Description
1006+
1007+
| `Uri`
1008+
| one or more
1009+
| Specifies the URI of a resouce to be polled for changes.
1010+
|===
1011+
1012+
The Uri component supports the following attribute:
1013+
[#uri-attributes-name]
1014+
=== `uri`
1015+
1016+
[cols="1h,5"]
1017+
|===
1018+
| Type | `String`
1019+
|===
1020+
1021+
See example below:
1022+
1023+
[tabs]
1024+
====
1025+
XML::
1026+
+
1027+
[source,xml]
1028+
----
1029+
include::example$manual/configuration/monitoruris.xml[lines=18..]
1030+
----
1031+
1032+
JSON::
1033+
+
1034+
[source,json]
1035+
----
1036+
include::example$manual/configuration/monitoruris.json[]
1037+
----
1038+
1039+
YAML::
1040+
+
1041+
[source,yaml]
1042+
----
1043+
include::example$manual/configuration/monitoruris.yaml[lines=17..]
1044+
----
1045+
1046+
Properties::
1047+
+
1048+
[source,properties]
1049+
----
1050+
include::example$manual/configuration/monitoruris.properties[lines=18..]
1051+
----
1052+
====
1053+
9911054
[id=arbiters]
9921055
== [[Arbiters]] Arbiters
9931056

0 commit comments

Comments
 (0)