Creating a Custom Application Log for SLG1

Creating a Custom Application Log for SLG1

SAP provides the ability to view logs for various programs (objects) using transaction SLG1, provided such logging has been implemented in the respective application (object).

Few people know that this functionality can be customized. By "customized," I mean creating your own log for a specific application. This could be a standard report executed via transaction SE38, where a log is generated upon encountering an error (though logging can be done even without errors). It could also be a WebDynpro application or an FPM process/form. These logs allow you to monitor your applications for later analysis or troubleshooting, depending on what you write to them.

Creating a Log

What needs to be done to create your own log?
First, create a custom log object using transaction SLG0.

Next, create a subobject for it.

If needed, you can create as many subobjects as required. Save your changes.

Now run transaction SLG1 and select the newly created object/subobject. An informational message like this will appear:

To view any log, you must first generate it. To do this, you need to write some ABAP code. Below is a simple example—nothing fancy—just to demonstrate how this structure can work. You can also find a lot of information on this topic on SAP Community. Nonetheless:

See: Create application log

Before jumping into the code, let’s create the message that will be written to the new log. You can do this using transaction SE91.

REPORT  ZSLG1_DEMO.
 
 
DATA:  g_s_log               TYPE bal_s_log,
      ls_log_handle TYPE  balloghndl,
      ls_msg TYPE BAL_S_MSG,
      li_log_handle type BAL_T_LOGH.
 
 
 
g_s_log-object = 'ZDEMO'.
g_s_log-SUBOBJECT = 'ZDEMO'.
g_s_log-aluser = sy-uname.
g_s_log-alprog  = sy-repid.
 
*  open log
 
CALL FUNCTION 'BAL_LOG_CREATE'
  EXPORTING
    i_s_log                  = g_s_log
  IMPORTING
    e_log_handle             = ls_log_handle
  EXCEPTIONS
    log_header_inconsisstent = 1
    OTHERS                   = 2.
 
IF sy-subrc EQ 0.
  ls_msg-msgty = 'E'.
  ls_msg-MSGID = 'ZDEMO_MESSGE_CLASS'.
  ls_msg-msgno = '001'.
 
* add
 
  CALL FUNCTION 'BAL_LOG_MSG_ADD'
    EXPORTING
      i_log_handle     = ls_log_handle
      i_s_msg          = ls_msg
    EXCEPTIONS
      log_not_found    = 1
      msg_inconsistent = 2
      log_is_full      = 3
      OTHERS           = 4.
  IF sy-subrc NE 0.
  ENDIF.
  INSERT ls_log_handle INTO TABLE li_log_handle.
 
*  save
 
  CALL FUNCTION 'BAL_DB_SAVE'
    EXPORTING
      i_client         = sy-mandt
      i_save_all       = ' '
      i_t_log_handle   = li_log_handle
    EXCEPTIONS
      log_not_found    = 1
      msg_inconsistent = 2
      log_is_full      = 3
      OTHERS           = 4.
 
  if sy-subrc eq 0.
    REFRESH: li_log_handle.
    ENDIF.
ENDIF.

Run the program you just created, and then launch transaction SLG1 to view the generated application log using the previously defined objects.

Deleting Logs

All logs should be cleaned up periodically—and application logs are no exception. For more information on log deletion, refer to SAP Note 451706 - Deletion of logs of the application logVersion.

In short, logs can be deleted using transaction SLG2 or by executing program SBAL_DELETE.