gpt4 book ai didi

java - 如何在静态方法中拥有log.info

转载 作者:行者123 更新时间:2023-12-01 16:30:06 27 4
gpt4 key购买 nike

我的函数如下所示:

public static double isOverturn(final String reference, final String hypothesis, FieldType fieldType) {
double overturnScore = 1.0;
if (StringUtils.isEmpty(reference) || StringUtils.isEmpty(hypothesis))
return overturnScore;
Method comparisonMethod = null;
try {
comparisonMethod = comparison(fieldType.getName());
overturnScore = (double) comparisonMethod.invoke(null, reference, hypothesis);
} catch (NoSuchMethodException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
}
return overturnScore;
}

我想用 log.info 代替 e.printStackTrace。但这给了我错误。如何在静态方法中使用log.info?

最佳答案

  1. 只需将日志字段设置为静态即可。无论如何,这是行业标准。
  2. 这是令人遗憾的错误处理。如果反射调用出现问题,此方法将返回 1.0。这看起来是非常愚蠢的行为。一般来说,如果您无法正确处理它,您应该抛出异常(并且“记录它并返回一些一次性默认值”很少是有效的“处理它”策略!),因为您所做的会导致一条长链异常,或者直接做错误事情的代码,因为正如他们所说,垃圾输入,垃圾输出,并在面对定义问题时返回 1.0 听起来确实像 1.0 只是垃圾,不是吗?
  3. 如果您的目的是完全忽略异常的含义,将其分流到日志并返回默认值,则可以捕获异常。

关于java - 如何在静态方法中拥有log.info,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62064124/

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