*&---------------------------------------------------------------------*
*& Report ZDISPLAY_LOG
*&
*&---------------------------------------------------------------------*
*& 参考资料 http://wiki.sdn.sap.com/wiki/display/Snippets/Using+Application+Log
*& 表:BALHDR BALDAT
*& T-Code: SLG0 维护日志对象;SLG1 显示日志报表
*&---------------------------------------------------------------------*
*& 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. "Application Log: Log Handle 22个字符的GUID
DATA: gt_log_header TYPE balhdr_t. "作从数据库读取或是删除用
"日志对象信息
DATA: gv_object TYPE balhdr-object VALUE '/GC1/GC', "TCode:SLG0 维护日志对象
gv_subobject TYPE balhdr-subobject VALUE 'LOG', "TCode:SLG0 维护日志对象子对象
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'. "BL是系统自带的消息类
l_s_msg-msgno = '003'. "包含4个参数
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.
"PERFORM frm_log_delete.
DATA: gv_log_handle TYPE balloghndl. "Application Log: Log Handle 22个字符的GUID
DATA: gt_log_header TYPE balhdr_t. "作从数据库读取或是删除用
"日志对象信息
DATA: gv_object TYPE balhdr-object VALUE '/GC1/GC', "TCode:SLG0 维护日志对象
gv_subobject TYPE balhdr-subobject VALUE 'LOG', "TCode:SLG0 维护日志对象子对象
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'. "BL是系统自带的消息类
l_s_msg-msgno = '003'. "包含4个参数
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.
"PERFORM frm_log_delete.
*&---------------------------------------------------------------------*
*& Form frm_log_create
*&---------------------------------------------------------------------*
* 创建日志
*----------------------------------------------------------------------*
*& Form frm_log_create
*&---------------------------------------------------------------------*
* 创建日志
*----------------------------------------------------------------------*
FORM frm_log_create.
DATA: ls_log TYPE bal_s_log. "Log header data
DATA: ls_log TYPE bal_s_log. "Log header data
* define some header data of this log
ls_log-extnumber = gv_extnumber.
ls_log-object = gv_object. "TCode:SLG0 维护日志对象
ls_log-subobject = gv_subobject. "TCode:SLG0 维护日志对象子对象
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. "frm_log_create
"
ls_log-object = gv_object. "TCode:SLG0 维护日志对象
ls_log-subobject = gv_subobject. "TCode:SLG0 维护日志对象子对象
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. "frm_log_create
"
*&---------------------------------------------------------------------*
*& Form frm_log_msg_add
*&---------------------------------------------------------------------*
* 向日志里添加消息(1)
*----------------------------------------------------------------------*
*& 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. "frm_log_msg_add
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. "frm_log_msg_add
*&---------------------------------------------------------------------*
*& Form frm_log_msg_add_text
*&---------------------------------------------------------------------*
* 向日志里添加消息(2)
*----------------------------------------------------------------------*
*& 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' "信息级别 1-非常重要;2-重要;3-中等;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. "frm_log_msg_add_text
CALL FUNCTION 'BAL_LOG_MSG_ADD_FREE_TEXT'
EXPORTING
i_msgty = 'S'
i_probclass = '4' "信息级别 1-非常重要;2-重要;3-中等;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. "frm_log_msg_add_text
*&---------------------------------------------------------------------*
*& Form frm_log_display
*&---------------------------------------------------------------------*
* 显示日志
*----------------------------------------------------------------------*
*& Form frm_log_display
*&---------------------------------------------------------------------*
* 显示日志
*----------------------------------------------------------------------*
FORM frm_log_display.
"设置日志显示方式
DATA: l_s_display_profile TYPE bal_s_prof.
"设置日志显示方式
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
*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.
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:
* 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. "frm_log_display
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. "frm_log_display
*&---------------------------------------------------------------------*
*& Form frm_log_save
*&---------------------------------------------------------------------*
* 保存日志到数据库
*----------------------------------------------------------------------*
*& 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. "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. "frm_log_save
*&---------------------------------------------------------------------*
*& Form frm_log_refresh
*&---------------------------------------------------------------------*
* 清空内存中的所有日志
*----------------------------------------------------------------------*
*& 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. "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. "frm_log_refresh
*&---------------------------------------------------------------------*
*& Form frm_log_search
*&---------------------------------------------------------------------*
* 在数据库里查找日志
*----------------------------------------------------------------------*
*& 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. "此对象里有很多筛选条件可供使用
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.
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. "frm_log_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. "frm_log_search
*&---------------------------------------------------------------------*
*& Form frm_log_load
*&---------------------------------------------------------------------*
* 从数据库加载日志
*----------------------------------------------------------------------*
*& Form frm_log_load
*&---------------------------------------------------------------------*
* 从数据库加载日志
*----------------------------------------------------------------------*
FORM frm_log_load.
IF gt_log_header IS INITIAL.
MESSAGE '请先调用 BAL_DB_SEARCH 方法检索日志信息' TYPE 'E'.
EXIT.
ENDIF.
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 "来源于之前的 BAL_DB_SEARCH 方法
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. "frm_log_load
EXPORTING
i_t_log_header = gt_log_header "来源于之前的 BAL_DB_SEARCH 方法
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. "frm_log_load
*&---------------------------------------------------------------------*
*& Form frm_log_read
*&---------------------------------------------------------------------*
* 从数据库中读取日志信息到内表
*----------------------------------------------------------------------*
*& Form frm_log_read
*&---------------------------------------------------------------------*
* 从数据库中读取日志信息到内表
*----------------------------------------------------------------------*
FORM frm_log_read.
DATA: p_number_of_logs LIKE sy-tabix.
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.
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.
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.
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. "frm_log_read
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. "frm_log_read
*&---------------------------------------------------------------------*
*& Form frm_log_delete
*&---------------------------------------------------------------------*
* 删除数据库里的日志
*----------------------------------------------------------------------*
*& 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 "来源于之前的 BAL_DB_SEARCH 方法
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. "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 "来源于之前的 BAL_DB_SEARCH 方法
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. "frm_log_delete
*Messages
*----------------------------------------------------------
*
* Message class: Hard coded
* 请先调用 BAL_DB_SEARCH 方法检索日志信息
*----------------------------------------------------------
*
* Message class: Hard coded
* 请先调用 BAL_DB_SEARCH 方法检索日志信息