【SAP】ABAP——接口日志

背景:书写RFC接口时,需要添加接口日志,记录OA系统调用RFC接口的传入参数,便于问题排查。

实现代码:

*&---------------------------------------------------------------------*
*&  Include           ZUTIL_RFC_LOG_V1
*&---------------------------------------------------------------------*

**********************************************************************
* 通用宏  记录RFC接口传入参数 V1.0
*
* 为了直接使用运行时的上下文环境
* 必须使用宏
* 若在高并发环境  且RFC是有状态的调用
* 这部分宏的处理需要考虑资源的竞争  锁定解锁带来一定的开销
* 对外RFC接口  基本是无状态调用
* 暂不用考虑写入日志时的互斥
**********************************************************************
DEFINE m_rfc_ipara.
  data:
    lo_xml    type ref to cl_xml_document,
    node_root type ref to if_ixml_node,
    node_import type ref to if_ixml_node,
    node_change type ref to if_ixml_node,
    node_table  type ref to if_ixml_node,

    lt_table  type rsfb_para,
    lt_import type rsfb_para,
    lt_export type rsfb_para,
    lt_change type rsfb_para,
    ls_para   type rsfbpara.

  data: ld_fname type zmm112f-funcname.
  "配置表控制是否记录日志
  select single funcname from zmm112f into ld_fname
    where funcname = &1 and
          inact    = 'X'.
  if sy-subrc = 0.
    data: ls_log type zmm112.

    select count(*) from zmm112f where funcname = &1
                                   and z_inrecd = 'X'.

    if sy-subrc <> 0."配置表控制在日志中是否记录接口传入的参数数据:  不记录接口传入数据
      ls_log-funcname = &1.
      ls_log-datum    = sy-datum.
      ls_log-uzeit    = sy-uzeit.
      ls_log-uname    = sy-uname.

      modify zmm112 from ls_log.
      commit work.
    else."在日志表中记录传入的数据

      try.
          data: exc_ref type ref to cx_root.


*       获取函数信息
          perform read_function_para using  &1
                                            lt_table
                                            lt_import
                                            lt_export
                                            lt_change.

*       添加xml节点
*        data: ls_log type zmm112.
          ls_log-funcname = &1.
          ls_log-datum    = sy-datum.
          ls_log-uzeit    = sy-uzeit.
          ls_log-uname    = sy-uname.

          create object lo_xml.
          node_root   = lo_xml->create_simple_element( name = &1 ).
          node_import = lo_xml->create_simple_element( name = 'IMPORT' parent = node_root ).
          node_change = lo_xml->create_simple_element( name = 'CHANGE' parent = node_root ).
          node_table  = lo_xml->create_simple_element( name = 'TABLE'  parent = node_root ).

          data: ld_name type string.
          field-symbols:  type any.
          "import 结点
          loop at lt_import into ls_para.
            assign (ls_para-parameter) to .
            ld_name = ls_para-parameter .
            lo_xml->set_data( name = ld_name  dataobject =  parent_node = node_import ) .
          endloop.

          "change 结点
          loop at lt_change into ls_para.
            assign (ls_para-parameter) to .
            ld_name = ls_para-parameter .
            lo_xml->set_data( name = ld_name  dataobject =  parent_node = node_change ) .
          endloop.

          "table 结点
          loop at lt_table into ls_para.
            concatenate ls_para-parameter '[]' into ld_name.
            assign (ld_name) to .
            ld_name = ls_para-parameter .
            lo_xml->set_data( name = ld_name  dataobject =  parent_node = node_table ) .
          endloop.

          lo_xml->render_2_string( importing stream = ls_log-vdata ).
          modify zmm112 from ls_log.
          commit work. "必须commit  否则bapi失败 rollback无法记录

        catch  cx_root into exc_ref.
          ls_log-vdata = exc_ref->get_text( ).
          modify zmm112 from ls_log.
          commit work.
      endtry.

    endif.
  endif.

END-OF-DEFINITION.

*&---------------------------------------------------------------------*
*&      Form  read_function_para
*&---------------------------------------------------------------------*
*       text
*----------------------------------------------------------------------*
*      -->ID_FNAME   text
*      -->ET_TABLES  text
*      -->ET_IMPORT  text
*      -->ET_EXPORT  text
*      -->ET_CHANGE  text
*----------------------------------------------------------------------*
FORM read_function_para USING id_fname
                              et_table  TYPE rsfb_para
                              et_import TYPE rsfb_para
                              et_export TYPE rsfb_para
                              et_change TYPE rsfb_para.

  DATA: ls_header   TYPE header_fb,
        ld_pname    TYPE tfdir-pname.
  ls_header-name = id_fname.

  SELECT SINGLE pname
               FROM tfdir
               INTO  ld_pname
               WHERE funcname =  ls_header-name.
  CALL FUNCTION 'FUNCTION_INCLUDE_SPLIT'
    EXPORTING
      program   = ld_pname
    IMPORTING
      group     = ls_header-area
      namespace = ls_header-namespace.
  CONCATENATE ls_header-namespace ls_header-area
         INTO ls_header-area.

  CALL METHOD cl_fb_parameter_db=>read
    IMPORTING
      TABLES = et_table[]
      import = et_import[]
      export = et_export[]
      change = et_change[]
    CHANGING
      header = ls_header.
ENDFORM.                    "read_function_para

 

你可能感兴趣的:(【SAP】ABAP——接口日志)