How to log events in ABAP using application log with additional (CONTEXT) fields

SAPIn this step-by-step guide I’d like to show you how to create an application log which can be easily extended with your extra fields containing additional information. To maintain and access application logs using SAP standard you can use transaction codes SLG0 (add/edit/delete application log) and SLG1 (standard application log viewer).

Create application log

We will create our application log using transaction SLG0 and we will call it ie ZTEST.

Application log - extra fields 01

Now we create a Subobejct called ie EXT_FIELDS

Application log - extra fields 02

Now the application log storage area is prepared so we can start creating logs.

DDIC Structure for extra fields

Run transaction SE11 and create new structure which will hold the additional data we would like to display in application log – one field in the structure per column in the appication log.

Application log - extra fields 03

Application log - extra fields 04

Application log - extra fields 05

ABAP program

Run transaction SE38 and create new program called ie Z_APPLOG_EXT_FIELDS. 

Application log - extra fields 06

1. Data definitions

DATA:
  gs_log TYPE bal_s_log,
  gs_msg TYPE bal_s_msg,
  gt_handle TYPE bal_t_logh,
  gs_handle LIKE LINE OF gt_handle,
  gs_context TYPE zca_s_appllog,         " type of your z-structure
  l_s_display_profile TYPE bal_s_prof,
  l_s_fcat TYPE bal_s_fcat.

2. Create new log in log storage area

* Create log entry
 gs_log-object    = 'ZTEST'. " All available logs are in BALSUB table, use
                             " TCode SLG0 for maintenance
 gs_log-subobject = 'EXT_FIELDS'.
 gs_log-extnumber = 'TEST_APPLOG_EXT_FIELDS'.
 gs_log-aluser    = sy-uname.
 gs_log-alprog    = sy-repid.

* Create new log in logs storage area
 CALL FUNCTION 'BAL_LOG_CREATE'
   EXPORTING
     i_s_log      = gs_log
   IMPORTING
     e_log_handle = gs_handle.
 APPEND gs_handle TO gt_handle.

3. Adding messages

* Adding random message
 CLEAR gs_msg.
 gs_msg-msgty = 'S'.
 gs_msg-msgid = 'BL'.
 gs_msg-msgno = '003'.
 gs_msg-msgv1 = 'This is example of additional fields usage'.
 gs_msg-msgv2 = 'in application log - see additional columns'.

* Add message context
 gs_context-plant = '2021'.
 gs_context-location = 'PRAGUE'.

 gs_msg-context-value = gs_context.
 gs_msg-context-tabname = 'ZCA_S_APPLLOG'.

* Save message
 CALL FUNCTION 'BAL_LOG_MSG_ADD'
   EXPORTING
     i_s_msg      = gs_msg
     i_log_handle = gs_handle.

4. Save log

* The above will most probably be done in a loop so more messages 
* are stored
* Once we have all messages collected we can save the completed log to DB
* This log can be later displayed with tcode SLG1
 CALL FUNCTION 'BAL_DB_SAVE'
   EXPORTING
     i_t_log_handle = gt_handle.

5. Display log

* Get standard display profile
 CALL FUNCTION 'BAL_DSP_PROFILE_SINGLE_LOG_GET'
   IMPORTING
     e_s_display_profile = l_s_display_profile.

* Modify the field catalog with the extra fields
* we would like to have on output

* Add the "PLANT" field
 l_s_fcat-ref_table = 'ZCA_S_APPLLOG'.
 l_s_fcat-ref_field = 'PLANT'.
 l_s_fcat-col_pos = 100.
 APPEND l_s_fcat TO l_s_display_profile-mess_fcat.

* Add the "LOCATION" field
 l_s_fcat-ref_table = 'ZCA_S_APPLLOG'.
 l_s_fcat-ref_field = 'LOCATION'.
 l_s_fcat-col_pos = 99.
 APPEND l_s_fcat TO l_s_display_profile-mess_fcat.

* Display log
* This is possible even if the log is not save in the DB
 CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
   EXPORTING
     i_t_log_handle      = gt_handle
     i_s_display_profile = l_s_display_profile.

6. Output

Application log - extra fields 07

Appliaction log in SLG1

Application log - extra fields 08

Be aware that in SLG1 you won’t be able to see the additional columns. So in case you have to code any logic using the additional columns, always create new Z-transaction for displaying the application log with additional fields as described in the example above.

Application log - extra fields 09

 

2 thoughts on “How to log events in ABAP using application log with additional (CONTEXT) fields

Leave a Reply