Skip to content

Commit f2e1130

Browse files
committed
slighltly better configuration watch list reporting
Signed-off-by: Ceki Gulcu <[email protected]>
1 parent 164754b commit f2e1130

File tree

4 files changed

+61
-18
lines changed

4 files changed

+61
-18
lines changed

logback-classic/src/main/java/ch/qos/logback/classic/joran/ReconfigureOnChangeTask.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,22 @@ public String toString() {
183183
return "ReconfigureOnChangeTask(born:" + birthdate + ")";
184184
}
185185

186-
186+
/**
187+
* Contains typo. Replaced by {@link #setScheduledFuture(ScheduledFuture)}.
188+
* @param aScheduledFuture
189+
* @deprecated
190+
*/
191+
@Deprecated
187192
public void setScheduredFuture(ScheduledFuture<?> aScheduledFuture) {
193+
setScheduledFuture(aScheduledFuture);
194+
}
195+
196+
/**
197+
* Replaces {@link #setScheduredFuture(ScheduledFuture)}
198+
* @param aScheduledFuture
199+
* @since 1.5.19
200+
*/
201+
public void setScheduledFuture(ScheduledFuture<?> aScheduledFuture) {
188202
this.scheduledFuture = aScheduledFuture;
189203
}
190204
}

logback-classic/src/main/java/ch/qos/logback/classic/model/processor/ConfigurationModelHandlerFull.java

+14-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
import ch.qos.logback.classic.joran.ReconfigureOnChangeTask;
1717
import ch.qos.logback.classic.model.ConfigurationModel;
1818
import ch.qos.logback.core.Context;
19+
import ch.qos.logback.core.joran.spi.ConfigurationWatchList;
1920
import ch.qos.logback.core.joran.util.ConfigurationWatchListUtil;
2021
import ch.qos.logback.core.model.Model;
2122
import ch.qos.logback.core.model.processor.ModelHandlerBase;
@@ -25,7 +26,6 @@
2526
import ch.qos.logback.core.util.Duration;
2627
import ch.qos.logback.core.util.OptionHelper;
2728

28-
import java.net.URL;
2929
import java.util.concurrent.ScheduledExecutorService;
3030
import java.util.concurrent.ScheduledFuture;
3131
import java.util.concurrent.TimeUnit;
@@ -58,6 +58,18 @@ public void postHandle(ModelInterpretationContext mic, Model model) throws Model
5858
// post handling of scan attribute works even we need to watch for included files because the main url is
5959
// set in GenericXMLConfigurator very early in the configuration process
6060
postProcessScanAttrib(mic, configurationModel);
61+
62+
ConfigurationWatchList cwl = ConfigurationWatchListUtil.getConfigurationWatchList(getContext());
63+
if (cwl != null) {
64+
try {
65+
addInfo("Main configuration file URL: " + cwl.getMainURL());
66+
addInfo("FileWatchList= {" + cwl.getFileWatchListAsStr()+"}");
67+
addInfo("URLWatchList= {" + cwl.getUrlWatchListAsStr()+"}");
68+
} catch(NoSuchMethodError e) {
69+
addWarn("It looks like the version of logback-classic is more recent than");
70+
addWarn("the version of logback-core. Please align the two versions.");
71+
}
72+
}
6173
}
6274

6375
protected void postProcessScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
@@ -103,7 +115,7 @@ public void detachedPostProcess(String scanStr, String scanPeriodStr) {
103115

104116
ScheduledFuture<?> scheduledFuture = scheduledExecutorService.scheduleAtFixedRate(rocTask, duration.getMilliseconds(), duration.getMilliseconds(),
105117
TimeUnit.MILLISECONDS);
106-
rocTask.setScheduredFuture(scheduledFuture);
118+
rocTask.setScheduledFuture(scheduledFuture);
107119
context.addScheduledFuture(scheduledFuture);
108120
}
109121

logback-core/src/main/java/ch/qos/logback/core/joran/spi/ConfigurationWatchList.java

+22-12
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import java.io.File;
2020
import java.net.HttpURLConnection;
21-
import java.net.MalformedURLException;
2221
import java.net.URL;
2322
import java.net.URLDecoder;
2423
import java.security.NoSuchAlgorithmException;
@@ -30,6 +29,8 @@
3029
import static ch.qos.logback.core.CoreConstants.PROPERTIES_FILE_EXTENSION;
3130

3231
/**
32+
* This class manages the list of files and/or urls that are watched for changes.
33+
*
3334
* @author Ceki G&uuml;lc&uuml;
3435
*/
3536
public class ConfigurationWatchList extends ContextAwareBase {
@@ -130,18 +131,16 @@ private void addAsHTTP_or_HTTPS_URLToWatch(URL url) {
130131
}
131132

132133
/**
133-
* Add the url but only if it is file://.
134-
* @param url should be a file
134+
* Add the url but only if it is file:// or http(s)://
135+
* @param url should be a file or http(s)
135136
*/
136-
137137
public void addToWatchList(URL url) {
138+
// assume that the caller has checked that the protocol is one of {file, https, http}.
138139
String protocolStr = url.getProtocol();
139140
if (protocolStr.equals(FILE_PROTOCOL_STR)) {
140141
addAsFileToWatch(url);
141142
} else if (isHTTP_Or_HTTPS(protocolStr)) {
142143
addAsHTTP_or_HTTPS_URLToWatch(url);
143-
} else {
144-
addInfo("Cannot watch ["+url + "] as its protocol is not one of file, http or https.");
145144
}
146145
}
147146

@@ -269,7 +268,7 @@ static public boolean isWatchableProtocol(URL url) {
269268
return false;
270269
}
271270
String protocolStr = url.getProtocol();
272-
return Arrays.stream(WATCHABLE_PROTOCOLS).anyMatch(protocol -> protocol.equalsIgnoreCase(protocolStr));
271+
return isWatchableProtocol(protocolStr);
273272
}
274273

275274
/**
@@ -283,12 +282,23 @@ static public boolean isWatchableProtocol(String protocolStr) {
283282
return Arrays.stream(WATCHABLE_PROTOCOLS).anyMatch(protocol -> protocol.equalsIgnoreCase(protocolStr));
284283
}
285284

286-
@Override
287-
public String toString() {
288-
289-
String fileWatchListStr = fileWatchList.stream().map(File::getPath).collect(Collectors.joining(", "));
285+
/**
286+
* Returns the urlWatchList field as a String
287+
* @return the urlWatchList field as a String
288+
* @since 1.5.19
289+
*/
290+
public String getUrlWatchListAsStr() {
290291
String urlWatchListStr = urlWatchList.stream().map(URL::toString).collect(Collectors.joining(", "));
292+
return urlWatchListStr;
293+
}
291294

292-
return "ConfigurationWatchList(" + "mainURL=" + mainURL + ", fileWatchList={" + fileWatchListStr + "}, urlWatchList=[" + urlWatchListStr + "})";
295+
/**
296+
* Returns the fileWatchList field as a String
297+
* @return the fileWatchList field as a String
298+
* @since 1.5.19
299+
*/
300+
public String getFileWatchListAsStr() {
301+
return fileWatchList.stream().map(File::getPath).collect(Collectors.joining(", "));
293302
}
303+
294304
}

logback-core/src/main/java/ch/qos/logback/core/joran/util/ConfigurationWatchListUtil.java

+10-3
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@
2424
import java.net.URL;
2525

2626
/**
27+
* A thin layer on top of {@link ConfigurationWatchList}.
28+
*
2729
* @author Ceki G&uuml;lc&uuml;
2830
*/
2931
public class ConfigurationWatchListUtil {
@@ -85,9 +87,14 @@ public static void addToWatchList(Context context, URL url, boolean createCWL) {
8587
}
8688
}
8789

88-
addInfo(context, "Adding [" + url + "] to configuration watch list.");
89-
cwl.addToWatchList(url);
90-
90+
String protocol = url.getProtocol();
91+
if(cwl.isWatchableProtocol(protocol)) {
92+
addInfo(context, "Will add [" + url + "] to configuration watch list.");
93+
cwl.addToWatchList(url);
94+
} else {
95+
addInfo(context, "Will not add configuration file ["+url + "] to watch list, because '"+protocol+"' protocol is not watchable.");
96+
addInfo(context, "Only the protocols 'file', 'http' and 'https' are watchable.");
97+
}
9198
}
9299

93100
private static ConfigurationWatchList registerNewConfigurationWatchListWithContext(Context context) {

0 commit comments

Comments
 (0)