use of io.quarkiverse.logback.runtime.LogbackRecorder in project quarkus-logging-logback by quarkiverse.
the class LoggingLogbackProcessor method init.
@Record(ExecutionTime.STATIC_INIT)
@BuildStep
void init(LogbackRecorder recorder, RecorderContext context, BuildProducer<RunTimeConfigurationDefaultBuildItem> runTimeConfigurationDefaultBuildItemBuildProducer, BuildProducer<GeneratedClassBuildItem> generatedClasses, OutputTargetBuildItem outputTargetBuildItem, CurateOutcomeBuildItem curateOutcomeBuildItem, ShutdownContextBuildItem shutdownContextBuildItem) throws Exception {
// first check the versions
doVersionCheck();
URL url = getUrl();
if (url == null) {
return;
}
context.registerSubstitution(StartEvent.class, StartSub.class, (Class) EventSubstitution.class);
context.registerSubstitution(BodyEvent.class, BodySub.class, (Class) EventSubstitution.class);
context.registerSubstitution(EndEvent.class, EndSub.class, (Class) EventSubstitution.class);
final AtomicReference<List<SaxEvent>> events = new AtomicReference<>();
JoranConfigurator configurator = new JoranConfigurator() {
@Override
public void doConfigure(List<SaxEvent> eventList) throws JoranException {
events.set(eventList);
}
};
configurator.setContext(new LoggerContext());
configurator.doConfigure(url);
List<String> loggerPath = Arrays.asList("configuration", "logger");
List<String> rootPath = Arrays.asList("configuration", "root");
String rootLevel = null;
Map<String, String> levels = new HashMap<>();
Set<String> allClasses = new HashSet<>();
for (SaxEvent i : events.get()) {
if (i instanceof StartEvent) {
StartEvent s = ((StartEvent) i);
if (Objects.equals(loggerPath, s.elementPath.getCopyOfPartList())) {
String level = s.attributes.getValue("level");
if (level != null) {
levels.put(s.attributes.getValue("name"), level);
}
} else if (Objects.equals(rootPath, s.elementPath.getCopyOfPartList())) {
String level = s.attributes.getValue("level");
if (level != null) {
rootLevel = level;
}
}
int classIndex = s.attributes.getIndex("class");
if (classIndex != -1) {
allClasses.add(s.attributes.getValue(classIndex));
}
}
}
boolean disableConsole = false;
Set<String> delayedClasses = new HashSet<>();
for (String i : allClasses) {
if (i.equals("ch.qos.logback.core.ConsoleAppender")) {
disableConsole = true;
}
try {
Class<?> c = Thread.currentThread().getContextClassLoader().loadClass(i);
if (LifeCycle.class.isAssignableFrom(c)) {
delayedClasses.add(i);
}
} catch (ClassNotFoundException exception) {
throw new RuntimeException(exception);
}
}
if (disableConsole) {
runTimeConfigurationDefaultBuildItemBuildProducer.produce(new RunTimeConfigurationDefaultBuildItem("quarkus.log.console.enable", "false"));
}
for (String i : delayedClasses) {
try (ClassCreator c = new ClassCreator(new GeneratedClassGizmoAdaptor(generatedClasses, (Function<String, String>) s -> s.substring(s.length() - LogbackRecorder.DELAYED.length())), i + LogbackRecorder.DELAYED, null, i, DelayedStart.class.getName())) {
MethodCreator start = c.getMethodCreator("start", void.class);
start.invokeStaticMethod(MethodDescriptor.ofMethod(LogbackRecorder.class, "addDelayed", void.class, DelayedStart.class), start.getThis());
start.returnValue(null);
MethodCreator method = c.getMethodCreator("doQuarkusDelayedStart", void.class);
method.invokeSpecialMethod(MethodDescriptor.ofMethod(i, "start", void.class), method.getThis());
method.returnValue(null);
}
}
if (rootLevel != null) {
runTimeConfigurationDefaultBuildItemBuildProducer.produce(new RunTimeConfigurationDefaultBuildItem("quarkus.log.level", rootLevel));
}
for (Map.Entry<String, String> e : levels.entrySet()) {
runTimeConfigurationDefaultBuildItemBuildProducer.produce(new RunTimeConfigurationDefaultBuildItem("quarkus.log.category.\"" + e.getKey() + "\".level", e.getValue()));
}
Map<String, String> buildProperties = new HashMap<>(outputTargetBuildItem.getBuildSystemProperties().entrySet().stream().collect(Collectors.toMap(Object::toString, Object::toString)));
buildProperties.put(PROJECT_VERSION, curateOutcomeBuildItem.getEffectiveModel().getAppArtifact().getVersion());
recorder.init(events.get(), delayedClasses, shutdownContextBuildItem, buildProperties);
}
Aggregations