打日志的技巧【接口类】

如果要查出bug,打日志是必不可少的。那么什么时候应该打日志呢。日志并不是多多益善,有一些不必要的日志是没有必要打的。

public CCZ3Response sendFrontRecommend(CCZ3Request ccz3request) throws JAXBException { 
        //接收类的时候判断一次
        if(ccz3request==null)
        {
             logger.info("ccz3request is null");
           return null;
        }
         logger.info("ccz3request is as follows"+ccz3request.toString());

        CCZ3Request.HEAD head=new CCZ3Request.HEAD();
        head.setTRCD("CCZ3");//交易代码
         head.setTRDT(YearMonDay());//渠道日期  
        head.setTRTM(HourMinSec());//交易时间
          //前台交易流水
         String mscoprseq= YearMonDay().substring(4)+HourMinSec()+"000001";
         head.setMSCOPRSEQ(mscoprseq);//前台交易流水
        head.setTRTP("");//交易类别
        head.setSOUR("MH");
        ccz3request.setHEAD(head);
        ccz3request.getBODY().setCheckscore("");
        ccz3request.getBODY().setDistinctsum("");

       //设置编码格式
        String encoding = "GB18030";
        System.out.println("---convert java to stringxml Start---");  
        String xmlobj= convertToXml(ccz3request);
        xmlobj="<?xml version=\"1.0\" encoding=\"GB18030\"?>"+xmlobj;

        //发送报文之前判断报文的正确性
        logger.info("xmlobj is as follows"+xmlobj);

        String response = this.sendXml(xmlobj, encoding);
        if(response==null)
        {
             logger.info("response xml is null and response is as follows"+response.toString());
          return null;

        }else
        {
             logger.info("response xml is not null and response is as follows"+response.toString());
        }
        CCZ3Response ccz3response = (CCZ3Response) convertXmlStrToObject(CCZ3Response.class, response);  
          if(ccz3response==null)
        {
             logger.info("ccz3response class is null and response xml convert to java is fuliure");
          return null;

        }else
        {
             logger.info("response xml convert to java is success");
              //将返回的相应报文返回 
            logger.info("ccz3response class is null and ccz3response is as follows"+ccz3response.toString());
        }
       return ccz3response;

    }

(1)首先一个函数接收到一个参数,则首先判断判断参数是否为空,如果为空则直接返回空并打印‘参数为空’的log,如果不为空则直接将参数打印出来。
例如:

    if(ccz3request==null)
        {
             logger.info("ccz3request is null");
           return null;
        }
         logger.info("ccz3request is as follows"+ccz3request.toString());

(2)然后在向前置发送报文之前检查自己所要发送的报文是否符合前置要求的格式。如下

 logger.info("xmlobj is as follows"+xmlobj);

(3)然后得到前置的响应之后,首先判断前置的响应是否为空。如果为空,则直接返回null并在log里面注明‘响应值为空’,如下:

      if(response==null)
        {
             logger.info("response xml is null and response is as follows"+response.toString());
          return null;

        }else
        {
             logger.info("response xml is not null and response is as follows"+response.toString());
        }

(4)然后如果前置响应判断不为空,则将其转换成java类,因为这个转换的过程也有可能出错,所以此处也应该打log。那么转换成的java类为空,则判断相应类转换失败,并在log里面打印出来。否则,如果相应类不为空,则将其打印出来,判断其是否打印正确。如下:

“`
if(ccz3response==null)
{
logger.info(“ccz3response class is null and response xml convert to java is fuliure”);
return null;

    }else
    {
         logger.info("response xml convert to java is success");

      logger.info("ccz3response class is null and ccz3response is as follows"+ccz3response.toString());
    }

总而言之,也就是说应该在边界区域打日志,保证入口和出口的正确性!

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值