SAP 函数调用日志记录方案

2022-06-26 09:14:17 浏览数 (1)

思路:

将函数调用的参数和结果使用json序列化后保存到表中

效果

建表

ZFUNC_LOGS

代码语言:javascript复制
MANDT	MANDT
ID	SYSUUID_C22
FUNCNAME	RS38L_FNAM
UNAME	UNAME
ERDAT	ERDAT
UZEIT	UZEIT
INB_MESSAGE	ASR_FIELD_VALUE
OUTB_MESSAGE	ASR_FIELD_VALUE
EX_MESSAGE	ASR_FIELD_VALUE

函数

ZFUNC_LOGS_RECORDER

代码语言:javascript复制
function zfunc_logs_recorder.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(FUNCNAME) TYPE  RS38L_FNAM
*"     VALUE(INB_MESSAGE) TYPE  STRING OPTIONAL
*"     VALUE(OUTB_MESSAGE) TYPE  STRING OPTIONAL
*"     VALUE(EX_MESSAGE) TYPE  STRING OPTIONAL
*"----------------------------------------------------------------------
* DATA: l_uuid_c22  TYPE sysuuid_c22.

  cl_system_uuid=>if_system_uuid_static~create_uuid_c22( receiving uuid = data(lv_uuid_c22) ).

  data: lw_logs type zfunc_logs.
  lw_logs-id = lv_uuid_c22.
  lw_logs-funcname = funcname.
  lw_logs-inb_message = inb_message.
  lw_logs-outb_message = outb_message.
  lw_logs-ex_message = ex_message.
  lw_logs-uname = sy-uname.
  lw_logs-erdat = sy-datum.
  lw_logs-uzeit = sy-uzeit.
  insert zfunc_logs from lw_logs.
endfunction.

样例

ZFUNC_LOGS_SAMPLE

代码语言:javascript复制
function zfunc_logs_sample.
*"----------------------------------------------------------------------
*"*"本地接口:
*"  IMPORTING
*"     VALUE(IMPORT_PARA1) TYPE  STRING
*"     VALUE(IMPORT_PARA2) TYPE  DATUM
*"     VALUE(IMPORT_PARA3) TYPE  CHAR1
*"     VALUE(IMPORT_PARA4) TYPE  WERKS
*"  EXPORTING
*"     VALUE(EXPORT_PARA1) TYPE  STRING
*"  TABLES
*"      TABLE_PARA1 STRUCTURE  ZPP_MSG_RETURN
*"  CHANGING
*"     VALUE(CHANGE_PARA1) TYPE  STRING
*"  EXCEPTIONS
*"      EX1
*"      EX2
*"----------------------------------------------------------------------


* json 序列化器
  data: json_ser type ref to cl_trex_json_serializer.
  data: inb_message  type string,
        outb_message type string,
        ex_message   type string.
* 准备入参消息-----------------------------------------------------------
  data: begin of inb_object,
          import_para1 like  import_para1,
          import_para2 like import_para2,
          import_para3 like import_para3,
          import_para4 like import_para4,
          table_para1  like table of table_para1,
          change_para1 like change_para1,
        end of inb_object.
  inb_object-import_para1 = import_para1.
  inb_object-import_para2 = import_para2.
  inb_object-import_para3 = import_para3.
  inb_object-import_para4 = import_para4.
  loop at table_para1.
    append table_para1 to inb_object-table_para1.
  endloop.
  inb_object-change_para1 = change_para1.
  create object json_ser
    exporting
      data = inb_object.
  call method json_ser->serialize.
  call method json_ser->get_data
    receiving
      rval = inb_message.
* 准备入参消息-----------------------------------------------------------

* 处理逻辑
  change_para1 = 'change-result'.
  export_para1 = 'export-result'.
  loop at table_para1.
    table_para1-message_v4 = 'msg-result'.
    modify table_para1.
  endloop.

* 准备出参消息-----------------------------------------------------------
  data: begin of outb_object,
          export_para1 like export_para1,
          table_para1  like table of table_para1,
          change_para1 like change_para1,
        end of outb_object.
  outb_object-export_para1 = 'result'.
  loop at table_para1.
    append table_para1 to outb_object-table_para1.
  endloop.
  outb_object-change_para1 = change_para1.
  create object json_ser
    exporting
      data = outb_object.
  call method json_ser->serialize.
  call method json_ser->get_data
    receiving
      rval = outb_message.
* 准备入参消息-----------------------------------------------------------
* 记录日志
  data ls_funcname type rs38l_fnam.
  ls_funcname = 'ZFUNC_LOGS_SAMPLE'.
  call function 'ZFUNC_LOGS_RECORDER'
    exporting
      funcname     = ls_funcname
      inb_message  = inb_message
      outb_message = outb_message.

endfunction.

0 人点赞