Как получить один и тот же журнал slf4j с JDK8 и JDK11 (с String.format внутри)? - PullRequest
0 голосов
/ 11 марта 2020

Как вести один и тот же журнал slf4j с JDK8 и JDK11?

My java Slf4j logger:

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

Мой след в java 8 JDK8:

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

Мой след в java 8 от JDK11:

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

1 Ответ

0 голосов
/ 12 марта 2020

проблема не в Slf4j, а в stepAnnotation.toString(), отличном от JDK8 и JDK11)

openjdk11 и oraclejdk11 до не уважают javado c:

<code>/**
 * 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)
 * 
* * @ вернуть строковое представление этой аннотации * / 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();
        }
    }

}
...