如何在静态方法中使用log.info

评论:

我的功能看起来像:

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?

rzwitserloot:
  1. 只需将您的日志字段设为静态即可。无论如何,这是行业标准。
  2. 那是令人遗憾的错误处理。如果您的反射调用出了点问题,则此方法返回1.0。这似乎是非常愚蠢的行为。通常,如果无法采取任何措施来正确处理它,就应该抛出异常(并且“记录并返回一些丢弃的默认值”很少是有效的“处理”策略!),因为这样做会导致较长的过程异常或直接执行的代码做错了事情,因为正如他们所说的那样,垃圾回收,垃圾回收以及面对定义问题时返回1.0肯定听起来像1.0就是垃圾,不是吗?
  3. 如果您的意图是完全忽略所有含义,将其分流到日志并返回默认值,则可以捕获Exception。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章