Skip to content

Commit

Permalink
LOGABCK-1717 configuirators shipping with logback-classic are instant…
Browse files Browse the repository at this point in the history
…iated via reflection

Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Aug 4, 2023
1 parent f4fe391 commit 6001f5b
Show file tree
Hide file tree
Showing 8 changed files with 65 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ public long getSequenceNumber() {
return sequenceNumber;
}

public void setSquenceNumber(long sn) {
public void setSequenceNumber(long sn) {
sequenceNumber = sn;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -66,41 +75,59 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
StatusListenerConfigHelper.installIfAsked(loggerContext);


// invoke custom configurators
List<Configurator> 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<Configurator> configuratorList) {
Expand Down Expand Up @@ -131,13 +158,6 @@ private Configurator.ExecutionStatus attemptConfigurationUsingJoranUsingReflexio

}

private void fallbackOnToBasicConfigurator() {
BasicConfigurator basicConfigurator = new BasicConfigurator();
basicConfigurator.setContext(loggerContext);
basicConfigurator.configure(loggerContext);
}


Comparator<Configurator> rankComparator = new Comparator<Configurator>() {
@Override
public int compare(Configurator c1, Configurator c2) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions logback-classic/src/main/java/module-info.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down

0 comments on commit 6001f5b

Please sign in to comment.