From d94ebec6edfdd6e7e3737e9e4c3cd1551e8cdec0 Mon Sep 17 00:00:00 2001 From: Ceki Gulcu Date: Mon, 6 Nov 2023 11:11:18 +0100 Subject: [PATCH] fix guthub issue 715 Signed-off-by: Ceki Gulcu --- .../logback/classic/util/ContextInitializer.java | 6 ++++++ .../java/ch/qos/logback/core/util/Loader.java | 16 +++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java b/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java index 086b457002..312a10e49d 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/util/ContextInitializer.java @@ -21,6 +21,7 @@ import ch.qos.logback.core.spi.ContextAwareImpl; import ch.qos.logback.core.status.InfoStatus; import ch.qos.logback.core.util.EnvUtil; +import ch.qos.logback.core.util.Loader; import ch.qos.logback.core.util.StatusListenerConfigHelper; import java.util.Comparator; @@ -65,7 +66,12 @@ public void autoConfig() throws JoranException { autoConfig(Configurator.class.getClassLoader()); } + public void autoConfig(ClassLoader classLoader) throws JoranException { + + // see https://github.com/qos-ch/logback/issues/715 + classLoader = Loader.systemClassloaderIfNull(classLoader); + String versionStr = EnvUtil.logbackVersion(); if (versionStr == null) { versionStr = CoreConstants.NA; diff --git a/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java b/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java index 88ab0336d1..4959e6de6c 100644 --- a/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java +++ b/logback-core/src/main/java/ch/qos/logback/core/util/Loader.java @@ -56,6 +56,21 @@ public Boolean run() { }); } + /** + * This method is used to sanitize the cl argument in case it is null. + * + * @param cl a class loader, may be null + * @return the system class loader if the cl argument is null, return cl otherwise. + * + * @since 1.4.12 + */ + public static ClassLoader systemClassloaderIfNull(ClassLoader cl) { + if(cl == null) + return ClassLoader.getSystemClassLoader(); + else + return cl; + } + /** * Compute the number of occurrences a resource can be found by a class loader. * @@ -64,7 +79,6 @@ public Boolean run() { * @return * @throws IOException */ - public static Set getResources(String resource, ClassLoader classLoader) throws IOException { // See LBCLASSIC-159 Set urlSet = new HashSet();