diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 593f827..de79cc7 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -9,6 +9,6 @@ geantyref = "2.0.0" [libraries] junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } -log4j = { module = "org.apache.logging.log4j:log4j-core", version.ref = "log4j"} +log4j = { module = "org.apache.logging.log4j:log4j-api", version.ref = "log4j"} javacord = { module = "org.javacord:javacord", version.ref = "javacord"} geantyref = { module = "io.leangen.geantyref:geantyref", version.ref = "geantyref"} diff --git a/lib/src/main/java/net/tomatentum/marinara/Marinara.java b/lib/src/main/java/net/tomatentum/marinara/Marinara.java index 0896858..c1be562 100644 --- a/lib/src/main/java/net/tomatentum/marinara/Marinara.java +++ b/lib/src/main/java/net/tomatentum/marinara/Marinara.java @@ -1,10 +1,15 @@ package net.tomatentum.marinara; +import org.apache.logging.log4j.Logger; + import net.tomatentum.marinara.registry.InteractionCheckRegistry; import net.tomatentum.marinara.registry.InteractionRegistry; +import net.tomatentum.marinara.util.LoggerUtil; import net.tomatentum.marinara.wrapper.LibraryWrapper; public class Marinara { + + private Logger logger = LoggerUtil.getLogger(getClass()); public static Marinara load(LibraryWrapper wrapper) { return new Marinara(wrapper); @@ -18,6 +23,7 @@ public class Marinara { this.wrapper = wrapper; this.registry = new InteractionRegistry(this); this.checkRegistry = new InteractionCheckRegistry(); + logger.info("Marinara loaded successfully!"); } public InteractionRegistry getRegistry() { diff --git a/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java b/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java index 0383ac6..6b4c487 100644 --- a/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java +++ b/lib/src/main/java/net/tomatentum/marinara/checks/AppliedCheck.java @@ -5,9 +5,14 @@ import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Method; import java.util.Arrays; +import org.apache.logging.log4j.Logger; + +import net.tomatentum.marinara.util.LoggerUtil; import net.tomatentum.marinara.util.ReflectionUtil; public record AppliedCheck(InteractionCheck check, Annotation annotation) { + + private static Logger logger = LoggerUtil.getLogger(AppliedCheck.class); public boolean pre(Object context) { Method[] methods = Arrays.stream(check.getClass().getMethods()) @@ -17,9 +22,12 @@ public record AppliedCheck(InteractionCheck check, Annotation annotation) { Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType()); method.setAccessible(true); try { - return (boolean) method.invoke(check, context, annotation); + logger.debug("Executing pre check {} with context {}", check.getClass().getName(), context.toString()); + boolean result = (boolean) method.invoke(check, context, annotation); + logger.debug("Pre Check {} {} with context {}", check.getClass().getName(), result ? "succeeded" : "failed", context.toString()); + return result; } catch (IllegalAccessException | InvocationTargetException | SecurityException e) { - e.printStackTrace(); + logger.fatal(e); return false; } } @@ -32,9 +40,10 @@ public record AppliedCheck(InteractionCheck check, Annotation annotation) { Method method = ReflectionUtil.getMostSpecificMethod(methods, context.getClass(), annotation.annotationType()); method.setAccessible(true); try { + logger.debug("Executing post check {} with context {}", check.getClass().getName(), context.toString()); method.invoke(check, context, annotation); } catch (IllegalAccessException | InvocationTargetException | SecurityException e) { - e.printStackTrace(); + logger.fatal(e); } } diff --git a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java index f9c5a1c..06112ca 100644 --- a/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java +++ b/lib/src/main/java/net/tomatentum/marinara/interaction/methods/InteractionMethod.java @@ -7,6 +7,8 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.List; +import org.apache.logging.log4j.Logger; + import net.tomatentum.marinara.Marinara; import net.tomatentum.marinara.checks.AppliedCheck; import net.tomatentum.marinara.interaction.InteractionHandler; @@ -16,6 +18,8 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; import net.tomatentum.marinara.parser.AnnotationParser; import net.tomatentum.marinara.parser.InteractionCheckParser; +import net.tomatentum.marinara.util.LoggerUtil; +import net.tomatentum.marinara.util.ReflectionUtil; public abstract class InteractionMethod { @@ -33,6 +37,8 @@ public abstract class InteractionMethod { protected List parsers; protected List appliedChecks; + private Logger logger = LoggerUtil.getLogger(getClass()); + protected InteractionMethod(Method method, InteractionHandler handler, Marinara marinara @@ -67,7 +73,7 @@ public abstract class InteractionMethod { try { method.invoke(handler, getParameters(context)); }catch (IllegalAccessException | InvocationTargetException ex) { - throw new RuntimeException(ex); + logger.fatal(ex); } this.appliedChecks.forEach(x -> x.post(context)); @@ -82,11 +88,14 @@ public abstract class InteractionMethod { List parameters = new ArrayList<>(); for (int i = 0; i < parameterCount; i++) { + Object parameter; if (i == 0) { - parameters.add(context); - continue; - } - parameters.add(getParameter(context, i-1)); + parameter = context; + }else + parameter = getParameter(context, i-1); + + logger.trace("Found parameter {}={} for method {}", parameter.getClass().toString(), parameter, ReflectionUtil.getFullMethodName(method)); + parameters.add(parameter); } return parameters.toArray(); } diff --git a/lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java b/lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java index 1b7dcbb..1e5bdb4 100644 --- a/lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java +++ b/lib/src/main/java/net/tomatentum/marinara/parser/ButtonParser.java @@ -3,13 +3,19 @@ package net.tomatentum.marinara.parser; import java.lang.reflect.Method; import java.util.function.Consumer; +import org.apache.logging.log4j.Logger; + import net.tomatentum.marinara.interaction.annotation.Button; +import net.tomatentum.marinara.util.LoggerUtil; +import net.tomatentum.marinara.util.ReflectionUtil; public class ButtonParser implements AnnotationParser { private Method method; private Consumer consumer; + private Logger logger = LoggerUtil.getLogger(getClass()); + public ButtonParser(Method method, Consumer consumer) { this.method = method; this.consumer = consumer; @@ -18,6 +24,7 @@ public class ButtonParser implements AnnotationParser { @Override public void parse() { Button button = getMethod().getAnnotation(Button.class); + logger.trace("Parsed Button annotation {} for method {}", button.toString(), ReflectionUtil.getFullMethodName(method)); this.consumer.accept(button.value()); } diff --git a/lib/src/main/java/net/tomatentum/marinara/parser/InteractionCheckParser.java b/lib/src/main/java/net/tomatentum/marinara/parser/InteractionCheckParser.java index abba9e9..a397168 100644 --- a/lib/src/main/java/net/tomatentum/marinara/parser/InteractionCheckParser.java +++ b/lib/src/main/java/net/tomatentum/marinara/parser/InteractionCheckParser.java @@ -6,9 +6,13 @@ import java.util.Arrays; import java.util.Optional; import java.util.function.Consumer; +import org.apache.logging.log4j.Logger; + import net.tomatentum.marinara.checks.AppliedCheck; import net.tomatentum.marinara.checks.InteractionCheck; import net.tomatentum.marinara.registry.InteractionCheckRegistry; +import net.tomatentum.marinara.util.LoggerUtil; +import net.tomatentum.marinara.util.ReflectionUtil; public class InteractionCheckParser implements AnnotationParser { @@ -16,6 +20,8 @@ public class InteractionCheckParser implements AnnotationParser { private Method method; private Consumer consumer; + private Logger logger = LoggerUtil.getLogger(getClass()); + public InteractionCheckParser(Method method, Consumer consumer, InteractionCheckRegistry checkRegistry) { this.checkRegistry = checkRegistry; this.method = method; @@ -30,8 +36,11 @@ public class InteractionCheckParser implements AnnotationParser { private void convertAnnotation(Annotation annotation) { Optional> check = this.checkRegistry.getCheckFromAnnotation(annotation.annotationType()); - if (check.isPresent()) - consumer.accept(new AppliedCheck(check.get(), annotation)); + if (check.isPresent()) { + AppliedCheck appliedCheck = new AppliedCheck(check.get(), annotation); + logger.trace("Parsed InteractionCheck {} for annotation {} for method {}", check.getClass().getName(), annotation.toString(), ReflectionUtil.getFullMethodName(method)); + consumer.accept(appliedCheck); + } } @Override diff --git a/lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java b/lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java index 8997d9f..cf2a244 100644 --- a/lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java +++ b/lib/src/main/java/net/tomatentum/marinara/parser/SlashCommandParser.java @@ -3,10 +3,13 @@ package net.tomatentum.marinara.parser; import java.lang.reflect.Method; import java.util.function.Consumer; +import org.apache.logging.log4j.Logger; + import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.annotation.SlashCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; +import net.tomatentum.marinara.util.LoggerUtil; import net.tomatentum.marinara.util.ReflectionUtil; public class SlashCommandParser implements AnnotationParser { @@ -14,6 +17,8 @@ public class SlashCommandParser implements AnnotationParser { private Method method; private Consumer consumer; + private Logger logger = LoggerUtil.getLogger(getClass()); + public SlashCommandParser(Method method, Consumer consumer) { this.method = method; this.consumer = consumer; @@ -37,6 +42,9 @@ public class SlashCommandParser implements AnnotationParser { builder.setSubCommand(subCmd); } + ExecutableSlashCommandDefinition def = builder.build(); + + logger.trace("Parsed using SlashCommandParser for method {} with the result:\n{}", ReflectionUtil.getFullMethodName(method), def.toString()); consumer.accept(builder.build()); } diff --git a/lib/src/main/java/net/tomatentum/marinara/registry/InteractionCheckRegistry.java b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionCheckRegistry.java index 370419e..59f5220 100644 --- a/lib/src/main/java/net/tomatentum/marinara/registry/InteractionCheckRegistry.java +++ b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionCheckRegistry.java @@ -6,19 +6,25 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import org.apache.logging.log4j.Logger; + import io.leangen.geantyref.GenericTypeReflector; import net.tomatentum.marinara.checks.InteractionCheck; +import net.tomatentum.marinara.util.LoggerUtil; public class InteractionCheckRegistry { private List> checks; + private Logger logger = LoggerUtil.getLogger(getClass()); + public InteractionCheckRegistry() { this.checks = new ArrayList<>(); } public void addCheck(InteractionCheck check) { checks.add(check); + logger.info("Registered Check {}", check.getClass().getName()); } public Optional> getCheckFromAnnotation(Type annotation) { diff --git a/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java index d1640d6..c771761 100644 --- a/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java +++ b/lib/src/main/java/net/tomatentum/marinara/registry/InteractionRegistry.java @@ -5,15 +5,19 @@ import java.util.ArrayList; import java.util.List; import java.util.Optional; +import org.apache.logging.log4j.Logger; + import net.tomatentum.marinara.Marinara; import net.tomatentum.marinara.interaction.InteractionHandler; import net.tomatentum.marinara.interaction.InteractionType; import net.tomatentum.marinara.interaction.commands.SlashCommandDefinition; import net.tomatentum.marinara.interaction.commands.ExecutableSlashCommandDefinition; import net.tomatentum.marinara.interaction.methods.SlashCommandInteractionMethod; +import net.tomatentum.marinara.util.LoggerUtil; import net.tomatentum.marinara.interaction.methods.InteractionMethod; public class InteractionRegistry { + private Logger logger = LoggerUtil.getLogger(getClass()); private List interactionMethods; private Marinara marinara; @@ -26,9 +30,12 @@ public class InteractionRegistry { public void addInteractions(InteractionHandler interactionHandler) { for (Method method : interactionHandler.getClass().getMethods()) { InteractionMethod iMethod = InteractionMethod.create(method, interactionHandler, marinara); - if (iMethod != null) + if (iMethod != null) { this.interactionMethods.add(iMethod); + logger.debug("Added {} method from {}", iMethod.getMethod().getName(), interactionHandler.getClass().getSimpleName()); + } } + logger.info("Added all Interactions from {}", interactionHandler.getClass().getSimpleName()); } public void registerCommands() { @@ -46,16 +53,26 @@ public class InteractionRegistry { appDef.get().addExecutableCommand(def); else defs.add(new SlashCommandDefinition(def.applicationCommand()).addExecutableCommand(def)); + + logger.debug("Added Executable Command {}{}{} for registration", + def.applicationCommand().name(), + def.subCommandGroup() == null ? "" : "." + def.subCommandGroup().name(), + def.subCommand() == null ? "" : "." + def.subCommand().name() + ); }); - marinara.getWrapper().registerSlashCommands(defs.toArray(new SlashCommandDefinition[0])); + marinara.getWrapper().registerSlashCommands(defs.toArray(SlashCommandDefinition[]::new)); + logger.info("Registered all SlashCommands"); } public void handle(Object context) { + InteractionType type = marinara.getWrapper().getInteractionType(context.getClass()); + logger.debug("Received {} interaction ", context); interactionMethods.forEach((m) -> { - InteractionType type = marinara.getWrapper().getInteractionType(context.getClass()); - if (m.getType().equals(type) && m.canRun(context)) + if (m.getType().equals(type) && m.canRun(context)) { + logger.info("Running {} interaction using {}\ncontext: {}", type, m.getMethod().toString(), context.toString()); m.run(context); + } }); } } diff --git a/lib/src/main/java/net/tomatentum/marinara/util/LoggerUtil.java b/lib/src/main/java/net/tomatentum/marinara/util/LoggerUtil.java new file mode 100644 index 0000000..bf883db --- /dev/null +++ b/lib/src/main/java/net/tomatentum/marinara/util/LoggerUtil.java @@ -0,0 +1,24 @@ +package net.tomatentum.marinara.util; + +import java.util.Properties; + +import org.apache.logging.log4j.Level; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.simple.SimpleLogger; +import org.apache.logging.log4j.util.PropertiesUtil; +import org.apache.logging.log4j.util.ProviderUtil; + +public class LoggerUtil { + public static Logger getLogger(String name) { + if (ProviderUtil.hasProviders()) { + return LogManager.getLogger(name); + }else + return new SimpleLogger(name, Level.DEBUG, true, false, true, true, "yyyy-MM-dd HH:mm:ss.SSSZ", null, + new PropertiesUtil(new Properties()), System.out); + } + + public static Logger getLogger(Class clazz) { + return getLogger(clazz.getName()); + } +} diff --git a/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java b/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java index 2a65282..9446e8f 100644 --- a/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java +++ b/lib/src/main/java/net/tomatentum/marinara/util/ReflectionUtil.java @@ -100,4 +100,8 @@ public final class ReflectionUtil { } return true; } + + public static String getFullMethodName(Method method) { + return method.getClass().getName() + "." + method.getName(); + } } diff --git a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java index 98988df..ba540e6 100644 --- a/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java +++ b/wrapper/javacord/src/main/java/net/tomatentum/marinara/wrapper/javacord/JavacordWrapper.java @@ -8,6 +8,7 @@ import java.util.List; import java.util.Map; import java.util.Set; +import org.apache.logging.log4j.Logger; import org.javacord.api.DiscordApi; import org.javacord.api.interaction.ApplicationCommandInteraction; import org.javacord.api.interaction.ButtonInteraction; @@ -25,15 +26,19 @@ import net.tomatentum.marinara.interaction.commands.annotation.SlashCommandOptio import net.tomatentum.marinara.interaction.commands.annotation.SubCommand; import net.tomatentum.marinara.interaction.commands.annotation.SubCommandGroup; import net.tomatentum.marinara.interaction.commands.option.SlashCommandOptionType; +import net.tomatentum.marinara.util.LoggerUtil; import net.tomatentum.marinara.wrapper.LibraryWrapper; public class JavacordWrapper extends LibraryWrapper { private DiscordApi api; + private Logger logger = LoggerUtil.getLogger(getClass()); + public JavacordWrapper(DiscordApi api) { this.api = api; api.addInteractionCreateListener((e) -> handleInteraction(e.getInteraction())); + logger.info("Javacord wrapper loaded!"); } @Override @@ -99,7 +104,7 @@ public class JavacordWrapper extends LibraryWrapper { }else builder.setSubCommand(TypeFactory.annotation(SubCommand.class, Map.of("name", options.getFirst().getName()))); } catch (AnnotationFormatException e) { - e.printStackTrace(); + logger.fatal(e); } return builder.build();