Often there is a need to display a list of messages to a SAP user. The ABAP message log can display information about the progress of the SAP program. It is also necessary to display a list of errors that occurred as a result of its execution. As a result, we will analyze a set of FMs intended for displaying the ABAP message log.
Implementations are a global variable in an ABAP program or an attribute of a class. And in the course of program execution, messages are stored in this variable. At the end of the program execution, a log of messages is displayed. Or the log is stored in the SAP system and can be viewed in transaction SLG1.
Stages of working with the SAP message log.
Creating an message log ABAP SAP.
The SAP message log instance is created using the function module BAL_LOG_CREATE.
1 2 3 4 5 6 7 8 9 10 11 |
DATA: ls_log TYPE bal_s_log, lv_log_h TYPE balloghndl. CALL FUNCTION 'BAL_LOG_CREATE' EXPORTING i_s_log = ls_log IMPORTING e_log_handle = lv_log_h EXCEPTIONS log_header_inconsistent = 0. |
Adding messages to the SAP log.
You can add new messages to the log using the BAL_LOG_MSG_ADD function module.
1 2 3 4 5 6 7 8 9 10 11 12 |
DATA: ls_msg TYPE bal_s_msg, lv_log_h TYPE balloghndl. ls_msg-msgid = 'ESH_TAU_MSG'. ls_msg-msgno = '000'. ls_msg-msgty = 'E'. CALL FUNCTION 'BAL_LOG_MSG_ADD' EXPORTING i_log_handle = lv_log_h i_s_msg = ls_msg. |
Displaying the ABAP message log on the screen.
To display the ABAP message log on the screen as a pop-up window, use the BAL_DSP_LOG_DISPLAY FM.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
" Populate the log window variable CALL FUNCTION 'BAL_DSP_PROFILE_STANDARD_GET' IMPORTING e_s_display_profile = ls_prof. " Setting the window size ls_prof-start_row = 5. ls_prof-end_row = 25. ls_prof-start_col = 10. ls_prof-end_col = 150. ls_prof-show_all = abap_true. " Bringing the window to the screen CALL FUNCTION 'BAL_DSP_LOG_DISPLAY' EXPORTING i_s_display_profile = ls_prof. |
Deletion of the ABAP message log from memory.
The BAL_LOG_REFRESH FM is used to remove the ABAP message log from memory. By the message log identifier i_log_handle, it finds the log and removes it from memory.
1 2 3 4 5 6 7 8 |
DATA: lv_log_h TYPE balloghndl. CALL FUNCTION 'BAL_LOG_REFRESH' EXPORTING i_log_handle = lv_log_h EXCEPTIONS LOG_NOT_FOUND = 0. |
ABAP message log output template:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
DATA: ls_log TYPE bal_s_log, ls_prof TYPE bal_s_prof, lv_log_h TYPE balloghndl. CALL FUNCTION 'BAL_LOG_CREATE' EXPORTING i_s_log = ls_log IMPORTING e_log_handle = lv_log_h EXCEPTIONS OTHERS = 0. LOOP AT mt_msg REFERENCE INTO DATA(lr_msg). CALL FUNCTION 'BAL_LOG_MSG_ADD' EXPORTING i_log_handle = lv_log_h i_s_msg = lr_msg->*. ENDLOOP. CALL FUNCTION 'BAL_DSP_PROFILE_STANDARD_GET' IMPORTING e_s_display_profile = ls_prof. ls_prof-start_row = 5. ls_prof-end_row = 25. ls_prof-start_col = 10. ls_prof-end_col = 150. ls_prof-show_all = abap_true. CALL FUNCTION 'BAL_DSP_LOG_DISPLAY' EXPORTING i_s_display_profile = ls_prof. CALL FUNCTION 'BAL_LOG_REFRESH' EXPORTING i_log_handle = lv_log_h. CLEAR mt_msg. |
Ready. As a result, we get a message log on the screen. And each line has an indicator depending on the type of message with text. It is also possible to view the details of each message. You can use the option without a global variable. For example, we can immediately create an ABAP message object and save the messages directly to the log. This is suitable for background execution. Also, the need for a global variable is affected by the need for a complete list of messages at the end of execution.
Also many interesting examples can be found in SBAL_DEMO*.