Skip to content

Commit

Permalink
ongoing work on GraalVM support
Browse files Browse the repository at this point in the history
Signed-off-by: Ceki Gulcu <ceki@qos.ch>
  • Loading branch information
ceki committed Jul 26, 2023
1 parent 9c31880 commit efbeff6
Show file tree
Hide file tree
Showing 13 changed files with 71 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
*
* @author Ceki G&uuml;lc&uuml;
*/
@ConfiguratorRank(value = ConfiguratorRank.Value.FALLBACK)
@ConfiguratorRank(value = ConfiguratorRank.FALLBACK)
public class BasicConfigurator extends ContextAwareBase implements Configurator {

public BasicConfigurator() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public class ClassicConstants {
* property name for the model file used for configuration
* @since 1.3.9/1.4.9
*/
public static final String MODEL_CONFIG_FILE_PROPERTY = "logback.modelFile";
public static final String MODEL_CONFIG_FILE_PROPERTY = "logback.serializedModelFilew";

public static final String JNDI_CONFIGURATION_RESOURCE = JNDI_JAVA_NAMESPACE
+ "comp/env/logback/configuration-resource";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import ch.qos.logback.classic.ClassicConstants;
import ch.qos.logback.classic.joran.serializedModel.HardenedModelInputStream;
import ch.qos.logback.classic.model.processor.LogbackClassicDefaultNestedComponentRules;
import ch.qos.logback.classic.spi.ConfiguratorRank;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.LogbackException;
import ch.qos.logback.core.model.Model;
Expand All @@ -43,6 +44,7 @@
*/

// BEWARE: the fqcn is used in SerializedModelModelHandler
@ConfiguratorRank(value = ConfiguratorRank.SERIALIZED_MODEL)
public class SerializedModelConfigurator extends ContextAwareBase implements Configurator {

final public static String AUTOCONFIG_MODEL_FILE = "logback"+ MODEL_CONFIG_FILE_EXTENSION;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ public void handle(ModelInterpretationContext mic, Model model) {
}

protected void processScanAttrib(ModelInterpretationContext mic, ConfigurationModel configurationModel) {
System.out.println("This is overriden processScanAttrib");
String scanStr = mic.subst(configurationModel.getScanStr());
if (!OptionHelper.isNullOrEmpty(scanStr) && !"false".equalsIgnoreCase(scanStr)) {
addInfo("Skipping ReconfigureOnChangeTask registration");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,19 @@
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface ConfiguratorRank {
enum Value {
FIRST,
REGULAR,
FALLBACK;
}

Value value() default Value.REGULAR;
static public int FALLBACK = -10;
static public int NOMINAL = 0;
static public int SERIALIZED_MODEL = 10;

static public int DEFAULT = 20;

static public int CUSTOM_LOW_PRIORITY = DEFAULT;

static public int CUSTOM_NORMAL_PRIORITY = 30;

static public int CUSTOM_HIGH_PRIORITY = 40;

static public int CUSTOM_TOP_PRIORITY = 50;
public int value() default DEFAULT;
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,6 @@ public class ContextInitializer {
* @deprecated Please use ClassicConstants.CONFIG_FILE_PROPERTY instead
*/
final public static String CONFIG_FILE_PROPERTY = ClassicConstants.CONFIG_FILE_PROPERTY;
private static final String JORAN_CONFIGURATION_DURATION_MSG = "JoranConfiguration duration ";
private static final String CONFIGURATION_AS_A_SERVICE_DURATION_MSG = "Configuration as a service duration ";

final LoggerContext loggerContext;

Expand All @@ -67,18 +65,21 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
loggerContext.getStatusManager().add(new InfoStatus(CoreConstants.LOGBACK_CLASSIC_VERSION_MESSAGE + versionStr, loggerContext));
StatusListenerConfigHelper.installIfAsked(loggerContext);

long startConfigurationAsAService = System.currentTimeMillis();

List<Configurator> configuratorList = ClassicEnvUtil.loadFromServiceLoader(Configurator.class, classLoader);

configuratorList.sort(rankComparator);

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) {
printDuration(startConfigurationAsAService, CONFIGURATION_AS_A_SERVICE_DURATION_MSG, true);
return;
}
} catch (Exception e) {
Expand All @@ -87,25 +88,33 @@ public void autoConfig(ClassLoader classLoader) throws JoranException {
}
}

printDuration(startConfigurationAsAService, CONFIGURATION_AS_A_SERVICE_DURATION_MSG, false);

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);
// 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();
}

// at this stage invoke basicConfigurator
fallbackOnToBasicConfigurator();
private void printConfiguratorOrder(List<Configurator> configuratorList) {
contextAware.addInfo("Here is a list of configurators discovered as a service, by rank: ");
for(Configurator c: configuratorList) {
contextAware.addInfo(" "+c.getClass().getName());
}
contextAware.addInfo("They will be invoked in order until ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY is returned.");
}

private void printDuration(long start, String message, boolean success) {
private void printDuration(long start, Configurator configurator, Configurator.ExecutionStatus executionStatus) {
long end = System.currentTimeMillis();
long configurationAsAServiceDuration = end - start;
contextAware.addInfo(message+configurationAsAServiceDuration + " milliseconds. Success status="+success);
long diff = end - start;
contextAware.addInfo( configurator.getClass().getName()+".configure() call lasted "+diff + " milliseconds. ExecutionStatus="+executionStatus);
}

private Configurator.ExecutionStatus attemptConfigurationUsingJoranUsingReflexion(ClassLoader classLoader) {
Expand All @@ -128,21 +137,6 @@ private void fallbackOnToBasicConfigurator() {
basicConfigurator.configure(loggerContext);
}

// private void sortByPriority(List<Configurator> configuratorList) {
// configuratorList.sort(new Comparator<Configurator>() {
// @Override
// public int compare(Configurator o1, Configurator o2) {
// if (o1.getClass() == o2.getClass())
// return 0;
// if (o1 instanceof DefaultJoranConfigurator) {
// return 1;
// }
//
// // otherwise do not intervene
// return 0;
// }
// });
// }

Comparator<Configurator> rankComparator = new Comparator<Configurator>() {
@Override
Expand All @@ -151,39 +145,21 @@ public int compare(Configurator c1, Configurator c2) {
ConfiguratorRank r1 = c1.getClass().getAnnotation(ConfiguratorRank.class);
ConfiguratorRank r2 = c2.getClass().getAnnotation(ConfiguratorRank.class);

ConfiguratorRank.Value value1 = r1 == null ? ConfiguratorRank.Value.REGULAR : r1.value();
ConfiguratorRank.Value value2 = r2 == null ? ConfiguratorRank.Value.REGULAR : r2.value();
int value1 = r1 == null ? ConfiguratorRank.DEFAULT : r1.value();
int value2 = r2 == null ? ConfiguratorRank.DEFAULT : r2.value();

int result = compareRankValue(value1, value2);
// reverse the result for high to low sort
return (-result);
}
};

private int compareRankValue(ConfiguratorRank.Value value1, ConfiguratorRank.Value value2) {

switch (value1) {
case FIRST:
if (value2 == ConfiguratorRank.Value.FIRST)
return 0;
else
return 1;
case REGULAR:
if (value2 == ConfiguratorRank.Value.FALLBACK)
return 1;
else if (value2 == ConfiguratorRank.Value.REGULAR)
return 0;
else
return -1;
case FALLBACK:
if (value2 == ConfiguratorRank.Value.FALLBACK)
return 0;
else
return -1;

default:
private int compareRankValue(int value1, int value2) {
if(value1 > value2)
return 1;
else if (value1 == value2)
return 0;
}
else return -1;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
/**
* @since 1.3.0-beta1
*/
@ConfiguratorRank(value = ConfiguratorRank.Value.REGULAR)
// Note that DefaultJoranConfigurator is invoked via reflection

@ConfiguratorRank(value = ConfiguratorRank.NOMINAL)
public class DefaultJoranConfigurator extends ContextAwareBase implements Configurator {

final public static String AUTOCONFIG_FILE = "logback.xml";
Expand All @@ -37,7 +39,7 @@ public ExecutionStatus configure(Context context) {
} catch (JoranException e) {
e.printStackTrace();
}
// we tried and that counts Mary.
// You tried and that counts Mary.
return ExecutionStatus.DO_NOT_INVOKE_NEXT_IF_ANY;
} else {
return ExecutionStatus.INVOKE_NEXT_IF_ANY;
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
ch.qos.logback.classic.joran.SerializedModelConfigurator
# 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

2 changes: 1 addition & 1 deletion logback-classic/src/test/input/joran/model/minimal.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<configuration debug="false">

<serializeModel file="${target.smo}"/>
<serializeModel file="${targetModelFile}"/>

<logger name="ModelSerializationTest" level="DEBUG"/>
</configuration>
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package ch.qos.logback.classic.joran;

import ch.qos.logback.classic.AsyncAppender;
import ch.qos.logback.classic.ClassicConstants;
import ch.qos.logback.classic.ClassicTestConstants;
import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.Logger;
Expand Down Expand Up @@ -728,7 +729,7 @@ public void dateConverterWithLocale() throws JoranException {
public void modelSerialization() throws JoranException, IOException, ClassNotFoundException {
String outputPath = OUTPUT_DIR_PREFIX+"minimal_"+diff+ MODEL_CONFIG_FILE_EXTENSION;

loggerContext.putProperty("target.smo", outputPath);
loggerContext.putProperty("targetModelFile", outputPath);
configure(ClassicTestConstants.JORAN_INPUT_PREFIX + "model/minimal.xml");
StatusPrinter.print(loggerContext);

Expand All @@ -747,13 +748,13 @@ public void modelSerialization() throws JoranException, IOException, ClassNotFou
assertEquals(2, configurationModel.getSubModels().size());

SerializeModelModel smm = (SerializeModelModel) configurationModel.getSubModels().get(0);
assertEquals("${target.smo}", smm.getFile());
assertEquals("${targetModelFile}", smm.getFile());


LoggerModel loggerModel = (LoggerModel) configurationModel.getSubModels().get(1);
assertEquals("ModelSerializationTest", loggerModel.getName());

// <serializeModel file="${target.smo}"/>
// <serializeModel file="${targetModelFile}"/>
// <logger name="ModelSerializationTest" level="DEBUG"/>


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import ch.qos.logback.core.Context;
import ch.qos.logback.core.spi.ContextAwareBase;

@ConfiguratorRank(ConfiguratorRank.Value.REGULAR)
@ConfiguratorRank(ConfiguratorRank.CUSTOM_LOW_PRIORITY)
public class MockConfigurator extends ContextAwareBase implements Configurator {

static Context context = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -227,5 +227,5 @@ public class CoreConstants {

public static final String LOGBACK_CLASSIC_VERSION_MESSAGE = "This is logback-classic version ";
public static final char JSON_LINE_SEPARATOR = '\n';
final public static String MODEL_CONFIG_FILE_EXTENSION = ".smo";
final public static String MODEL_CONFIG_FILE_EXTENSION = ".scmo";
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void handle(ModelInterpretationContext modelInterpretationContext, Model
Object configuratorHint = modelInterpretationContext.getConfiguratorHint();

if(configuratorHint != null && configuratorHint.getClass().getName().equals("ch.qos.logback.classic.joran.SerializedModelConfigurator")) {
addInfo("Skipping model serialization as calling configurator is model based.");
addInfo("Skipping model serialization as calling configurator is already model based.");
return;
}

Expand Down

0 comments on commit efbeff6

Please sign in to comment.