0

如何使用 JDK8 和 JDK11 拥有相同的 slf4j 日志?

我的 java Slf4j 记录器:

log.info("---> {} {}", "When", String.format(matcher.group(1).replaceAll("\\{\\S+\\}", "{%s}").replace("(\\?)", ""), invocation.getArguments()));

JDK8 在 java 8 中的跟踪:

---> When I update text {bakery.DemoPage-input_text_field} with {Jenkins T5}

JDK11 在 java 8 中的跟踪:

---> When "I update text {bakery.DemoPage-input_text_field} with {Jenkins T5}"

编辑:

我尝试了这个但结果相同:

String message = MessageFormat.format("---> {0} {1}",
                                      stepAnnotation.annotationType().getSimpleName(),
                                      String.format(matcher.group(1).replaceAll("\\{\\S+\\}", "{%s}").replace("(\\?)", ""), invocation.getArguments())
                                     );
log.info(message);

编辑(如果你想要一个更简单的案例):

log.info("---> {} {}", "When", String.format("I update text {%s} with {%s}", "bakery.DemoPage-input_text_field", "Jenkins T5"));

用@M编辑。Deinum 提案但不工作

log.info("---> {} " + matcher.group(1).replaceAll("\\{\\S+\\}", "{}").replace("(\\?)", ""), stepAnnotation.annotationType().getSimpleName(), invocation.getArguments());

---> When "I update text [bakery.DemoPage-input_text_field, Jenkins T5, []] with {}"

编辑:我尝试使用外部替换的其他建议:

String mes = String.format(matcher.group(1).replaceAll("\\{\\S+\\}", "{%s}").replace("(\\?)", ""), invocation.getArguments());
log.info("---> {} {}", stepAnnotation.annotationType().getSimpleName(), mes);

---> When "I update text {bakery.DemoPage-input_text_field} with {Jenkins T5}"
4

1 回答 1

1

问题不是来自 Slf4j 而是来自stepAnnotation.toString()不同的JDK8and JDK11)

openjdk11并且oraclejdk11尊重javadoc:

/**
 * Returns a string representation of this annotation.  The details
 * of the representation are implementation-dependent, but the following
 * may be regarded as typical:
 * <pre>
 *   &#064;com.acme.util.Name(first=Alfred, middle=E., last=Neuman)
 * </pre>
 *
 * @return a string representation of this annotation
 */
String toString();

解决方案:

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import io.cucumber.java.en.When;

public class Sof {

    private static final Logger log = LoggerFactory.getLogger(Sof.class);

    @When(value = "I update text {string} with {string}(\\?)")
    public static void main(String[] args) {
        Object as[] = { "a", "b" };
        Class c = Sof.class;
        Method[] methods = c.getMethods();
        Method method = null;
        for (Method m : methods) {
            if (m.getName().equals("main")) {
                method = m;
            }
        }
        Annotation stepAnnotation = method.getAnnotation(When.class);
        Class<? extends Annotation> annotationClass = stepAnnotation.annotationType();
        try {
            Method valueMethods = annotationClass.getDeclaredMethod("value");
            if (Modifier.isPublic(valueMethods.getModifiers())) {
                log.info("---> {} " + String.format(valueMethods.invoke(stepAnnotation).toString().replaceAll("\\{\\S+\\}", "{%s}").replace("(\\?)", ""), as),
                        stepAnnotation.annotationType().getSimpleName());
            }
        } catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
            e1.printStackTrace();
        }
    }

}
于 2020-03-12T12:04:45.297 回答