Using business application log framework (BAL)

Log and monitor messages for your application

The Application Log is a tool for collecting messages, saving, reading, and deleting logs in the database and displaying logs. These messages are not to be output individually (with the ABAP commands MESSAGE and Raise), they are to be collected and displayed later as a log.


Message attributes
  1. Message type - this parameter determines the color of the icon when viewing the log
  2. Importance level - records are automatically divided into categories based on this attribute
  3. Sorting criterion - a word of three characters, a sign for grouping and sorting messages
  4. Detail level - a number from 1 to 9 inclusive, can participate in filters
  5. Additional information - an arbitrary set of parameters and a link to a function module or procedure for displaying it. Can be used, for example, to attach the HTTP header of the server response to a data transfer error message, or to invoke a transaction with parameters 
Functions
  • Logs are displayed using function module BAL_DSP_LOG_DISPLAY. This function module receives a profile (Structure BAL_S_PROF) which describes how this the presentation of logs should look like.
    • BAL_DSP_PROFILE_SINGLE_LOG_GET: the 'standard' profile to display one log
    • BAL_DSP_PROFILE_STANDARD_GET: The 'standard' profile to display many logs.
    • BAL_DSP_PROFILE_NO_TREE_GET: Presentation of a log without a navigation tree next to it.
    • BAL_DSP_PROFILE_POPUP_GET: Presentation of a log in a popup
    • BAL_DSP_PROFILE_DETLEVEL_GET: The messages are inserted into the navigation tree.
  • Application Log header is created using BAL_LOG_CREATE with the main parameters:
    • OBJECT, SUBOBJECT: The Application Log is used by various applications. Every log has the attributes OBJECT and SUBOBJECT to help applications to find their logs efficiently.
    • EXTNUMBER: The external ID in the log header (CHAR100) is a free text description of the log by the application.
    • DATE_DEL, DEL_BEFORE: Logs have an expiry date (DATE_DEL) after which they can be deleted, and a flag (DEL_BEFORE) which explicitly forbids deletion before this date. 
    • The function module BAL_LOG_CREATE returns the log handle (LOG_HANDLE, CHAR22). The LOG_HANDLE is a GUID (globally unique identifier) that uniquely identifies a log. 
  • Using Function Module 'BAL_LOG_MSG_ADD', we can add a message into the application log. This message is put in the log identified by the log handle GV_LOG_HANDLE, which is mostly the T100 information (message type, work area, message number, the 4 message variables).
  • Put exception in log using FM BAL_LOG_EXCEPTION_ADD.
  • Using Function Module 'BAL_DB_SAVE, we can save all memory data in the database. The importing parameter I_SAVE_ALL should be set as 'X'.
  • Find logs in the database with the function module BAL_DB_SEARCH.
  • Load logs from the database using the function module BAL_DB_LOAD.
  • Delete logs from the database using function module BAL_DB_DELETE.

Choose transaction SLG2. You go to the dialog box Application Log: Delete Obsolete Logs
The archiving object BC_SBAL is used to archive application logs. SLG0 and SLG1.

We can affect the program flow at various events in Application Log with callback routines. The program SBAL_CALLBACK is an example (and parameterization template).

To find more standard SAP BAL log demo, type ‘SBAL_DEMO_*’ in SE38 and press F4. Detailed documentation on all function modules, and so on, can be found in the system by executing the program SBAL_DOCUMENTATION.


Modal BAL 

CLASS lcl_logger DEFINITION.
  PUBLIC SECTION.
    DATA: handle TYPE balloghndl.
    METHODS: constructor IMPORTING it_messages TYPE Z

  PRIVATE SECTION.
    METHODS:
      log_create,
      log_show,
      message_add IMPORTING it_messages TYPE Z.
ENDCLASS.

CLASS lcl_logger IMPLEMENTATION.


  METHOD constructor.

    IF it_messages IS NOT INITIAL.
      log_create( ).
      message_add( it_messages ).
      log_show( ).
    ENDIF.
  ENDMETHOD.

  METHOD message_add.
    DATA: ls_msg     TYPE bal_s_msg,
          lv_message TYPE c LENGTH 125.

    DATA(lt_messages) = it_messages.
    LOOP AT lt_messages ASSIGNING FIELD-SYMBOL(<ls_message>).

      CALL FUNCTION 'BAL_LOG_MSG_ADD_FREE_TEXT'
        EXPORTING
          i_log_handle     = handle
          i_msgty          = 'I'
          i_text           = <ls_message>-text
        EXCEPTIONS
          log_not_found    = 1
          msg_inconsistent = 2
          log_is_full      = 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.
    ENDLOOP.
  ENDMETHOD.
  METHOD log_create.

    DATA:
      l_log_handle TYPE balloghndl,
      l_s_log      TYPE bal_s_log,
      l_s_msg      TYPE bal_s_msg,
      l_msgno      TYPE symsgno.

    l_s_log-extnumber  = 'Log'.
    CALL FUNCTION 'BAL_LOG_CREATE'
      EXPORTING
        i_s_log      = l_s_log
      IMPORTING
        e_log_handle = 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.
    ENDIF.

  ENDMETHOD.

  METHOD log_show.
    DATA: l_s_display_profile TYPE bal_s_prof.

    CALL FUNCTION 'BAL_DSP_PROFILE_POPUP_GET'
      IMPORTING
        e_s_display_profile = l_s_display_profile
      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.

    l_s_display_profile-disvariant-report = sy-repid.
    l_s_display_profile-disvariant-handle = 'LOG'.

    CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
      EXPORTING
        i_s_display_profile = l_s_display_profile
      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.


    CALL FUNCTION 'BAL_LOG_REFRESH'
      EXPORTING
        i_log_handle  = handle
      EXCEPTIONS
        log_not_found = 1
        OTHERS        = 2.
    IF sy-subrc <> 0.
      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
                     WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
    ENDIF.

  ENDMETHOD.
ENDCLASS.

Logging Application Jobs

  DATA lv_log_handle TYPE balloghndl.

CALL FUNCTION 'BAL_LOG_CREATE'
  EXPORTING
    i_s_log                 = ...
  IMPORTING
    e_log_handle            = lv_log_handle
 
  EXCEPTIONS
    log_header_inconsistent = 1
    OTHERS                  = 2.
 ...

IF sy-batch = abap_true.
  CALL FUNCTION 'BP_ADD_APPL_LOG_HANDLE'
    EXPORTING
      loghandle = lv_log_handle.
ENDIF.

Comments