鍋を振るのに便利なログセクションを書きました!

最近、プロジェクトは共同デバッグ段階に入りました。サービスレイヤーのインターフェイスはプロトコルレイヤーと対話する必要があります。プロトコルレイヤーは、入力パラメーター[jsonstring]をサービスレイヤーに必要なjson文字列にアセンブルする必要があります。組み立てプロセス中にエラーが発生しやすくなります。

共同デバッグでは、入力パラメータエラーによるインターフェイスデバッグの失敗の問題が何度も発生したため、入力パラメータ情報を出力するリクエストログアスペクトを記述し、同時にサービス層インターフェイス名と呼ばれるプロトコル層とまた、ログセクションから、上位層が呼び出しを開始したかどうかを知ることができます。これは、フロントエンドとバックエンドがポットを投げて証拠を出すのに便利です。

 

前に書く

この記事は実用的であり、アスペクトの原則を説明しませんが、アスペクトの知識ポイントを簡単に紹介するだけです。

前書き

アスペクト指向プログラミングは、OOPオブジェクト指向プログラミングの補足としてのプログラミングパラダイムであり、トランザクション管理権限制御キャッシュ制御ログ印刷、およびなど、システム内のさまざまなモジュールに分散された横断的関心事に対処するために使用されます。など。

AOPは、ソフトウェアの機能モジュールを2つの部分に分けます。コアの関心事と横断的関心事です。ビジネス処理の主な機能はコアフォーカスですが、非コアで拡張が必要な​​機能は横断的なフォーカスです。AOPの機能は、システム内のさまざまな懸念事項を分離し、主要な懸念事項を横断的関心事から分離することです。アスペクトの使用には、次の利点があります。

  • 特定の焦点/分野横断的なロジックに集中する

  • 興味のあるポイントを簡単に追加/削除できます

  • 煩わしさが少なく、コードの可読性と保守性が向上しているため、リクエストログを印刷する場合、アスペクトを考えて制御層コード0に侵入するのは簡単です。

アスペクトの使用[注釈に基づく]

  • @ Aspect =>このクラスをアノテーションクラスとして宣言する

カットポイント注釈:

  • @ Pointcut =>コードを単純化するためにポイントカットを定義する

注意事項:

  • @ Before =>ポイントカットの前にコードを実行する

  • @ After =>ポイントカット後にコードを実行する

  • @AfterReturning =>ポイントカットがコンテンツを返した後にコードが実行され、ポイントカットの戻り値をカプセル化できます

  • @ AfterThrowing =>ポイントカットが例外をスローした後に実行する

  • @Around =>サラウンド、カットポイントの前後でコードを実行

リクエストログの側面を書く

  • @Pointcutを使用してカットポイントを定義します

@Pointcut("execution(* your_package.controller..*(..))")  
public void requestServer() {  
}  
 

@Pointcutは、ログカットエッジを要求しているため、ポイントカットを定義します。したがって、ポイントカットは、Controllerパッケージのすべてのクラスのメソッドを定義します。ポイントカットを定義した後、通知アノテーションでrequestServerメソッド名を直接使用できます。

  • @Beforeを使用して、カットポイントの前に実行します

@Before("requestServer()")  
public void doBefore(JoinPoint joinPoint) {  
 ServletRequestAttributes attributes = (ServletRequestAttributes)  
RequestContextHolder.getRequestAttributes();  
 HttpServletRequest request = attributes.getRequest();  
  
 LOGGER.info("===============================Start========================");  
 LOGGER.info("IP                 : {}", request.getRemoteAddr());  
 LOGGER.info("URL                : {}", request.getRequestURL().toString());  
 LOGGER.info("HTTP Method        : {}", request.getMethod());  
 LOGGER.info("Class Method       : {}.{}", joinPoint.getSignature().getDeclaringTypeName(), joinPoint.getSignature().getName());  
}  
 

Controllerメソッドを入力する前に、呼び出し元のIP、要求URL、HTTP要求タイプ、および呼び出されたメソッドの名前を出力します

  • @Aroundを使用して、コントロールレイヤーに入る入力パラメーターを出力します

@Around("requestServer()")  
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {  
 long start = System.currentTimeMillis();  
 Object result = proceedingJoinPoint.proceed();  
 LOGGER.info("Request Params       : {}", getRequestParams(proceedingJoinPoint));  
 LOGGER.info("Result               : {}", result);  
 LOGGER.info("Time Cost            : {} ms", System.currentTimeMillis() - start);  
  
 return result;  
}  
 

入力パラメータ、結果、および時間がかかることが印刷されます

  • getRquestParamsメソッド

private Map<String, Object> getRequestParams(ProceedingJoinPoint proceedingJoinPoint) {  
  Map<String, Object> requestParams = new HashMap<>();  
  
   //参数名  
  String[] paramNames = ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();  
  //参数值  
  Object[] paramValues = proceedingJoinPoint.getArgs();  
  
  for (int i = 0; i < paramNames.length; i++) {  
  Object value = paramValues[i];  
  
  //如果是文件对象  
  if (value instanceof MultipartFile) {  
  MultipartFile file = (MultipartFile) value;  
  value = file.getOriginalFilename();  //获取文件名  
  }  
  
  requestParams.put(paramNames[i], value);  
  }  
  
  return requestParams;  
 }  
 

@PathVariableアノテーションと@RequestParamアノテーションを介して渡されたパラメーターはパラメーター名を出力できないため、パラメーター名を手動でスプライスする必要があります。同時に、ファイルオブジェクトに対して特別な処理が実行され、ファイル名を取得するだけです。

  • @Afterメソッドが呼び出された後に実行する

@After("requestServer()")  
public void doAfter(JoinPoint joinPoint) {  
 LOGGER.info("===============================End========================");  
}  
 

ビジネスロジックはありませんが、Endが出力されます

  • 完全なセクションコード

@Component  
@Aspect  
public class RequestLogAspect {  
 private final static Logger LOGGER = LoggerFactory.getLogger(RequestLogAspect.class);  
  
 @Pointcut("execution(* your_package.controller..*(..))")  
 public void requestServer() {  
 }  
  
 @Before("requestServer()")  
 public void doBefore(JoinPoint joinPoint) {  
 ServletRequestAttributes attributes = (ServletRequestAttributes)  
RequestContextHolder.getRequestAttributes();  
 HttpServletRequest request = attributes.getRequest();  
  
 LOGGER.info("===============================Start========================");  
 LOGGER.info("IP                 : {}", request.getRemoteAddr());  
 LOGGER.info("URL                : {}", request.getRequestURL().toString());  
 LOGGER.info("HTTP Method        : {}", request.getMethod());  
 LOGGER.info("Class Method       : {}.{}", joinPoint.getSignature().getDeclaringTypeName(),  
 joinPoint.getSignature().getName());  
 }  
  
  
 @Around("requestServer()")  
 public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {  
 long start = System.currentTimeMillis();  
 Object result = proceedingJoinPoint.proceed();  
 LOGGER.info("Request Params     : {}", getRequestParams(proceedingJoinPoint));  
 LOGGER.info("Result               : {}", result);  
 LOGGER.info("Time Cost            : {} ms", System.currentTimeMillis() - start);  
  
 return result;  
 }  
  
 @After("requestServer()")  
 public void doAfter(JoinPoint joinPoint) {  
 LOGGER.info("===============================End========================");  
 }  
  
 /**  
  * 获取入参  
  * @param proceedingJoinPoint  
  *  
  * @return  
  * */  
 private Map<String, Object> getRequestParams(ProceedingJoinPoint proceedingJoinPoint) {  
 Map<String, Object> requestParams = new HashMap<>();  
  
 //参数名  
 String[] paramNames =  
((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();  
 //参数值  
 Object[] paramValues = proceedingJoinPoint.getArgs();  
  
 for (int i = 0; i < paramNames.length; i++) {  
 Object value = paramValues[i];  
  
 //如果是文件对象  
 if (value instanceof MultipartFile) {  
 MultipartFile file = (MultipartFile) value;  
 value = file.getOriginalFilename();  //获取文件名  
 }  
  
 requestParams.put(paramNames[i], value);  
 }  
  
 return requestParams;  
 }  
}  
 

同時実行性の高いリクエストログの側面

書いた後は自分のコードにとても満足していましたが、何か改善できるかもしれないと思ったので、友達とコミュニケーションを取りました。えーと

確かに、各メッセージを最適化して1行を印刷する場所はまだあります。同時実行性の高いリクエストでは、テストフェーズのリクエスト数が少ないため、リクエスト間でログを連続して印刷するという問題が発生します。シリアルの状況ではありません。確かに、本番環境はただです。これは最初の開発力であり、より多くのバグに遭遇し、ログのシリアルの問題を解決するためのより堅牢なコードを記述し、印刷情報の複数の行を1行にマージするだけなので、オブジェクト

  • RequestInfo.java

@Data  
public class RequestInfo {  
 private String ip;  
 private String url;  
 private String httpMethod;  
 private String classMethod;  
 private Object requestParams;  
 private Object result;  
 private Long timeCost;  
}  
 
  • サラウンド通知方式本体

@Around("requestServer()")  
public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {  
 long start = System.currentTimeMillis();  
 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  
 HttpServletRequest request = attributes.getRequest();  
 Object result = proceedingJoinPoint.proceed();  
 RequestInfo requestInfo = new RequestInfo();  
 requestInfo.setIp(request.getRemoteAddr());  
 requestInfo.setUrl(request.getRequestURL().toString());  
 requestInfo.setHttpMethod(request.getMethod());  
 requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(),  
 proceedingJoinPoint.getSignature().getName()));  
 requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint));  
 requestInfo.setResult(result);  
 requestInfo.setTimeCost(System.currentTimeMillis() - start);  
 LOGGER.info("Request Info      : {}", JSON.toJSONString(requestInfo));  
  
 return result;  
}  
 

URLおよびhttpリクエスト情報をRequestInfoオブジェクトにアセンブルし、オブジェクトをシリアル化して印刷します。オブジェクトを直接印刷する代わりに
シリアル化されたオブジェクトの結果を印刷するのは、シリアル化がより直感的で明確であり、オンライン分析を使用して結果を解析できるためです。ツール

悪くないですか

同時実行性の高いリクエストのシリアル化の問題を解決しながら、例外リクエスト情報の出力を追加し、@ AfterThrowingアノテーションを使用して例外をスローする方法を処理します。

  • RequestErrorInfo.java

@Data  
public class RequestErrorInfo {  
 private String ip;  
 private String url;  
 private String httpMethod;  
 private String classMethod;  
 private Object requestParams;  
 private RuntimeException exception;  
}  
 
  • 異常な通知ラッパー

@AfterThrowing(pointcut = "requestServer()", throwing = "e")  
public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) {  
 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  
 HttpServletRequest request = attributes.getRequest();  
 RequestErrorInfo requestErrorInfo = new RequestErrorInfo();  
 requestErrorInfo.setIp(request.getRemoteAddr());  
 requestErrorInfo.setUrl(request.getRequestURL().toString());  
 requestErrorInfo.setHttpMethod(request.getMethod());  
 requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),  
 joinPoint.getSignature().getName()));  
 requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint));  
 requestErrorInfo.setException(e);  
 LOGGER.info("Error Request Info      : {}", JSON.toJSONString(requestErrorInfo));  
}  
 

例外として、時間のかかることは無意味なので、時間のかかることはカウントされませんが、異常な印刷が追加されます

最後に、完全なログリクエストアスペクトコードを入力しましょう。

@Component  
@Aspect  
public class RequestLogAspect {  
    private final static Logger LOGGER = LoggerFactory.getLogger(RequestLogAspect.class);  
  
    @Pointcut("execution(* your_package.controller..*(..))")  
    public void requestServer() {  
    }  
  
    @Around("requestServer()")  
    public Object doAround(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {  
        long start = System.currentTimeMillis();  
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  
        HttpServletRequest request = attributes.getRequest();  
        Object result = proceedingJoinPoint.proceed();  
        RequestInfo requestInfo = new RequestInfo();  
                requestInfo.setIp(request.getRemoteAddr());  
        requestInfo.setUrl(request.getRequestURL().toString());  
        requestInfo.setHttpMethod(request.getMethod());  
        requestInfo.setClassMethod(String.format("%s.%s", proceedingJoinPoint.getSignature().getDeclaringTypeName(),  
                proceedingJoinPoint.getSignature().getName()));  
        requestInfo.setRequestParams(getRequestParamsByProceedingJoinPoint(proceedingJoinPoint));  
        requestInfo.setResult(result);  
        requestInfo.setTimeCost(System.currentTimeMillis() - start);  
        LOGGER.info("Request Info      : {}", JSON.toJSONString(requestInfo));  
  
        return result;  
    }  
  
  
    @AfterThrowing(pointcut = "requestServer()", throwing = "e")  
    public void doAfterThrow(JoinPoint joinPoint, RuntimeException e) {  
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();  
        HttpServletRequest request = attributes.getRequest();  
        RequestErrorInfo requestErrorInfo = new RequestErrorInfo();  
        requestErrorInfo.setIp(request.getRemoteAddr());  
        requestErrorInfo.setUrl(request.getRequestURL().toString());  
        requestErrorInfo.setHttpMethod(request.getMethod());  
        requestErrorInfo.setClassMethod(String.format("%s.%s", joinPoint.getSignature().getDeclaringTypeName(),  
                joinPoint.getSignature().getName()));  
        requestErrorInfo.setRequestParams(getRequestParamsByJoinPoint(joinPoint));  
        requestErrorInfo.setException(e);  
        LOGGER.info("Error Request Info      : {}", JSON.toJSONString(requestErrorInfo));  
    }  
  
    /**  
     * 获取入参  
     * @param proceedingJoinPoint  
     *  
     * @return  
     * */  
    private Map<String, Object> getRequestParamsByProceedingJoinPoint(ProceedingJoinPoint proceedingJoinPoint) {  
        //参数名  
        String[] paramNames = ((MethodSignature)proceedingJoinPoint.getSignature()).getParameterNames();  
        //参数值  
        Object[] paramValues = proceedingJoinPoint.getArgs();  
  
        return buildRequestParam(paramNames, paramValues);  
    }  
  
    private Map<String, Object> getRequestParamsByJoinPoint(JoinPoint joinPoint) {  
        //参数名  
        String[] paramNames = ((MethodSignature)joinPoint.getSignature()).getParameterNames();  
        //参数值  
        Object[] paramValues = joinPoint.getArgs();  
  
        return buildRequestParam(paramNames, paramValues);  
    }  
  
    private Map<String, Object> buildRequestParam(String[] paramNames, Object[] paramValues) {  
        Map<String, Object> requestParams = new HashMap<>();  
        for (int i = 0; i < paramNames.length; i++) {  
            Object value = paramValues[i];  
  
            //如果是文件对象  
            if (value instanceof MultipartFile) {  
                MultipartFile file = (MultipartFile) value;  
                value = file.getOriginalFilename();  //获取文件名  
            }  
  
            requestParams.put(paramNames[i], value);  
        }  
  
        return requestParams;  
    }  
  
    @Data  
    public class RequestInfo {  
        private String ip;  
        private String url;  
        private String httpMethod;  
        private String classMethod;  
        private Object requestParams;  
        private Object result;  
        private Long timeCost;  
    }  
  
    @Data  
    public class RequestErrorInfo {  
        private String ip;  
        private String url;  
        private String httpMethod;  
        private String classMethod;  
        private Object requestParams;  
        private RuntimeException exception;  
    }  
}  
 

急いでアプリケーションに追加します[追加されていない場合]。ログがない場合は、常に上位層が間違っていると疑われますが、証拠を示すことはできません。

traceIdの追跡とポジショニングに関しては、traceIdに従ってコールチェーン全体をトレースできます。例としてlog4j2を取り上げて、traceIdを追加する方法を紹介します。

  • インターセプターを追加する

public class LogInterceptor implements HandlerInterceptor {  
 private final static String TRACE_ID = "traceId";  
  
 @Override  
 public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {  
 String traceId = java.util.UUID.randomUUID().toString().replaceAll("-", "").toUpperCase();  
 ThreadContext.put("traceId", traceId);  
  
 return true;  
 }  
  
 @Override  
 public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)  
 throws Exception {  
 }  
  
 @Override  
 public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)  
 throws Exception {  
 ThreadContext. remove(TRACE_ID);  
 }  
}  
 

呼び出す前にThreadContextを介してtraceIdを追加し、呼び出し後に削除します

  • ログ構成ファイルを変更して、
    traceIdのプレースホルダーを元のログ形式で追加します

<property >[TRACEID:%X{traceId}] %d{HH:mm:ss.SSS} %-5level %class{-1}.%M()/%L - %msg%xEx%n</property>  
 
  • 実行効果

ログ追跡はより便利です

DMCは、logbackとlog4jの構成に使用されます。使用法はThreadContextと同様です。ThreadContext.putをMDC.putに置き換えて、ログ構成ファイルを変更するだけです。

log4j2はMDCでも使用できます

MDCはslf4jパッケージの下にあり、使用するロギングフレームワークは依存関係に関連しています。

おすすめ

転載: blog.csdn.net/baidu_39322753/article/details/109379778