代码之家  ›  专栏  ›  技术社区  ›  Nuñito Calzada

sonarLint:仅有条件地调用方法

  •  1
  • Nuñito Calzada  · 技术社区  · 2 年前

    我有一段代码:

    logger.error("Unauthorized error: {} with request {} from {} ",
            authException.getMessage(),
            request.getServletPath(),
            getClientIp(request));
    

    以及:

      private static String getClientIp(HttpServletRequest request) {
    
            String remoteAddr = "";
    
            if (request != null) {
                remoteAddr = request.getHeader("X-FORWARDED-FOR");
                if (remoteAddr == null || "".equals(remoteAddr)) {
                    remoteAddr = request.getRemoteAddr();
                }
            }
    
            return remoteAddr;
        }
    

    我有一个警告:

     Invoke method(s) only conditionally
    
    1 回复  |  直到 2 年前
        1
  •  1
  •   Unmitigated    2 年前

    你可以检查 isErrorEnabled 在登录之前。

    if (logger.isErrorEnabled())
        logger.error("Unauthorized error: {} with request {} from {} ",
            authException.getMessage(),
            request.getServletPath(),
            getClientIp(request));
    
    推荐文章