Create Application Log Using SLG0, SLG1 and SLG2

In this tutorial we will show how to create, view and delete application log using transaction SLG0, SLG1 and SLG2 respectively. The demo program below can be used to populate the application log.

Please note that this type of application log comes handy when we want to add a log to proxy interfaces. Message in this type of application log can be viewed any time.

The following main transactions are used for application log:

  • SLG0 : To create log \’Object\’ and \’Sub Object\’
  • SLG1  : To display the application log entries
  • SLG2  : To delete the application log entries

Step 1 – Create Application Using SLG0:

The first step is to create the application log in transaction \’SLG0\’ before we can populate the log via an ABAP program. In \’SLG0\’ create an \’Object\’ together with its \’Sub Object\’ using the following steps:

  • Create the object \’ZOBJ001\’

\"\"

\"\"

  • Create the sub object \’ZSUBOBJ001\’  (same steps as when creating the object)

\"\"

  • Save

 

Step 2 – Create ABAP program to populate the Application Log:

Please find below a sample program which demonstrate how to populate the application log created in step 1.

REPORT ztestapplog.
*&--------------------------------------------------------------------&*
*& Program Description:                                               &*
*& -----------------------                                            &*
*& This demo program demonstrate how to create application log via    &*
*& transaction code \'SLG0\' and display the log via \'SLG1\'.            &*
*&                                                                    &*
*& Author:  ABAPCOOKBOOK                                              &*
*& Website: www.abapcookbook.com                                      &*
************************************************************************

************************************************************************
* DATA DECLARATIONS                                                    *
************************************************************************
*Constants:
CONSTANTS:
  gc_object                  TYPE bal_s_log-object      VALUE \'ZOBJ001\',
  gc_subobject               TYPE bal_s_log-subobject   VALUE \'ZSUBOBJ001\',
  gc_message_id              TYPE bapiret2-id           VALUE \'V1\',
  gc_msgno1                  TYPE bapiret2-number       VALUE \'091\',
  gc_success                 TYPE c                     VALUE \'S\',
  gc_error                   TYPE c                     VALUE \'E\',
  gc_detail_level1           TYPE c                     VALUE \'1\',
  gc_problem_class_important TYPE c                     VALUE \'2\',
  gc_probclass_medium        TYPE bal_s_msg-probclass   VALUE \'3\',
  gc_probclass_low           TYPE bal_s_msg-probclass   VALUE \'4\',
  gc_problem_class_other     TYPE c                     VALUE \'\'.

*Variables:
DATA:
  gv_log_handle TYPE balloghndl,
  gv_msgv1      TYPE symsgv.


************************************************************************
* CREATE LOG.                                                          *
************************************************************************
*IMPORTANT: Make sure the object \'ZOBJ001\' and sub-object \'ZOBJ001\' has
*           been created in transaction \'SLG0\'. Log can be deleted via
*           transaction \'SLG2\'.

*           The log can be viewed via transaction \'SLG1\' using
*           the object \'ZOBJ001\' and sub-object \'ZOBJ001\'.

*Create the application log.
PERFORM f_create_bal_log USING gv_log_handle.


************************************************************************
* ADD MESSAGE TO LOG.                                                  *
************************************************************************
*This is a test condition to add a message.
IF 1 NE 2.

* Typecasting the date.
  gv_msgv1 = sy-datum.

* Add a message to the application log.
* Message: \'Date & is not valid\'
  PERFORM f_add_msg_to_log USING  gv_log_handle                 \" Log Handler
                                  gc_problem_class_important    \" Class Problem Level
                                  gc_error                      \" Error Message
                                  gc_message_id                 \" Message Class
                                  gc_msgno1                     \" Message Number
                                  gv_msgv1                      \" Message Dynamic Content(&) 1
                                  space                         \" Message Dynamic Content(&) 2 (Empty)
                                  space                         \" Message Dynamic Content(&) 3 (Empty)
                                  space                         \" Message Dynamic Content(&) 4 (Empty)
                                  gc_detail_level1.             \" Application Detail Level

ENDIF.

************************************************************************
* SAVE LOG.                                                            *
************************************************************************
*At the end of all operation, save the application log.
*The log can be viewed via transaction \'SLG1\' using
*the object \'ZOBJ001\' and sub-object \'ZOBJ001\'.
PERFORM f_save_bal_log USING gv_log_handle.


************************************************************************
* ROUTINES:                                                            *
************************************************************************

*----------------------------------------------------------------------*
* Sub routine to create the application log for error logging          *
* purposes.                                                            *
*----------------------------------------------------------------------*
FORM f_create_bal_log USING pe_log_handle TYPE balloghndl.
*----------------------------------------------------------------------*
* Local data declarations.
* Structures.
  DATA:
    lst_log         TYPE bal_s_log.

* Defining some header data of the application log.
  lst_log-aldate    = sy-datum.
  lst_log-altime    = sy-uzeit.
  lst_log-aluser    = sy-uname.
  lst_log-alprog    = sy-repid.
  lst_log-object    = gc_object.
  lst_log-subobject = gc_subobject.

* Creationg the application log.
  CALL FUNCTION \'BAL_LOG_CREATE\'
    EXPORTING
      i_s_log      = lst_log
    IMPORTING
      e_log_handle = pe_log_handle
    EXCEPTIONS
      OTHERS       = 1.
* Not necessary to cater for this exception.

ENDFORM.


*----------------------------------------------------------------------*
* Sub routine to add a message to the processing log.                  *
*----------------------------------------------------------------------*
FORM f_add_msg_to_log USING pi_log_handle TYPE balloghndl
                            pi_probclass TYPE bal_s_msg-probclass
                            pi_msgty TYPE bal_s_msg-msgty
                            pi_msgid TYPE bal_s_msg-msgid
                            pi_msgno TYPE bal_s_msg-msgno
                            pi_msgv1 TYPE bal_s_msg-msgv1
                            pi_msgv2 TYPE bal_s_msg-msgv2
                            pi_msgv3 TYPE bal_s_msg-msgv3
                            pi_msgv4 TYPE bal_s_msg-msgv4
                            pi_detlevel TYPE bal_s_msg-detlevel.
*----------------------------------------------------------------------*
* Structures:
  DATA:
    lst_ballog_msg         TYPE bal_s_msg.

* Defining data of message for the application log.
  lst_ballog_msg-probclass = pi_probclass.
  lst_ballog_msg-msgty     = pi_msgty.
  lst_ballog_msg-msgid     = pi_msgid.
  lst_ballog_msg-msgno     = pi_msgno.
  lst_ballog_msg-msgv1     = pi_msgv1.
  lst_ballog_msg-msgv2     = pi_msgv2.
  lst_ballog_msg-msgv3     = pi_msgv3.
  lst_ballog_msg-msgv4     = pi_msgv4.
  lst_ballog_msg-detlevel  = pi_detlevel.

* Adding this message to log file.
  CALL FUNCTION \'BAL_LOG_MSG_ADD\'
    EXPORTING
      i_log_handle  = pi_log_handle
      i_s_msg       = lst_ballog_msg
    EXCEPTIONS
      log_not_found = 0
      OTHERS        = 1.
* Not necessary to cater for these exceptions.

ENDFORM.


*----------------------------------------------------------------------*
* Sub routine to save the application log to database.                 *
*----------------------------------------------------------------------*
FORM f_save_bal_log USING pi_log_handle TYPE balloghndl.
*----------------------------------------------------------------------*
* Internal Tables:
  DATA:
    lt_loghandle TYPE bal_t_logh.

* Append the log handler to an internal table for saving purposes.
  APPEND pi_log_handle TO lt_loghandle.

* Saving the application log.
  CALL FUNCTION \'BAL_DB_SAVE\'
    EXPORTING
      i_t_log_handle   = lt_loghandle
    EXCEPTIONS
      log_not_found    = 1
      save_not_allowed = 2
      numbering_error  = 3
      error_message    = 4
      OTHERS           = 5.
* Not necessary to cater for these exceptions.

ENDFORM.                    \"F_SAVE_BAL_LOG

Step 3 – Displaying application log in SLG1:

Execute the above program, an entry will be added to the application log. This log entry can be viewed via transaction \’SLG1\’.

In transaction, please enter the object \’ZOBJ001\’ and sub object \’ZSUBOBJ001\’ (created in step 1) and execute.

\"\"

The following log will be displayed.

\"\"

 

Step 4 – Delete log entries in SLG2:

Finally, log entries can be deleted via transaction code \’SLG2\’.

\"\"

 

Voila 🙂 hope this helps. You can also refer to this tutorial on how to display application log automatically in a program.