Application Log in ABAP

Suppose you are uploading a file to SAP system. You want to validate each and every entry and all the errors should be displayed to the user as log at the end. For this purpose you can make use of SAP standard function modules.

SAP has provided the following function modules to create an application log. There are many other function modules available to work with application logs.

Function module Use
BAL_LOG_CREATE Create log with header data
BAL_LOG_MSG_ADD Put message in log
BAL_LOG_EXCEPTION_ADD Put exception in log
BAL_DSP_LOG_DISPLAY Display messages in memory

Let us see a simple example how the application log function modules could be used to create a log. The demo program uses the following steps.

  • A log is created with BAL_LOG_CREATE.
  • Messages are sent to this log using BAL_LOG_MSG_ADD
  • Finally the log is displayed with BAL_DSP_LOG_DISPLAY.
  • Nothing is saved on the database.
REPORT zdemo MESSAGE-ID e4.

DATA: g_s_log TYPE bal_s_log,
      g_dummy TYPE c.

*&---------------------------------------------------------------------*
*& START-OF-SELECTION.
*&---------------------------------------------------------------------*
START-OF-SELECTION.
* Create a log where all messages should be added to
  g_s_log-extnumber = 'Application Log Demo'.
  g_s_log-aluser    = sy-uname.
  g_s_log-alprog    = sy-repid.

* Create a log
  CALL FUNCTION 'BAL_LOG_CREATE'
    EXPORTING
      i_s_log = g_s_log
    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.

*Add some dummy messages
  MESSAGE e000 WITH 'Message No 1' INTO g_dummy.
  PERFORM msg_add USING '2'.

  MESSAGE w000 WITH 'Message No 2' INTO g_dummy.
  PERFORM msg_add USING '1'.

  MESSAGE s000 WITH 'Message No 3' INTO g_dummy.
  PERFORM msg_add USING '3'.

* Display log file
  CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
    EXCEPTIONS
      OTHERS = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE 'S' NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.
*&---------------------------------------------------------------------*
* FORM MSG_ADD
*&---------------------------------------------------------------------*
FORM msg_add USING value(i_probclass) TYPE bal_s_msg-probclass.
  DATA: l_s_msg TYPE bal_s_msg.

  l_s_msg-msgty     = sy-msgty.
  l_s_msg-msgid     = sy-msgid.
  l_s_msg-msgno     = sy-msgno.
  l_s_msg-msgv1     = sy-msgv1.
  l_s_msg-msgv2     = sy-msgv2.
  l_s_msg-msgv3     = sy-msgv3.
  l_s_msg-msgv4     = sy-msgv4.
  l_s_msg-probclass = i_probclass.

* Add this message to log file
  CALL FUNCTION 'BAL_LOG_MSG_ADD'
    EXPORTING
      i_s_msg       = l_s_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.

ENDFORM.                    "MSG_ADD

Output

abap-application-log