In this tutorial, we will explain how to implement a BAL log or application log in an ABAP program.
The following function modules will be used when implementing BAL Log:
- BAL_LOG_CREATE : To create and initialize the BAL log
- BAL_LOG_MSG_ADD : To add message to the BAL log
- BAL_DSP_LOG_DISPLAY : To display the BAL log
Step 1 – Creating BAL Log:
The first step is to create/setup the BAL log.
The following code logic will be used to create the BAL log:
*----------------------------------------------------------------------* * Sub routine to create the application log for error logging * * purposes. * *----------------------------------------------------------------------* FORM f_create_log. *----------------------------------------------------------------------* * Local data declarations. * Structures: DATA: lst_log TYPE bal_s_log. * Defining some header data of the application log. lst_log-extnumber = text-006. \" Text: \'Program Log\'. lst_log-aluser = sy-uname. lst_log-alprog = sy-repid. * Creationg the application log. CALL FUNCTION \'BAL_LOG_CREATE\' EXPORTING i_s_log = lst_log EXCEPTIONS OTHERS = 1. IF sy-subrc NE 0. * Display error message. MESSAGE ID sy-msgid TYPE gc_i NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE gc_e. ENDIF. ENDFORM. \" F_CREATE_LOG
Step 2 – Adding Message to the BAL Log:
Once the BAL log has been created, we will have to add logging message to it which will later be used to display to the user. To add message to the BAL log, we will use the function module ‘BAL_LOG_MSG_ADD’.
The following code logic will be used to add messages to the BAL log:
*----------------------------------------------------------------------* * Sub routine to add a message to the processing log. * *----------------------------------------------------------------------* * -->PI_PROBCLASS Problem Class. * *----------------------------------------------------------------------* FORM f_add_msg_to_log USING pi_probclass TYPE bal_s_msg-probclass. *----------------------------------------------------------------------* * Local data declarations. * Structures: DATA: lst_msg TYPE bal_s_msg. * Defininig data of message for the application log. lst_msg-msgty = sy-msgty. lst_msg-msgid = sy-msgid. lst_msg-msgno = sy-msgno. lst_msg-msgv1 = sy-msgv1. lst_msg-msgv2 = sy-msgv2. lst_msg-msgv3 = sy-msgv3. lst_msg-msgv4 = sy-msgv4. lst_msg-probclass = pi_probclass. * Adding this message to log file. CALL FUNCTION \'BAL_LOG_MSG_ADD\' EXPORTING i_s_msg = lst_msg EXCEPTIONS log_not_found = 0 OTHERS = 1. IF sy-subrc NE 0. * Display error message. MESSAGE ID sy-msgid TYPE gc_i NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE gc_e. * Stop processing and go to end of selection. STOP. ENDIF. ENDFORM. \"f_add_msg_to_log
Note: The above sub routine must be called as follows:
*Add the message to the log. PERFORM f_add_msg_to_log USING gc_probclass_none.
Where the constant ‘GC_PROBCLASS_NONE’ is declared as follows:
CONSTANTS: gc_probclass_none TYPE bal_s_msg-probclass VALUE \' \'.
We can also pass the following value to the field ‘LST_MSG-PROBCLASS’ for different type of message error type.
CONSTANTS: gc_probclass_medium TYPE bal_s_msg-probclass VALUE \'3\', gc_probclass_low TYPE bal_s_msg-probclass VALUE \'4\'.
Step 3 – Displaying the BAL Log:
Once log messages have been added to the BAL log, we can display the BAL log using the function module ‘BAL_DSP_LOG_DISPLAY’. Normally, we display in the event ‘END-OF-SELECTION’.
The following code logic will be used to display the BAL log:
*----------------------------------------------------------------------* * Sub routine to display the error log. * *----------------------------------------------------------------------* FORM f_display_log. *----------------------------------------------------------------------* * Function module to display the processing log. CALL FUNCTION \'BAL_DSP_LOG_DISPLAY\' EXPORTING * Sort logs by Timestamp (\'X\') or Log Number (SPACE). i_srt_by_timstmp = space EXCEPTIONS profile_inconsistent = 1 internal_error = 2 no_data_available = 3 no_authority = 4. IF sy-subrc NE 0. * Display error message. MESSAGE ID sy-msgid TYPE gc_i NUMBER sy-msgno WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4 DISPLAY LIKE gc_e. ENDIF. ENDFORM. \" F_DISPLAY_LOG
Screenshot of a BAL log:
To find more standard SAP BAL log demo, type ‘SBAL_DEMO_*’ in SE38 and press F4.
One response to “Using BAL Log for Application Logging”
[…] hope this helps. You can also refer to this tutorial on how to display application log automatically in a […]