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.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->CONSTRUCTOR
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_BAL_S_LOG TYPE BAL_S_LOG
* +--------------------------------------------------------------------------------------</SIGNATURE>
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.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->LOG_MESSAGE_CREATE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IS_MSG TYPE BAL_S_MSG
* +--------------------------------------------------------------------------------------</SIGNATURE>
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.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->LOG_MESSAGE_DISPLAY
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
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.
* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Instance Public Method ZBAL_LOG_COMMON->LOG_MESSAGE_SAVE
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
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.
* define some header data of this log
DATA(ls_bal_s_log) = VALUE bal_s_log( extnumber = 'Application Log Demo'
aluser = sy-uname
alprog = sy-repid
object = '/BDL/LOG' "replace owner object
subobject = 'DELETE' "replace owner subobject
).
*Create zbal_log_common object
DATA(lo_log) = NEW zbal_log_common( ls_bal_s_log ).
MESSAGE e301(bl) INTO DATA(g_dummy).
* define data of message for Application Log
DATA(ls_msg) = VALUE bal_s_msg( msgty = sy-msgty
msgid = sy-msgid
msgno = sy-msgno
msgv1 = sy-msgv1
msgv2 = sy-msgv2
msgv3 = sy-msgv3
msgv4 = sy-msgv4
probclass = 2
).
*Add msg
lo_log->log_message_create( is_msg = ls_msg ).
*Save msg
lo_log->log_message_save( ).
*Display msg
lo_log->log_message_display( ).