高并发系统-使用自定义日志埋点快速排查问题
背景 通用上下文 CommonContext 升级
@Slf4j@Datapublic class CommonContext {// 日志private StringBuffer logSb = new StringBuffer();// 日志开关private boolean isDebug;// 通用参数private boolean compare = false;// 中间结果private Set<Integer> targetSet = new HashSet<>();public void clearContext() {targetSet = Collections.emptySet();compare = false;}public void debug(String message) {if (!isDebug || StringUtils.isEmpty(message)) {return;}logSb.append(message).append("\t\n");}public void debug(String format, Object... argArray) {if (!isDebug) {return;}String[] msgArray = new String[argArray.length];for (int i = 0; i < argArray.length; i++) {msgArray[i] = JSON.toJSONString(argArray[i]);}FormattingTuple ft = MessageFormatter.arrayFormat(format, msgArray);logSb.append(ft.getMessage()).append("\t\n");}public void debugEnd() {if (!isDebug) {return;}String msg = logSb.toString();log.info(msg);}}
@Overridepublic Response method(Request request) {if (checkParam(request)) {log.error("request param error:{}", JSON.toJSONString(request));return Response.failed(ResponseCode.PARAM_INVALID);}CallerInfo info = Profiler.registerInfo(Ump.getUmpKey(xxxx), false, true);ParamVO paramVO = request.getParam();try {CommonContext context = new CommonContext();context.setDebug(Constants.SPECIAL_UUID.equals(request.getUuid()));Long userId = paramVO.getUserId();context.setCompare(paramVO.getCompare());context.debug("输入参数:{}", paramVO);List result = userAppService.match(context, paramVO);context.debug("输出结果:{}", result);context.clearContext();Response response = Response.success(result);context.debugEnd(response);return response;} catch (Exception e) {log.error("method error", e);Profiler.functionError(info);return Response.failed(ResponseCode.ERROR);} finally {Profiler.registerInfoEnd(info);}}
context.debug("activityList:{}", activityList.stream().map(ActivityInfo::toString).collect(Collectors.joining("######")));
org.apache.logging.log4j.Logger#info(java.lang.String,org.apache.logging.log4j.util.Supplier<?>...)
package org.example;import com.alibaba.fastjson.JSON;import lombok.Data;import lombok.extern.slf4j.Slf4j;import org.apache.commons.lang.StringUtils;import org.slf4j.helpers.FormattingTuple;import org.slf4j.helpers.MessageFormatter;import java.util.Collections;import java.util.HashSet;import java.util.Set;import java.util.function.Supplier;@Slf4j@Datapublic class CommonContext {// 日志private StringBuffer logSb = new StringBuffer();// 日志开关private boolean isDebug;// 日志开关是否记录详细日志private boolean isDebugDetail;// 通用参数private boolean compare = false;// 中间结果private Set<Integer> targetSet = new HashSet<>();public void clearContext() {targetSet = Collections.emptySet();compare = false;}public void setDebugDetail(boolean debugDetail) {if (debugDetail) {isDebug = true;}isDebugDetail = debugDetail;}public void debug(String message) {if (!isDebug || StringUtils.isEmpty(message)) {return;}logSb.append(message).append("\t\n");}public void debug(String format, Object... argArray) {if (!isDebug) {return;}String[] msgArray = new String[argArray.length];for (int i = 0; i < argArray.length; i++) {msgArray[i] = JSON.toJSONString(argArray[i]);}FormattingTuple ft = MessageFormatter.arrayFormat(format, msgArray);logSb.append(ft.getMessage()).append("\t\n");}public void debug(String message, Supplier<?>... paramSuppliers) {if (!isDebug) {return;}commonDebug(message, paramSuppliers);}public void debugDetail(String message, Supplier<?>... paramSuppliers) {if (!isDebugDetail) {return;}commonDebug(message, paramSuppliers);}private void commonDebug(String message, Supplier<?>... paramSuppliers) {String[] msgArray = new String[paramSuppliers.length];for (int i = 0; i < paramSuppliers.length; i++) {msgArray[i] = JSON.toJSONString(paramSuppliers[i].get());}FormattingTuple ft = MessageFormatter.arrayFormat(message, msgArray);logSb.append(ft.getMessage()).append("\t\n");}public void debugEnd() {if (!isDebug) {return;}String msg = logSb.toString();log.info(msg);}}
CommonContext context = new CommonContext();context.setDebug(Constants.SPECIAL_UUID.equals(request.getUuid()));context.setDebugDetail(Constants.SPECIAL_UUID2.equals(request.getUuid()));
context.debugDetail("activityList:{}", () -> activityList.stream().map(ActivityInfo::toString).collect(Collectors.joining("######")));
- END -