zone 要支持什么 storage type 才是正确的? Do Not Let Me Think !错误日志应该做到:即使离开代码情境,也能清晰地描述发生了什么。此外,如果能够直接在错误日志中说明清楚原因, 在做巡检日志的时候也可以省些力气。从某种意义上来说, 错误日志也可以是一种非常有益的文档,记录着各种不合法的运行用例。目前程序错误日志的内容可能存在如下问题:1. 错误日志没有指明错误参数和内容:
catch(Exception ex){ log.error("control ip insert failed", ex); return new ResultSet<AddControlIpResponse>( ControlIpErrorCode.ERROR_CONTROL_IP_INSERT_FAILURE); }
没有指明插入失败的 control ip. 如果加上 control ip 关键字, 更容易搜索和锁定错误。类似的还有:
log.error("Get some errors when insert subnet and its IPs into database. Add subnet or IP failure.", e);
没有指明是哪个 subnet 的它下属的哪些 IP. 值得注意的是, 要指明这些要额外做一些事情, 可能会稍微影响性能。这时候需要权衡性能和可调试性。解决方案:使用 String.format("Some msg to ErrorObj: %s", errobj) 方法指明错误参数及内容。这通常要求对 DO 对象编写可读的 toString 方法。2. 错误场景不明确:log.error("nc has exist, nc ip" + request.getIp());在 createNc 中检测到 NC 已经存在报错。但是日志上没有指明错误场景, 让人猜测,为什么会报NC已存在错误。可以改为
log.error("nc has exist when want to create nc, please check nc parameters. Given nc ip: " + request.getIp());
log.error("[create nc] nc has exist, please check nc parameters. Given nc ip: " + request.getIp());
类似的还有:
log.error("not all vm destroyed, nc id " + request.getNcId());
改成
log.error("[delete nc] some vms [%s] in the nc are not destroyed. nc id: %s", vmNames, request.getNcId());
解决方案:错误消息加上 when 字句, 或者错误消息前加上 【接口名】, 指明错误场景,直接从错误日志就知道明白了。一般能够知道 executor 的可以加上 【接口名】, service 加上 when 字句。3. 内容不明确, 或不明其义:
if (!ncResourceService.isNcResourceEnough(ncResourceDO, vmResourceCondition)) { log.error("disk space is not enough at vm's nc, nc id:" + vmDO.getNcId()); throw new BizException(ResourceErrorCode.ERROR_RESOURCE_NOT_ENOUGH); }
log.warn("cache status conflict, device id "+deviceDO.getId()+" db status "+deviceDO.getStatus() +", nc status "+ status);
改为:
log.warn(String.format("[query cache status] device cache status conflicts between regiondb and nc, status of device '%s' in regiondb is %s , but is %s in nc.", deviceDO.getId(), deviceDO.getStatus(), status));
解决方案:改为自然可读的英文句式。总结起来, 错误日志格式可以为:
log.error("[接口名或操作名] [Some Error Msg] happens. [params] [Probably Because]. [Probably need to do].");
log.error(String.format("[接口名或操作名] [Some Error Msg] happens. [%s]. [Probably Because]. [Probably need to do].", params));
或
log.error("[Some Error Msg] happens to 错误参数或内容 when [in some condition]. [Probably Because]. [Probably need to do].");
log.error(String.format("[Some Error Msg] happens to %s when [in some condition]. [Probably Because]. [Probably need to do].", parameters));