{} {}", "When", String.format(mat-6ren">
gpt4 book ai didi

java - 如何与 JDK8 和 JDK11 具有相同的 slf4j 日志(内部带有 String.format)?

转载 作者:行者123 更新时间:2023-12-01 18:00:38 25 4
gpt4 key购买 nike

如何让 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}"

最佳答案

问题不是来自 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();
}
}

}

关于java - 如何与 JDK8 和 JDK11 具有相同的 slf4j 日志(内部带有 String.format)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60638844/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com