如何使用 JDK8 和 JDK11(里面有 String.format)拥有相同的 slf4j 日志?

How to have same slf4j log with JDK8 and JDK11 (with String.format inside)?

如何在JDK8和JDK11上拥有相同的slf4j日志?

我的 java Slf4j 记录器:

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

我在 java 8 by JDK8 中的踪迹:

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

问题不是来自 Slf4j,而是来自 stepAnnotation.toString() 不同于 JDK8JDK11)

openjdk11oraclejdk11 不尊重 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();
        }
    }

}