diff --git a/deployment/pom.xml b/deployment/pom.xml index 4991e17..8ca8014 100644 --- a/deployment/pom.xml +++ b/deployment/pom.xml @@ -15,6 +15,10 @@ io.quarkus quarkus-arc-deployment + + io.quarkus + quarkus-vertx-http-deployment + io.quarkiverse.chappie quarkus-chappie diff --git a/deployment/src/main/java/io/quarkiverse/chappie/deployment/ChappieProcessor.java b/deployment/src/main/java/io/quarkiverse/chappie/deployment/ChappieProcessor.java index 44a97dc..9899ae5 100644 --- a/deployment/src/main/java/io/quarkiverse/chappie/deployment/ChappieProcessor.java +++ b/deployment/src/main/java/io/quarkiverse/chappie/deployment/ChappieProcessor.java @@ -1,5 +1,9 @@ package io.quarkiverse.chappie.deployment; +import io.quarkiverse.chappie.runtime.LogMessageReceiver; +import io.quarkus.arc.deployment.AdditionalBeanBuildItem; +import io.quarkus.deployment.IsDevelopment; +import io.quarkus.deployment.annotations.BuildProducer; import io.quarkus.deployment.annotations.BuildStep; import io.quarkus.deployment.builditem.FeatureBuildItem; @@ -11,4 +15,9 @@ class ChappieProcessor { FeatureBuildItem feature() { return new FeatureBuildItem(FEATURE); } + + @BuildStep(onlyIf = IsDevelopment.class) + void registerBeans(BuildProducer additionalBeans) { + additionalBeans.produce(AdditionalBeanBuildItem.unremovableOf(LogMessageReceiver.class)); + } } diff --git a/runtime/pom.xml b/runtime/pom.xml index fde687a..e51e176 100644 --- a/runtime/pom.xml +++ b/runtime/pom.xml @@ -15,6 +15,11 @@ io.quarkus quarkus-arc + + + io.quarkus + quarkus-vertx-http + diff --git a/runtime/src/main/java/io/quarkiverse/chappie/runtime/LogMessageReceiver.java b/runtime/src/main/java/io/quarkiverse/chappie/runtime/LogMessageReceiver.java new file mode 100644 index 0000000..098e6df --- /dev/null +++ b/runtime/src/main/java/io/quarkiverse/chappie/runtime/LogMessageReceiver.java @@ -0,0 +1,61 @@ +package io.quarkiverse.chappie.runtime; + +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.event.Observes; +import jakarta.inject.Inject; + +import org.jboss.logging.Logger; + +import io.quarkus.devui.runtime.logstream.LogStreamBroadcaster; +import io.quarkus.runtime.StartupEvent; +import io.smallrye.mutiny.operators.multi.processors.BroadcastProcessor; +import io.vertx.core.json.JsonArray; +import io.vertx.core.json.JsonObject; + +/** + * Receive Log messages, and check if there is an exception + */ +@ApplicationScoped +public class LogMessageReceiver { + private static final Logger LOGGER = Logger.getLogger(LogMessageReceiver.class); + + @Inject + LogStreamBroadcaster logStreamBroadcaster; + + void onStart(@Observes StartupEvent ev) { + LOGGER.info("Chappie is listening for exceptions...."); + + BroadcastProcessor logStream = logStreamBroadcaster.getLogStream(); + + logStream.subscribe().with((JsonObject item) -> { + if (item.containsKey(STACKTRACE)) { + JsonArray stacktrace = item.getJsonArray(STACKTRACE); + if (!stacktrace.isEmpty()) { + System.out.println(">>>>>>> CHAPPIE FOUND AN EXCEPTION !"); + String fileName = getFileName(item); + int lineNumber = item.getInteger(SOURCE_LINE_NUMBER); + + System.out.println("\t\t " + fileName + " | line " + lineNumber); + } + } + }); + + } + + private String getFileName(JsonObject item) { + String classNameFull = item.getString(SOURCE_CLASS_NAME_FULL); + String fileName = item.getString(SOURCE_FILE_NAME); + int lastDotIndex = classNameFull.lastIndexOf('.'); + + if (lastDotIndex == -1) { + return fileName; // No package + } + return classNameFull.substring(0, lastDotIndex + 1) + fileName; + } + + private static final String STACKTRACE = "stacktrace"; + private static final String SOURCE_CLASS_NAME_FULL = "sourceClassNameFull"; + private static final String SOURCE_FILE_NAME = "sourceFileName"; + private static final String SOURCE_LINE_NUMBER = "sourceLineNumber"; + +}