From eccad71837ed4971afac9d0ff1ccb0fa44894e23 Mon Sep 17 00:00:00 2001 From: Tueem Date: Mon, 14 Apr 2025 18:34:46 +0200 Subject: [PATCH] feat(logging): add more logging --- gradle/libs.versions.toml | 1 + lib/build.gradle.kts | 2 ++ .../cutin/ReflectedMethodFactoryImpl.java | 8 +++++--- .../cutin/container/LoneMethodContainer.java | 6 ++++++ .../cutin/container/MultiMethodContainer.java | 15 ++------------- .../cutin/method/BestCandidateMethod.java | 6 ++++++ .../tomatentum/cutin/method/ReflectedMethod.java | 7 ++++--- .../net/tomatentum/cutin/util/ReflectionUtil.java | 3 ++- lib/src/test/resources/simplelogger.properties | 7 +++++++ 9 files changed, 35 insertions(+), 20 deletions(-) create mode 100644 lib/src/test/resources/simplelogger.properties diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index f7efc2c..89096c6 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -8,3 +8,4 @@ slf4j = "2.0.17" [libraries] junit-jupiter = { module = "org.junit.jupiter:junit-jupiter", version.ref = "junit-jupiter" } slf4j = { module = "org.slf4j:slf4j-api", version.ref = "slf4j"} +slf4j-simple = { module = "org.slf4j:slf4j-simple", version.ref = "slf4j"} diff --git a/lib/build.gradle.kts b/lib/build.gradle.kts index 79d1f8d..7ed3e92 100644 --- a/lib/build.gradle.kts +++ b/lib/build.gradle.kts @@ -20,6 +20,8 @@ dependencies { testImplementation(libs.junit.jupiter) testRuntimeOnly("org.junit.platform:junit-platform-launcher") + testImplementation(libs.slf4j.simple) + implementation(libs.slf4j) } diff --git a/lib/src/main/java/net/tomatentum/cutin/ReflectedMethodFactoryImpl.java b/lib/src/main/java/net/tomatentum/cutin/ReflectedMethodFactoryImpl.java index 94e20d3..fab8f54 100644 --- a/lib/src/main/java/net/tomatentum/cutin/ReflectedMethodFactoryImpl.java +++ b/lib/src/main/java/net/tomatentum/cutin/ReflectedMethodFactoryImpl.java @@ -34,9 +34,10 @@ public class ReflectedMethodFactoryImpl impl .map(Optional::get) .findFirst(); - if (rmethod.isEmpty()) { - logger.debug("Could not produce a ReflectedMethod for Method {}", ReflectionUtil.getFullMethodName(method)); - } + if (rmethod.isEmpty()) + logger.warn("Could not produce a ReflectedMethod for Method {} in ", ReflectionUtil.getFullMethodName(method), this); + else + logger.debug("Produced {} for Method {} in {}", rmethod.get(), ReflectionUtil.getFullMethodName(method), this); return rmethod; } @@ -44,6 +45,7 @@ public class ReflectedMethodFactoryImpl impl @Override public ReflectedMethodFactory addFactory(Factory factory) { this.factories.add(factory); + logger.trace("Added Factory {} to {}", factory, this); return this; } diff --git a/lib/src/main/java/net/tomatentum/cutin/container/LoneMethodContainer.java b/lib/src/main/java/net/tomatentum/cutin/container/LoneMethodContainer.java index c258d4b..b0807db 100644 --- a/lib/src/main/java/net/tomatentum/cutin/container/LoneMethodContainer.java +++ b/lib/src/main/java/net/tomatentum/cutin/container/LoneMethodContainer.java @@ -9,11 +9,16 @@ import java.util.Map; import java.util.Optional; import java.util.Set; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.tomatentum.cutin.ReflectedMethodFactory; import net.tomatentum.cutin.method.ReflectedMethod; public class LoneMethodContainer implements MethodContainer { + private Logger logger = LoggerFactory.getLogger(getClass()); + private Map> methodStore; private ReflectedMethodFactory factory; @@ -25,6 +30,7 @@ public class LoneMethodContainer implements @Override public MethodContainer addMethod(ReflectedMethod method) { this.methodStore.put(method.identifier(), method); + logger.debug("Added {} to container", method); return this; } diff --git a/lib/src/main/java/net/tomatentum/cutin/container/MultiMethodContainer.java b/lib/src/main/java/net/tomatentum/cutin/container/MultiMethodContainer.java index 9a5b20a..ce7cadb 100644 --- a/lib/src/main/java/net/tomatentum/cutin/container/MultiMethodContainer.java +++ b/lib/src/main/java/net/tomatentum/cutin/container/MultiMethodContainer.java @@ -95,21 +95,10 @@ public class MultiMethodContainer implements throw new IllegalArgumentException("Method's identifier did not equal the entry's identifier"); this.methods.add(method); - logger.debug("Added method {} to entry {}", method.method().getName(), this); + logger.debug("Added method {} to entry {}", ReflectionUtil.getFullMethodName(method.method()), this); return this; } - public Object[] runAll(C context) { - logger.trace("Running all Methods from {} with context {}", this, context); - return this.methods.stream() - .map(x -> { - logger.debug("Running Method {} from {} with context {}", x, this, context); - return x.run(context); - }) - .flatMap(ReflectionUtil::getReturnAsStream) - .toArray(); - } - @Override public boolean equals(Object obj) { if (!(obj instanceof Entry)) @@ -125,7 +114,7 @@ public class MultiMethodContainer implements @Override public String toString() { - return "Content(%s)".formatted(identifier().toString()); + return "Ident(%s)".formatted(identifier().toString()); } } diff --git a/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java b/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java index 4919782..084bce2 100644 --- a/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java +++ b/lib/src/main/java/net/tomatentum/cutin/method/BestCandidateMethod.java @@ -7,12 +7,17 @@ import java.util.List; import java.util.Objects; import java.util.Optional; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + import net.tomatentum.cutin.ReflectedMethodFactory; import net.tomatentum.cutin.container.MethodContainer; import net.tomatentum.cutin.util.ReflectionUtil; public abstract class BestCandidateMethod extends ReflectedMethod { + private Logger logger = LoggerFactory.getLogger(getClass()); + private String methodName; protected BestCandidateMethod(String methodName, Object containingObject) { @@ -30,6 +35,7 @@ public abstract class BestCandidateMethod ex .map(Object::getClass) .toArray(Class[]::new); super.method = ReflectionUtil.getMostSpecificMethod(methods, parameters); + logger.trace("Found {} for {}({}) in {}", ReflectionUtil.getFullMethodName(method()), this.methodName, String.join(", ", Arrays.stream(parameters).map(Object::toString).toList()), this); return super.run(context); } diff --git a/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java b/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java index e86b38f..cf57f79 100644 --- a/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java +++ b/lib/src/main/java/net/tomatentum/cutin/method/ReflectedMethod.java @@ -33,9 +33,10 @@ public abstract class ReflectedMethod { public Object run(C context) { method.setAccessible(true); try { + logger.debug("Invoking method {} from {}", ReflectionUtil.getFullMethodName(method), this); return method.invoke(containingObject, getParameters(context)); }catch (IllegalAccessException | InvocationTargetException ex) { - logger.error("ReflectedMethod failed to run", ex); + logger.error("ReflectedMethod %s failed to run".formatted(this), ex); return null; } } @@ -50,7 +51,7 @@ public abstract class ReflectedMethod { @Override public String toString() { - return identifier().toString(); + return "ReflectedMethod(%s)".formatted(identifier()); } private Object[] getParameters(C context) { @@ -59,7 +60,7 @@ public abstract class ReflectedMethod { for (int i = 0; i < parameterCount; i++) { Object parameter = getParameter(context, i); - logger.trace("Found parameter {}={} for method {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method)); + logger.trace("Found parameter {}={} for method {} in {}", parameter != null ? parameter.getClass().toString() : " ", parameter, ReflectionUtil.getFullMethodName(method), this); parameters.add(parameter); } return parameters.toArray(); diff --git a/lib/src/main/java/net/tomatentum/cutin/util/ReflectionUtil.java b/lib/src/main/java/net/tomatentum/cutin/util/ReflectionUtil.java index ae86982..ad8441c 100644 --- a/lib/src/main/java/net/tomatentum/cutin/util/ReflectionUtil.java +++ b/lib/src/main/java/net/tomatentum/cutin/util/ReflectionUtil.java @@ -135,6 +135,7 @@ public final class ReflectionUtil { } public static String getFullMethodName(Method method) { - return method.getDeclaringClass().getName() + "." + method.getName(); + List parameters = Arrays.stream(method.getParameterTypes()).map(Object::toString).toList(); + return String.format("%s.%s(%s)", method.getDeclaringClass().getName(), method.getName(), String.join(", ", parameters)); } } diff --git a/lib/src/test/resources/simplelogger.properties b/lib/src/test/resources/simplelogger.properties new file mode 100644 index 0000000..a25b6c7 --- /dev/null +++ b/lib/src/test/resources/simplelogger.properties @@ -0,0 +1,7 @@ +# SLF4J's SimpleLogger configuration file +# Simple implementation of Logger that sends all enabled log messages, for all defined loggers, to System.err. + +# Default logging detail level for all instances of SimpleLogger. +# Must be one of ("trace", "debug", "info", "warn", or "error"). +# If not specified, defaults to "info". +org.slf4j.simpleLogger.defaultLogLevel=trace \ No newline at end of file