gpt4 book ai didi

java - 为什么鱿鱼:S1166 not accept exception messages only when logging caught exceptions?

转载 作者:搜寻专家 更新时间:2023-11-01 02:41:07 28 4
gpt4 key购买 nike

引用规则的描述(SonarQube 4.5.5):

// Noncompliant - exception is lost (only message is preserved)   
try { /* ... */ }
catch (Exception e) { LOGGER.info(e.getMessage()); }

通过向记录器提供异常类,堆栈跟踪将写入日志。

我们代码库中的问题是这样的:通过关注Tell, don't ask原则上,我们使用检查异常作为正常执行路径的一部分,我们不希望它们导致不合理的大日志消息。

几个例子:服务器响应错误代码,数据库语句执行失败乐观锁定(并发用户)......

我的建议:将这个案例一分为二。

// Noncompliant - exception is lost (only message is preserved)
try { /* ... */ }
catch (Exception e) { LOGGER.info(e.getMessage()); }

// Compliant - exception is lost (only message is preserved) but there is business logic handling the situation      

try {
/* ... */
} catch (Exception e) {
LOGGER.info(e.getMessage());
*/ exception handling */
}

规则 squid:S00108(代码块不能为空)不会捕获问题,因为存在日志语句。

这不合理吗?我错过了什么重要的事情吗?

注意:我重写了问题以阐明我的用例

最佳答案

我理解维护堆栈跟踪和所有这些的论点,但我认为它会使您的日志因 < ERROR 级别的事件而膨胀。一种解决方案是将消息记录为 WARN,并将异常对象记录为 DEBUG 或 TRACE。这样,普通用户日志配置就不会像往常一样被堆栈跟踪淹没,但如果需要,仍然可以获得堆栈跟踪。

关于java - 为什么鱿鱼:S1166 not accept exception messages only when logging caught exceptions?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33032194/

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