2012 Jan 11 3:53 PM
Dear SAP community,
does SAP support writting log messages not into database but into log file?
My Problem:
I am trying to check if the bal messages would be saved in database even if an ASSERTION_FAILED occures. As you see in the report below, the function 'BAL_DB_SAVE' doesn't really save the logs directly into the database table but it waits until a COMMIT WORK is executred which means if any ASSERTION-FAILED occurs in between no BAL will be saved and I'll loose the traces of my program flow which I've written in that BAL handler.
Thank you in advance.
Bahjat
Here is a short report demonstrating my problem.
REPORT Z_BAL_AFTER_ASSERT_TEST.
PARAMETERS: pv_x type abap_bool as CHECKBOX.
data:
lv_bool type abap_bool,
lt_but type table of but000,
ls_but type but000.
DATA: lv_log_handle TYPE balloghndl,
ls_log TYPE bal_s_log,
ls_msg TYPE bal_s_msg,
lv_msgno TYPE symsgno,
lt_log_handle TYPE bal_t_logh.
* -------------------------------------------------------------------------------------
START-OF-SELECTION.
* Start BAL Logging
* -----------------------------------------------------------------
CONCATENATE sy-datum sy-uzeit INTO ls_log-extnumber SEPARATED BY space. "Unique number for the current log
ls_log-object = 'ZSALBAH_BAL'. "See table BALOBJ for possible entries (new entries via SLG0) CRM_DOCUMENT
ls_log-subobject = 'BAL_TEST'. "See table BALSUB for possible entries (new entries via SLG0) SINGLE
* Initialize BAL Logging (see TX SLG1)
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_s_log = ls_log
IMPORTING
e_log_handle = lv_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.
EXIT.
ENDIF.
* -----------------------------------------------------------------
ls_msg-msgid = 'ZCRM'.
ls_msg-msgno = '000'.
ls_msg-msgty = 'S'.
ls_msg-probclass = '3'.
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
i_log_handle = lv_log_handle
i_s_msg = ls_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 log
* -----------------------------------------------------------------
APPEND lv_log_handle TO lt_log_handle.
CALL FUNCTION 'BAL_DB_SAVE'
EXPORTING
i_in_update_task = ' '
i_save_all = ' '
i_t_log_handle = lt_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.
* -----------------------------------------------------------------
IF pv_x = abap_true.
ASSERT 1 = 2.
endif.
COMMIT WORK.
write lv_bool.
2012 Jan 11 4:06 PM
put the flag i_in_update_task = ' X'
this case irrespective of the assert statement it will wait for a update task to occur, when that occurs it will save the log.
if you dont pass the flag, database update will be called but not in update task and your assert statement wont let it reach the commit.
2012 Jan 12 8:08 AM
Thank you for the fast reply,
I tried to set the flag flag i_in_update_task = ' X' too but it seems that doesn't help too. the COMMIT WORK will never be executed when assertion_failed occurs.
Does anybody know an alternative to BAL ? for example writing the logs into a file instead into database?
Regards
Bahjat
2016 Aug 03 9:29 AM
Since this was the first search result for this issue I want to share some solutions from Application Logging in SAP Using ABAP - Code Gallery - SCN Wiki (and the comments, h/t Ben Meijs) which should work in most scenarios:
As of 731:
Legacy handling:
Also I should note another finding: BAL_DB_LOAD only exports a handle if the log is not already loaded yet -> use parameter i_exception_if_already_loaded, catch the exception and use BAL_GLB_SEARCH_LOG to find a recently saved log which is still in memory
2024 Apr 29 8:55 AM
Hi,
I solved my problem regarding your comment. Thanks.
I used parameters (2th +2th_commit) of BAL_DB_SAVE set to abap_true.