ABAP SLG1/SLG0 应用日志 自己封装类 记录

1 封装类 ZBAL_LOG_COMMON

CLASS zbal_log_common DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.

    METHODS constructor
      IMPORTING
        !is_bal_s_log TYPE bal_s_log .
    METHODS log_message_create
      IMPORTING
        !is_msg TYPE bal_s_msg .
    METHODS log_message_save .
    METHODS log_message_display .
  PROTECTED SECTION.
  PRIVATE SECTION.

    DATA mv_log_handle TYPE balloghndl .
    DATA mt_log_handle TYPE bal_t_logh .
ENDCLASS.



CLASS ZBAL_LOG_COMMON IMPLEMENTATION.


* ---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_BAL_S_LOG                   TYPE        BAL_S_LOG
* +--------------------------------------------------------------------------------------
  METHOD constructor.
*    is_log-object    = 'zsrm_mail'.
*    is_log-subobject = 'to_approver'.
*    is_log-aluser    = sy-uname.
*    is_log-alprog    = sy-repid.
*    is_log-aldate_del = sy-datum + 1.       "保留日志120天,默认9999.12.31
*    is_log-del_before = 'X'.                    "保留日志直到到期

* create the log header
    CALL FUNCTION 'BAL_LOG_CREATE'
      EXPORTING
        i_s_log      = is_bal_s_log
      IMPORTING
        e_log_handle = mv_log_handle
      EXCEPTIONS
        OTHERS       = 1.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.


  ENDMETHOD.


* ---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->LOG_MESSAGE_CREATE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_MSG                         TYPE        BAL_S_MSG
* +--------------------------------------------------------------------------------------
  METHOD log_message_create.
    "Create Message
*    is_msg-msgty = 'S'.
*    is_msg-msgid = '/BBA/BP'.
*    is_msg-msgno = '000'.
*    is_msg-msgv1 = '开始时间:' && sy-uzeit.
*    is_msg-msgv2 = ''.
*    is_msg-msgv3 = ''.
*    is_msg-msgv4 = ''.
*    is_msg-probclass = 2.;

* create the message
    CALL FUNCTION 'BAL_LOG_MSG_ADD'
      EXPORTING
        i_log_handle = mv_log_handle
        i_s_msg      = is_msg
      EXCEPTIONS
        OTHERS       = 1.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
              WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

* save this log
    INSERT mv_log_handle INTO TABLE mt_log_handle.

  ENDMETHOD.


* ---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->LOG_MESSAGE_DISPLAY
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------
  METHOD log_message_display.
* Display application log
    DATA:ls_disp_profile TYPE bal_s_prof.

    CALL FUNCTION 'BAL_DSP_PROFILE_NO_TREE_GET'
      IMPORTING
        e_s_display_profile = ls_disp_profile.
    ls_disp_profile-disvariant-report = sy-repid.
    ls_disp_profile-disvariant-handle = 'LOG'.
    ls_disp_profile-use_grid = 'X'.

    CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
      EXPORTING
        i_s_display_profile  = ls_disp_profile
        i_t_log_handle       = mt_log_handle
      EXCEPTIONS
        profile_inconsistent = 1
        internal_error       = 2
        no_data_available    = 3
        no_authority         = 4
        OTHERS               = 5.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.
  ENDMETHOD.


* ---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->LOG_MESSAGE_SAVE
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------
  METHOD log_message_save.
    CALL FUNCTION 'BAL_DB_SAVE'
      EXPORTING
        i_t_log_handle   = mt_log_handle
      EXCEPTIONS
        log_not_found    = 1
        save_not_allowed = 2
        numbering_error  = 3
        OTHERS           = 4.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
             WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.


  ENDMETHOD.
ENDCLASS.

2 调用示例

*&---------------------------------------------------------------------*
*& Report  YTEST_01
*&
*&---------------------------------------------------------------------*
*&
*&
*&---------------------------------------------------------------------*
REPORT ytest_01.



DATA ls_bal_s_log TYPE bal_s_log.

DATA lo_log TYPE REF TO zbal_log_common.
* define some header data of this log
ls_bal_s_log-extnumber = 'Application Log Demo'.            "#EC NOTEXT
ls_bal_s_log-aluser    = sy-uname.
ls_bal_s_log-alprog    = sy-repid.
ls_bal_s_log-alprog    = sy-repid.
ls_bal_s_log-object    = '/BDL/LOG'.
ls_bal_s_log-subobject = 'DELETE'.

CREATE OBJECT lo_log
  EXPORTING
    is_bal_s_log = ls_bal_s_log.

DATA ls_msg TYPE bal_s_msg.

MESSAGE e301(bl) INTO DATA(g_dummy).

* define data of message for Application Log
ls_msg-msgty     = sy-msgty.
ls_msg-msgid     = sy-msgid.
ls_msg-msgno     = sy-msgno.
ls_msg-msgv1     = sy-msgv1.
ls_msg-msgv2     = sy-msgv2.
ls_msg-msgv3     = sy-msgv3.
ls_msg-msgv4     = sy-msgv4.
ls_msg-probclass = 2.

CALL METHOD lo_log->log_message_create
  EXPORTING
    is_msg = ls_msg.

CALL METHOD lo_log->log_message_save.


CALL METHOD lo_log->log_message_display.

你可能感兴趣的:(开发语言)