diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java b/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java index 1466129dca..860533e668 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/ClassicConstants.java @@ -60,4 +60,6 @@ public class ClassicConstants { public static final String FINALIZE_SESSION = "FINALIZE_SESSION"; public static final Marker FINALIZE_SESSION_MARKER = MarkerFactory.getMarker(FINALIZE_SESSION); + final public static String AUTOCONFIG_FILE = "logback.xml"; + final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml"; } diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/joran/SerializedModelConfigurator.java b/logback-classic/src/main/java/ch/qos/logback/classic/joran/SerializedModelConfigurator.java index 388cb87e4e..363dbbcfe2 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/joran/SerializedModelConfigurator.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/joran/SerializedModelConfigurator.java @@ -98,10 +98,13 @@ private void buildModelInterpretationContext(Model topModel) { } private Model retrieveModel(URL url) { + long start = System.currentTimeMillis(); try (InputStream is = url.openStream()) { HardenedModelInputStream hmis = new HardenedModelInputStream(is); Model model = (Model) hmis.readObject(); + long diff = System.currentTimeMillis() - start; + addInfo("Model at ["+url+"] read in "+diff + " milliseconds"); return model; } catch(IOException e) { addError("Failed to open "+url, e); diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java b/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java index 77dc2cff7f..21b98e93c1 100755 --- a/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/spi/LoggingEvent.java @@ -332,7 +332,7 @@ public long getSequenceNumber() { return sequenceNumber; } - public void setSquenceNumber(long sn) { + public void setSequenceNumber(long sn) { sequenceNumber = sn; } 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 86b66520af..7b5010d223 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 @@ -37,13 +37,22 @@ */ public class ContextInitializer { - final public static String AUTOCONFIG_FILE = DefaultJoranConfigurator.AUTOCONFIG_FILE; - final public static String TEST_AUTOCONFIG_FILE = DefaultJoranConfigurator.TEST_AUTOCONFIG_FILE; + /** + * @deprecated Please use ClassicConstants.AUTOCONFIG_FILE instead + */ + final public static String AUTOCONFIG_FILE = ClassicConstants.AUTOCONFIG_FILE; + /** + * @deprecated Please use ClassicConstants.TEST_AUTOCONFIG_FILE instead + */ + final public static String TEST_AUTOCONFIG_FILE = ClassicConstants.TEST_AUTOCONFIG_FILE; /** * @deprecated Please use ClassicConstants.CONFIG_FILE_PROPERTY instead */ final public static String CONFIG_FILE_PROPERTY = ClassicConstants.CONFIG_FILE_PROPERTY; + String[] INTERNAL_CONFIGURATOR_CLASSNAME_LIST = {"ch.qos.logback.classic.joran.SerializedModelConfigurator", + "ch.qos.logback.classic.util.DefaultJoranConfigurator", "ch.qos.logback.classic.BasicConfigurator"}; + final LoggerContext loggerContext; final ContextAware contextAware; @@ -66,41 +75,59 @@ public void autoConfig(ClassLoader classLoader) throws JoranException { StatusListenerConfigHelper.installIfAsked(loggerContext); + // invoke custom configurators List configuratorList = ClassicEnvUtil.loadFromServiceLoader(Configurator.class, classLoader); - configuratorList.sort(rankComparator); - - printConfiguratorOrder(configuratorList); + if (configuratorList.isEmpty()) { + contextAware.addInfo("No custom configurators were discovered as a service."); + } else { + printConfiguratorOrder(configuratorList); + } for (Configurator c : configuratorList) { - try { - long start = System.currentTimeMillis(); - contextAware.addInfo("Constructed configurator of type " + c.getClass()); - c.setContext(loggerContext); - Configurator.ExecutionStatus status = c.configure(loggerContext); - printDuration(start, c, status); - if (status == Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY) { - return; - } - } catch (Exception e) { - throw new LogbackException(String.format("Failed to initialize Configurator: %s using ServiceLoader", - c != null ? c.getClass().getCanonicalName() : "null"), e); - } + if (invokeConfigure(c) == Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY) + return; } + // invoke internal configurators + for (String configuratorClassName : INTERNAL_CONFIGURATOR_CLASSNAME_LIST) { + contextAware.addInfo("Trying to configure with "+configuratorClassName); + Configurator c = instantiateConfiguratorByClassName(configuratorClassName, classLoader); + if(c == null) + continue; + if (invokeConfigure(c) == Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY) + return; + } + } + private Configurator instantiateConfiguratorByClassName(String configuratorClassName, ClassLoader classLoader) { + try { + Class classObj = classLoader.loadClass(configuratorClassName); + return (Configurator) classObj.getConstructor().newInstance(); + } catch (ReflectiveOperationException e) { + contextAware.addInfo("Instantiation failure: " + e.toString()); + return null; + } + } -// long startJoranConfiguration = System.currentTimeMillis(); -// Configurator.ExecutionStatus es = attemptConfigurationUsingJoranUsingReflexion(classLoader); -// -// if (es == Configurator.ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY) { -// printDuration(startJoranConfiguration, JORAN_CONFIGURATION_DURATION_MSG, true); -// return; -// } -// printDuration(startJoranConfiguration, JORAN_CONFIGURATION_DURATION_MSG, false); -// -// // at this stage invoke basicConfigurator -// fallbackOnToBasicConfigurator(); + /** + * + * @param configurator + * @return true if enclosing loop should break, false otherwise + */ + private Configurator.ExecutionStatus invokeConfigure(Configurator configurator) { + try { + long start = System.currentTimeMillis(); + contextAware.addInfo("Constructed configurator of type " + configurator.getClass()); + configurator.setContext(loggerContext); + Configurator.ExecutionStatus status = configurator.configure(loggerContext); + printDuration(start, configurator, status); + return status; + + } catch (Exception e) { + throw new LogbackException(String.format("Failed to initialize or to run Configurator: %s", + configurator != null ? configurator.getClass().getCanonicalName() : "null"), e); + } } private void printConfiguratorOrder(List configuratorList) { @@ -131,13 +158,6 @@ private Configurator.ExecutionStatus attemptConfigurationUsingJoranUsingReflexio } - private void fallbackOnToBasicConfigurator() { - BasicConfigurator basicConfigurator = new BasicConfigurator(); - basicConfigurator.setContext(loggerContext); - basicConfigurator.configure(loggerContext); - } - - Comparator rankComparator = new Comparator() { @Override public int compare(Configurator c1, Configurator c2) { diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/util/DefaultJoranConfigurator.java b/logback-classic/src/main/java/ch/qos/logback/classic/util/DefaultJoranConfigurator.java index 3bf6d90bae..00ed95247e 100644 --- a/logback-classic/src/main/java/ch/qos/logback/classic/util/DefaultJoranConfigurator.java +++ b/logback-classic/src/main/java/ch/qos/logback/classic/util/DefaultJoranConfigurator.java @@ -26,9 +26,6 @@ @ConfiguratorRank(value = ConfiguratorRank.NOMINAL) public class DefaultJoranConfigurator extends ContextAwareBase implements Configurator { - final public static String AUTOCONFIG_FILE = "logback.xml"; - final public static String TEST_AUTOCONFIG_FILE = "logback-test.xml"; - @Override public ExecutionStatus configure(Context context) { URL url = performMultiStepConfigurationFileSearch(true); @@ -52,12 +49,12 @@ private URL performMultiStepConfigurationFileSearch(boolean updateStatus) { return url; } - url = getResource(TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus); + url = getResource(ClassicConstants.TEST_AUTOCONFIG_FILE, myClassLoader, updateStatus); if (url != null) { return url; } - return getResource(AUTOCONFIG_FILE, myClassLoader, updateStatus); + return getResource(ClassicConstants.AUTOCONFIG_FILE, myClassLoader, updateStatus); } public void configureByResource(URL url) throws JoranException { if (url == null) { diff --git a/logback-classic/src/main/java/module-info.java b/logback-classic/src/main/java/module-info.java index a33e343fe4..28fafa54bb 100644 --- a/logback-classic/src/main/java/module-info.java +++ b/logback-classic/src/main/java/module-info.java @@ -19,9 +19,6 @@ requires ch.qos.logback.core; uses ch.qos.logback.core.spi.Configurator; provides org.slf4j.spi.SLF4JServiceProvider with ch.qos.logback.classic.spi.LogbackServiceProvider; - provides ch.qos.logback.core.spi.Configurator with ch.qos.logback.classic.joran.SerializedModelConfigurator, - ch.qos.logback.classic.util.DefaultJoranConfigurator, - ch.qos.logback.classic.BasicConfigurator; exports ch.qos.logback.classic; exports ch.qos.logback.classic.boolex; diff --git a/logback-classic/src/main/resources/META-INF/services/ch.qos.logback.core.spi.Configurator b/logback-classic/src/main/resources/META-INF/services/ch.qos.logback.core.spi.Configurator deleted file mode 100644 index d1c8bb88be..0000000000 --- a/logback-classic/src/main/resources/META-INF/services/ch.qos.logback.core.spi.Configurator +++ /dev/null @@ -1,9 +0,0 @@ -# Configurators are invoked according to their rank, highest rank is invoked first -# SerializedModelConfigurator has rank 10 -# DefaultJoranConfigurator has rank 0 (NOMINAL) -# BasicConfigurator has rank -10 (FALLBACK) -# -ch.qos.logback.classic.joran.SerializedModelConfigurator -ch.qos.logback.classic.util.DefaultJoranConfigurator -ch.qos.logback.classic.BasicConfigurator - diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java index 57e0264a7b..7ed2261c36 100644 --- a/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java +++ b/logback-classic/src/test/java/ch/qos/logback/classic/pattern/ConverterTest.java @@ -423,7 +423,7 @@ public void testSequenceNumber() { assertTrue(converter.isStarted()); LoggingEvent event = makeLoggingEvent(null); - event.setSquenceNumber(123); + event.setSequenceNumber(123); assertEquals("123", converter.convert(event)); StatusPrinter.print(loggerContext); }