*&---------------------------------------------------------------------*
*& Report ZDISPLAY_LOG
*&
*&---------------------------------------------------------------------*
*& 参考资料 http://wiki.sdn.sap.com/wiki/display/Snippets/Using+Application+Log
*& 表:BALHDR BALDAT
*& T-Code: SLG0 维护日志对象;SLG1 显示日志报表
*&---------------------------------------------------------------------*
REPORT zdisplay_log.
DATA: gv_log_handle TYPE balloghndl.
DATA: gt_log_header TYPE balhdr_t.
"日志对象信息
DATA: gv_object TYPE balhdr-object VALUE '/GC1/GC',
gv_subobject TYPE balhdr-subobject VALUE 'LOG',
gv_extnumber TYPE balhdr-extnumber VALUE 'Application Log Demo'.
START-OF-SELECTION.
PERFORM frm_log_create.
DATA: l_s_msg TYPE bal_s_msg.
l_s_msg-msgty = 'E'.
l_s_msg-msgid = 'BL'.
l_s_msg-msgno = '003'.
l_s_msg-msgv1 = '文本1'.
l_s_msg-msgv2 = '文本2'.
l_s_msg-msgv3 = '文本3'.
l_s_msg-msgv4 = '文本4'.
PERFORM frm_log_msg_add USING l_s_msg.
PERFORM frm_log_msg_add_text USING '测试直接添加文本日志信息'.
PERFORM frm_log_display.
PERFORM frm_log_save.
PERFORM frm_log_refresh.
PERFORM frm_log_search.
PERFORM frm_log_load.
PERFORM frm_log_display.
PERFORM frm_log_read.
*&---------------------------------------------------------------------*
*& Form frm_log_create
*&---------------------------------------------------------------------*
* 创建日志
*----------------------------------------------------------------------*
FORM frm_log_create.
DATA: ls_log TYPE bal_s_log.
* define some header data of this log
ls_log-extnumber = gv_extnumber.
ls_log-object = gv_object.
ls_log-subobject = gv_subobject.
ls_log-aldate = sy-datum.
ls_log-altime = sy-uzeit.
ls_log-aluser = sy-uname.
ls_log-alprog = sy-repid.
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_s_log = ls_log
IMPORTING
e_log_handle = gv_log_handle
EXCEPTIONS
log_header_inconsistent = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
"
*&---------------------------------------------------------------------*
*& Form frm_log_msg_add
*&---------------------------------------------------------------------*
* 向日志里添加消息(1)
*----------------------------------------------------------------------*
FORM frm_log_msg_add USING msg TYPE bal_s_msg.
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
i_log_handle = gv_log_handle
i_s_msg = l_s_msg
EXCEPTIONS
log_not_found = 1
msg_inconsistent = 2
log_is_full = 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.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_msg_add_text
*&---------------------------------------------------------------------*
* 向日志里添加消息(2)
*----------------------------------------------------------------------*
FORM frm_log_msg_add_text USING msg TYPE char100.
CALL FUNCTION 'BAL_LOG_MSG_ADD_FREE_TEXT'
EXPORTING
i_msgty = 'S'
i_probclass = '4'
i_text = msg
EXCEPTIONS
log_not_found = 1
msg_inconsistent = 2
log_is_full = 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.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_display
*&---------------------------------------------------------------------*
* 显示日志
*----------------------------------------------------------------------*
FORM frm_log_display.
DATA: l_s_display_profile TYPE bal_s_prof.
*BAL_DSP_PROFILE_DETLEVEL_GET "显示一个界面 空的?
*BAL_DSP_PROFILE_NO_TREE_GET "显示一个界面
*BAL_DSP_PROFILE_POPUP_GET "弹出框式
*BAL_DSP_PROFILE_SINGLE_LOG_GET "显示一个界面 包含TREE
*BAL_DSP_PROFILE_STANDARD_GET "显示一个界面 包含TREE 需要双击TREE节点才显示日志信息
* get a prepared profile
CALL FUNCTION 'BAL_DSP_PROFILE_POPUP_GET'
IMPORTING
e_s_display_profile = l_s_display_profile
EXCEPTIONS
OTHERS = 1.
* use grid for display if wanted
l_s_display_profile-use_grid = 'X'.
* set report to allow saving of variants
l_s_display_profile-disvariant-report = sy-repid.
* when you use also other ALV lists in your report,
* please specify a handle to distinguish between the display
* variants of these different lists, e.g:
l_s_display_profile-disvariant-handle = 'LOG'.
************************************************************************
CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
EXPORTING
i_s_display_profile = l_s_display_profile
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.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_save
*&---------------------------------------------------------------------*
* 保存日志到数据库
*----------------------------------------------------------------------*
FORM frm_log_save.
CALL FUNCTION 'BAL_DB_SAVE'
EXPORTING
i_save_all = 'X'
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.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_refresh
*&---------------------------------------------------------------------*
* 清空内存中的所有日志
*----------------------------------------------------------------------*
FORM frm_log_refresh.
CALL FUNCTION 'BAL_LOG_REFRESH'
EXPORTING
i_log_handle = gv_log_handle
EXCEPTIONS
log_not_found = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_search
*&---------------------------------------------------------------------*
* 在数据库里查找日志
*----------------------------------------------------------------------*
FORM frm_log_search.
DATA: gr_object TYPE bal_s_obj.
DATA: gr_extnumber TYPE bal_s_extn.
DATA: gs_log_filter TYPE bal_s_lfil.
* create a filter with all relevant criteria:
gr_object-sign = 'I'.
gr_object-option = 'EQ'.
gr_object-low = '/GC1/GC'.
APPEND gr_object TO gs_log_filter-object.
gr_extnumber-sign = 'I'.
gr_extnumber-option = 'EQ'.
gr_extnumber-low = 'Application Log Demo'.
APPEND gr_extnumber TO gs_log_filter-extnumber.
REFRESH: gt_log_header.
* search on DB for these logs
CALL FUNCTION 'BAL_DB_SEARCH'
EXPORTING
i_s_log_filter = gs_log_filter
IMPORTING
e_t_log_header = gt_log_header
EXCEPTIONS
log_not_found = 1
no_filter_criteria = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_load
*&---------------------------------------------------------------------*
* 从数据库加载日志
*----------------------------------------------------------------------*
FORM frm_log_load.
IF gt_log_header IS INITIAL.
MESSAGE '请先调用 BAL_DB_SEARCH 方法检索日志信息' TYPE 'E'.
EXIT.
ENDIF.
* load these messages into memory
CALL FUNCTION 'BAL_DB_LOAD'
EXPORTING
i_t_log_header = gt_log_header
EXCEPTIONS
no_logs_specified = 1
log_not_found = 2
log_already_loaded = 3.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_read
*&---------------------------------------------------------------------*
* 从数据库中读取日志信息到内表
*----------------------------------------------------------------------*
FORM frm_log_read.
DATA: p_number_of_logs LIKE sy-tabix.
* Log header data
DATA: BEGIN OF p_header_data_tab OCCURS 0.
INCLUDE STRUCTURE balhdr.
DATA: END OF p_header_data_tab.
* Log parameters
DATA: BEGIN OF p_header_para_tab OCCURS 0.
INCLUDE STRUCTURE balhdrp.
DATA: END OF p_header_para_tab.
* Log messages
DATA: BEGIN OF p_message_tab OCCURS 0.
INCLUDE STRUCTURE balm.
DATA: END OF p_message_tab.
* Message parameters
DATA: BEGIN OF p_message_para_tab OCCURS 0.
INCLUDE STRUCTURE balmp.
DATA: END OF p_message_para_tab.
CALL FUNCTION 'APPL_LOG_READ_DB'
EXPORTING
object = '/GC1/GC'
subobject = 'LOG'
external_number = 'Application Log Demo'
IMPORTING
number_of_logs = p_number_of_logs
TABLES
header_data = p_header_data_tab
header_parameters = p_header_para_tab
messages = p_message_tab
message_parameters = p_message_para_tab.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*&---------------------------------------------------------------------*
*& Form frm_log_delete
*&---------------------------------------------------------------------*
* 删除数据库里的日志
*----------------------------------------------------------------------*
FORM frm_log_delete.
IF gt_log_header IS INITIAL.
MESSAGE '请先调用 BAL_DB_SEARCH 方法检索日志信息' TYPE 'E'.
EXIT.
ENDIF.
CALL FUNCTION 'BAL_DB_DELETE'
EXPORTING
i_t_logs_to_delete = gt_log_header
EXCEPTIONS
no_logs_specified = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
ENDFORM.
*Messages
*----------------------------------------------------------
*
* Message class: Hard coded
* 请先调用 BAL_DB_SEARCH 方法检索日志信息