ABAP logging made easy using simple logger utility class ZCL_LOGGER

In the last post, Application logging in ABAP. I talked about application logging.

It's an excellent utility but it's cumbersome from a developer's point of you. Especially if you work or have worked with languages like JavaScript where logging is as easy as Console.log("your text").

to add the same level of ease, I created a utility class that makes logging easy.

Starting a log is as easy as instantiating the object. Below is the example :

DATA:
o_logger TYPE REF TO zcl_logger.
DATA:
s_log TYPE bal_s_log.
DATA:
v_log_handle TYPE balloghndl.
"With ZCL_LOGGER
CREATE OBJECT o_logger
EXPORTING
iv_object = 'Z_BAL_TST'
iv_sub_object = 'TEST_SOBJ'.
IF o_logger IS NOT BOUND.
MESSAGE e000(db) WITH 'Error Initiating Log'.
ENDIF.
"With Out ZCL_LOGGER
* Create the Log
s_log-object = 'Z_BAL_TST'.
s_log-subobject = 'TEST_SOBJ'.
s_log-aldate = syst-datum.
s_log-altime = syst-uzeit.
s_log-aluser = syst-uname.
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_s_log = s_log
IMPORTING
e_log_handle = v_log_handle
EXCEPTIONS
log_header_inconsistent = 1
OTHERS = 2.
IF sy-subrc <> 0.
MESSAGE e000(db) WITH 'Error Initiating Log'.
ENDIF.

Adding messages to the log becomes a single line code:

DATA:
o_logger TYPE REF TO zcl_logger.
DATA:
s_msg TYPE bal_s_msg.
"Adding error message to LOG
"With ZCL_LOGGER
o_logger->add_log_e( 'No Data found for Input' ).
"Without ZCL_LOGGER
CLEAR s_msg.
s_msg-msgty = 'E'.
s_msg-msgid = 'DB'.
s_msg-msgno = '000'.
s_msg-msgv1 = 'No Data found for Input'.
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
i_log_handle = v_log_handle
i_s_msg = s_msg
EXCEPTIONS
log_not_found = 1
msg_inconsistent = 2
log_is_full = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE e000(db) WITH 'Error while adding message to Log'.
ENDIF.

Here is the snippet to save the log.

DATA:
o_logger TYPE REF TO zcl_logger.
DATA:
v_log_handle TYPE balloghndl.
DATA:
t_log_handle TYPE bal_t_logh.
"With ZCl_logger
"You log messages are already saved... Nothing to do..
"Without ZCL_logger..
INSERT v_log_handle INTO t_log_handle INDEX 1.
CALL FUNCTION 'BAL_DB_SAVE'
EXPORTING
i_client = sy-mandt
i_save_all = ' '
i_t_log_handle = t_log_handle
IMPORTING
e_new_lognumbers = t_log_num
EXCEPTIONS
log_not_found = 1
save_not_allowed = 2
numbering_error = 3
OTHERS = 4.
IF sy-subrc <> 0.
MESSAGE e000(db) WITH 'Error while Saving Log to DB' .
ELSE.
MESSAGE s000(db) WITH 'Log Generated'.
ENDIF.

Notice how you don't have to worry about saving the log messages.. because the class saves your messages the moment you add them, so you don't have to worry about collecting your logs and making sure the save happens in all scenarios.

Now I will take the same code snippet that I shared in the last post and rewrite it using the logger class to show how convenient it is to use the utility.

REPORT sy-repid.
DATA:
s_msg TYPE bal_s_msg.
DATA:
v_vbeln TYPE vbeln_va.
DATA:
t_vbap TYPE STANDARD TABLE OF vbap.
DATA:
o_salv TYPE REF TO cl_salv_table,
o_logger TYPE REF TO zcl_logger.
SELECT-OPTIONS:
s_vbeln FOR v_vbeln.
INITIALIZATION.
CREATE OBJECT o_logger
EXPORTING
iv_object = 'Z_BAL_TST'
iv_sub_object = 'TEST_SOBJ'.
IF o_logger IS NOT BOUND.
MESSAGE e000(db) WITH 'Error Initiating Log'.
ENDIF.
START-OF-SELECTION.
IF 0 <> o_logger->add_log_s( `Fetching first 100 records from VBAP` ) .
MESSAGE e000(db) WITH 'Error while adding message to Log'.
ENDIF.
SELECT *
FROM vbap
INTO TABLE t_vbap
UP TO 100 ROWS
WHERE vbeln IN s_vbeln.
IF syst-subrc <> 0.
IF 0 <> o_logger->add_log_e( `No Data found for Input` ) .
MESSAGE e000(db) WITH 'Error while adding message to Log'.
ENDIF.
RETURN.
ELSE.
CLEAR s_msg.
s_msg-msgty = 'W'.
s_msg-msgid = 'DB'.
s_msg-msgno = '000'.
s_msg-msgv1 = '100 records Data found'.
s_msg-msgv2 = 'There could be more'.
IF 0 <> o_logger->add_log_w( is_msg = s_msg ) .
MESSAGE e000(db) WITH 'Error while adding message to Log'.
ENDIF.
APPEND s_msg TO t_msg.
ENDIF.
IF 0 <> o_logger->add_log_s( `Starting Display` ) .
MESSAGE e000(db) WITH 'Error while adding message to Log'.
ENDIF.
cl_salv_table=>factory( IMPORTING r_salv_table = o_salv " Basis Class Simple ALV Tables
CHANGING t_table = t_vbap
).
o_salv->display( ).

Here is the link to the source: simple ABAP logger

Use the utility as it is or modify it to suit your needs. As always, hope you like the post and utility and use it in your day to day work.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET

Use CL_BCS library to send email with ABAP