Manage SAP SLG1 – Application Log in ABAP with samples

Connection1

Sap Application Log with transactions SAP SLG0 and SAP SLG1. The First part covers the main SAP Application Log using the standard SAP Application Log SLG0 and SLG1.

The Second part details how you can create, manage SAP Log using ABAP with the different options.

What’s SAP Application Logging ?

Application logging records the progress of the execution of an application so that you can reconstruct it later if necessary. Whereas the system log records system events, you can use the application log to record application-specific events.

Use the transaction SLG0 to define entries for your own applications in the application log.

Use the transaction SLG1 to analyze the application log.
The application log is a table structure consisting of several tables.

Applications write their entries to these tables using SAP function modules. (These modules conform to the SAP authorization concept.)

You can also find out who accessed these function modules over a where-used list by using the report RSFKT100 (function group: SLG0)

source: Application Logging

SAP SLG1 : Display SAP Application Log

Let’s start by Display SAP Application Log. The main transaction to display SAP log is SLG1.

Display Log using SLG1

Within the bar menu, launch the SAP Tcode for Log SLG1.

You can select Analyse Application Log by Object, Subobjest, External ID, Time , User, Transaction Codes, Program …

Here a screenshot of SLG1 Filter screen :

Pop Up Show SAP Log Application

Within any other ABAP program, you can use Pop Up in order to display SAP Log Application.

Check this post for more detail ABAP PopUp: Types and Samples codes > ABAP PopUps Programs Sample

Create SAP Application Log (SLG0)

SLG0 : Create Log Object and SubObject

Create/Maintain SAP Application Log Objects and subobjects using tcode SLG0. You should define an Object and SubObjets.

Theses Structures will help you identify easily your generated Application Log in SLG1 from the standard generated Log by others programs.

SAP LOG Object SLG0

Add log in SAP SLG1 within ABAP Program

In this part, we will detail how to add log to SAP application Log using ABAP Code and so our log will be displayed in SLG1.

The Steps to create SLG1 log are:

  • Initialize Log (using BAL_LOG_CREATE)
  • Fill Log with free text or ?Message BAPIRET
  • Save and close SAP SLG1?Application Log

Let’s detail each step with a full ABAP Snipper

Initialize log 😕Use of BAL_LOG_CREATE

In order to initialize Log, you should use the standard function BAL_LOG_CREATE.

Pass the Object and the SubOject, the date, the user and potentially an external Number ( can be Order Id, or Partner Id … ). The Output of BAL_LOG_CREATE will be a reference number to Log Handle.

*IV_OBJ_LOG    TYPE BALOBJ_D  DEFAULT
*IV_SUB_OBJ_LOGTYPE BALSUBOBJ OPTIONAL
*IV_USER        TYPE SYUNAME  DEFAULT SY-UNAME
*IV_BAPI        TYPE RS38L_FNAM OPTIONAL
*IV_EXTERNAL    TYPE BALNREXT OPTIONAL
*EV_LOG_HANDLE  TYPE BALLOGHNDL
*EV_SUBRC      TYPE SYSUBRC

  DATA: ls_log      TYPE bal_s_log.
  DATA: lv_msgtext  TYPE baltmsg.

  CLEAR ev_log_handle.

  " Create the Log
  ls_log-object    = iv_obj_log ." 'APPLOG'.
  ls_log-subobject = iv_sub_obj_log. " 'SUBAPP'.
  ls_log-aldate    = sy-datum.
  ls_log-altime    = sy-uzeit.
  ls_log-aluser    = iv_user ." sy-uname.
  ls_log-extnumber = iv_external.

  CALL FUNCTION 'BAL_LOG_CREATE'
    EXPORTING
      i_s_log                 = ls_log
    IMPORTING
      e_log_handle            = ev_log_handle
    EXCEPTIONS
      log_header_inconsistent = 1
      OTHERS                  = 2.
  IF sy-subrc <> 0.
    ev_subrc = 1.
    RETURN.
  ENDIF.

Add Free text to SAP SLG1?Log

Once the Application Log is initialized, the next step is to fill information within the log.
Let’s start by the easy way on how to add free text to SAP SLG1 Log.

Simply use the standard function BAL_LOG_MSG_ADD with the log handle and your free text fited into BAPIRET2 structure.

You don’t need to create an new message class or number, just use the standard message class and number for Application Log (BL 001).

*IV_LOG_HANDLE    TYPE BALLOGHNDL OPTIONAL
*IV_TEXT          TYPE C
*IV_MSG_TYPE      TYPE SYMSGTY
*IV_LEVEL        TYPE BALLEVEL OPTIONAL
*IS_BAPIRET2      TYPE BAPIRET2 OPTIONAL

  DATA:
      BEGIN OF ls_string,
      part1   TYPE symsgv,
      part2   TYPE symsgv,
      part3   TYPE symsgv,
      part4   TYPE symsgv,
    END OF ls_string.

  DATA: ls_msg TYPE bal_s_msg.

  ls_string = iv_text.

  ls_msg-msgty = iv_msg_type.
  ls_msg-msgid = 'BL'.
  ls_msg-msgno = '001'.

  ls_msg-msgv1     = ls_string-part1.
  ls_msg-msgv2     = ls_string-part2.
  ls_msg-msgv3     = ls_string-part3.
  ls_msg-msgv4     = ls_string-part4.
  

  CALL FUNCTION 'BAL_LOG_MSG_ADD'
    EXPORTING
      i_log_handle  = iv_log_handle
      i_s_msg       = ls_msg
    EXCEPTIONS
      log_not_found = 0
      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.

Add Message Table BAPIRET2 to ?the?SAP SLG1 log

Sometimes, you need to log all BAPI Message to Application Log. In this case, loop to your BAPIRETTAB message and for each line in the BAPI return table, add a new line to Application Log using the BAL_LOG_MSG_ADD.

  " IV_LOG_HANDLE TYPE BALLOGHNDL OPTIONAL
  " IV_LEVEL     TYPE BALLEVEL OPTIONAL
  " ET_BAPIRET   TYPE BAPIRETTAB OPTIONAL


  DATA: ls_msg TYPE bal_s_msg.
  FIELD-SYMBOLS:  LIKE LINE OF et_bapiret.

  LOOP AT et_bapiret ASSIGNING  .
    CLEAR ls_msg.

    ls_msg-msgty = -type.
    ls_msg-msgid = -id.
    ls_msg-msgno = -number.
    ls_msg-msgv1 = -message_v1.
    ls_msg-msgv2 = -message_v2.
    ls_msg-msgv3 = -message_v3.
    ls_msg-msgv4 = -message_v4.

    " Add level
    ls_msg-detlevel = iv_level.

    CALL FUNCTION 'BAL_LOG_MSG_ADD'
      EXPORTING
        i_log_handle  = iv_log_handle
        i_s_msg       = ls_msg
      EXCEPTIONS
        log_not_found = 0
        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.
  ENDLOOP.

Save and close SAP SLG1?Application Log

Once all the information is added to SLG1 Log, you have to SAVE the Application Log using BAL_DB_SAVE. It will commit modification to database.

Now you can check the SLG1 transaction Code for Log and you will find your new log.

  " IV_LOG_HANDLETYPE BALLOGHNDL
  " EV_SUBRC  TYPE SYSUBRC

  DATA: lt_log_handle   TYPE bal_t_logh,
        lt_log_num      TYPE bal_t_lgnm.

  INSERT iv_log_handle INTO lt_log_handle INDEX 1.
  CALL FUNCTION 'BAL_DB_SAVE'
    EXPORTING
      i_client         = sy-mandt
      i_save_all       = ' '
      i_t_log_handle   = lt_log_handle
    IMPORTING
      e_new_lognumbers = lt_log_num
    EXCEPTIONS
      log_not_found    = 1
      save_not_allowed = 2
      numbering_error  = 3
      OTHERS           = 4.
  IF sy-subrc <> 0.
    ev_subrc = sy-subrc.
  ENDIF.