利用枚举类完善日志记录

一、前言

很多场景下我们需要在后端对接大量接口,因此完整且详细记录接口的调用的日志信息便于后期问题排查和记录。

二、方案

1、我们需要清楚调用的哪个接口,并规定标识。

String title = "dog";

2、可以创建一个枚举类和其对应的工具类。

枚举类代码:

@Getter
@RequiredArgsConstructor
public enum AnimalInterFaceEnum {
	animal("animal","动物接口)"),
	
	dog("dog","狗狗接口"),
	
	cat("cat","喵星人接口"),
	
	shark("shark","鲨鱼接口"),
	
	orangutan("orangutan","猩猩接口"),
	
	banbana("banbana","香蕉接口"),
	
	monkey("monkey","猴子接口");

	private static final Map<String, String> interfaceMap = new HashMap<>();

	static {
		// 初始化枚举值时,将键值对存入Map
		for (AnimalInterFaceEnum code : AnimalInterFaceEnum.values()) {
			interfaceMap.put(code.getCode(), code.getDescription());
		}
	}

	private String code;
	private String description;

	AnimalInterFaceEnum(String code, String description) {
		this.code = code;
		this.description = description;
	}

	public String getCode() {
		return code;
	}

	public String getDescription() {
		return description;
	}

	public static Map<String, String> getInterfaceMap() {
		return interfaceMap;
	}
}

 要求:要求枚举和code值一致。

AnimalInterFaceEnum类在项目初始化时即会加载,便于工具类进行调用。

3、枚举类工具类:

public class AnimalEnumUtil {
	public static String getDescriptionByCode(String code) {
		Map<String, String> dictMap = AnimalInterFaceEnum.getInterfaceMap();
		return dictMap.get(code);
	}
}

三、应用

String title = "dog";
String description = AnimalEnumUtil.getDescriptionByCode(title);
log.info("==============开始获取{}接口数据====================",description);
//发送http或https请求
log.info("{}接口共获取{}条记录",description,total);
//接收到数据分析并保存
log.info("==============获取{}接口数据成功====================",description);

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值