feat(logging): add more logging
All checks were successful
Build / Gradle-Build (push) Successful in 13s
Publish / Gradle-Publish (push) Successful in 10s
Test / Gradle-Test (push) Successful in 11s

This commit is contained in:
Tueem 2025-04-14 18:34:46 +02:00
parent cad019e44a
commit eccad71837
Signed by: tueem
GPG Key ID: F2CE0513D231AD7A
9 changed files with 35 additions and 20 deletions

View File

@ -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"}

View File

@ -20,6 +20,8 @@ dependencies {
testImplementation(libs.junit.jupiter)
testRuntimeOnly("org.junit.platform:junit-platform-launcher")
testImplementation(libs.slf4j.simple)
implementation(libs.slf4j)
}

View File

@ -34,9 +34,10 @@ public class ReflectedMethodFactoryImpl<I extends Object, C extends Object> 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<I extends Object, C extends Object> impl
@Override
public ReflectedMethodFactory<I, C> addFactory(Factory<I, C> factory) {
this.factories.add(factory);
logger.trace("Added Factory {} to {}", factory, this);
return this;
}

View File

@ -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<I extends Object, C extends Object> implements MethodContainer<I, C> {
private Logger logger = LoggerFactory.getLogger(getClass());
private Map<I, ReflectedMethod<I, C>> methodStore;
private ReflectedMethodFactory<I, C> factory;
@ -25,6 +30,7 @@ public class LoneMethodContainer<I extends Object, C extends Object> implements
@Override
public MethodContainer<I, C> addMethod(ReflectedMethod<I, C> method) {
this.methodStore.put(method.identifier(), method);
logger.debug("Added {} to container", method);
return this;
}

View File

@ -95,21 +95,10 @@ public class MultiMethodContainer<I extends Object, C extends Object> 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<I extends Object, C extends Object> implements
@Override
public String toString() {
return "Content(%s)".formatted(identifier().toString());
return "Ident(%s)".formatted(identifier().toString());
}
}

View File

@ -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<I extends Object, C extends Object> extends ReflectedMethod<I, C> {
private Logger logger = LoggerFactory.getLogger(getClass());
private String methodName;
protected BestCandidateMethod(String methodName, Object containingObject) {
@ -30,6 +35,7 @@ public abstract class BestCandidateMethod<I extends Object, C extends Object> 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);
}

View File

@ -33,9 +33,10 @@ public abstract class ReflectedMethod<I extends Object, C extends Object> {
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<I extends Object, C extends Object> {
@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<I extends Object, C extends Object> {
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();

View File

@ -135,6 +135,7 @@ public final class ReflectionUtil {
}
public static String getFullMethodName(Method method) {
return method.getDeclaringClass().getName() + "." + method.getName();
List<String> parameters = Arrays.stream(method.getParameterTypes()).map(Object::toString).toList();
return String.format("%s.%s(%s)", method.getDeclaringClass().getName(), method.getName(), String.join(", ", parameters));
}
}

View File

@ -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