Application log is used to generate a log which can be accessed later by the users. Sometimes there would be a situation where we need to generate the log, especially for the Errorenous situation.

Application Log is the rich type log. It contains the information like User, Time, Expiry date of the log. It has also traffic icons for the messages based on the message type.

Log is also getting generated when we run the report in the Background. This is called as the Job Log. But it doesn't have the same formatting as the Application Log. It will also not get generated when you run the report online. So, if we have to generate the Log in the both scenario than we have to use the Application Log.

Important Tcodes:
SLG0 - Create a new Log Object and subobject
SLG1 - Display Application Log
SLG2 - Delete the Application Log

To start generating the custom Application Log than we need to create a new Log object and Subobject. Use the Tcode SLG0 and create a new custom Application Log object.
So, let's see the code snippet to generate the Application Log.
REPORT  zapplog.

*&---------------------------------------------------------------------*
*& This code snippet will show how to generate the Application log
*&   by using some function modules
*&
*&---------------------------------------------------------------------*
*
DATA: lf_obj        TYPE balobj_d,
      lf_subobj     
TYPE balsubobj,
      ls_header     
TYPE balhdri,
      lf_log_handle 
TYPE balloghndl,
      lf_log_number 
TYPE balognr,
      lt_msg        
TYPE balmi_tab,
      ls_msg        
TYPE balmi,
      lt_lognum     
TYPE TABLE OF balnri,
      ls_lognum     
TYPE balnri.
*
* Application Log object & Subobject
lf_obj     = 
'ZTEST_NP'.
lf_subobj  = 
'ZTEST_1'.
*
* Header information for the log
ls_header-object     = lf_obj.
ls_header-subobject  = lf_subobj.
ls_header-aldate     = sy-datum.
ls_header-altime     = sy-uzeit.
ls_header-aluser     = sy-uname.
ls_header-aldate_del = sy-datum + 
1.
*
* Get the Log handle using the header
CALL FUNCTION 'APPL_LOG_WRITE_HEADER'
  
EXPORTING
    
header              = ls_header
  
IMPORTING
    e_log_handle        = lf_log_handle
  
EXCEPTIONS
    object_not_found    = 
1
    subobject_not_found = 
2
    error               = 
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.
*
* Get the next avaliable Log number
CALL FUNCTION 'BAL_DB_LOGNUMBER_GET'
  
EXPORTING
    i_client                 = sy-mandt
    i_log_handle             = lf_log_handle
  
IMPORTING
    e_lognumber              = lf_log_number
  
EXCEPTIONS
    log_not_found            = 
1
    lognumber_already_exists = 
2
    numbering_error          = 
3
    
OTHERS                   = 4.
*
* Fill the Application Log messages.
*   Here we can append our own messages as per our application
*   behaviour
*
* Information message will generate the Green Light
ls_msg-msgty = 
'I'.
ls_msg-msgid = 
'00'.
ls_msg-msgno = 
'398'.
ls_msg-msgv1 = 
'Testing 1234'.
APPEND ls_msg TO lt_msg.
*
* Warning message will generate the Yellow light
ls_msg-msgty = 
'W'.
ls_msg-msgid = 
'00'.
ls_msg-msgno = 
'398'.
ls_msg-msgv1 = 
'Warning Testing 1234'.
APPEND ls_msg TO lt_msg.
*
* Error message will generate the Red Light
ls_msg-msgty = 
'E'.
ls_msg-msgid = 
'00'.
ls_msg-msgno = 
'398'.
ls_msg-msgv1 = 
'Error Testing 1234'.
APPEND ls_msg TO lt_msg.
*
* Write the Log mesages to the memory
CALL FUNCTION 'APPL_LOG_WRITE_MESSAGES'
  
EXPORTING
    object              = lf_obj
    subobject           = lf_subobj
    log_handle          = lf_log_handle
  
TABLES
    messages            = lt_msg
  
EXCEPTIONS
    object_not_found    = 
1
    subobject_not_found = 
2
    
OTHERS              = 3.
IF sy-subrc <> 0.
  
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
          
WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
ENDIF.
*
* write the log message to Database which can be later analyzed
*   from transaction SLG1
MOVE-CORRESPONDING ls_header TO ls_lognum.
ls_lognum-lognumber = lf_log_number.
APPEND ls_lognum TO lt_lognum.
*
CALL FUNCTION 'APPL_LOG_WRITE_DB'
  
EXPORTING
    object                = lf_obj
    subobject             = lf_subobj
    log_handle            = lf_log_handle
  
TABLES
    object_with_lognumber = lt_lognum
  
EXCEPTIONS
    object_not_found      = 
1
    subobject_not_found   = 
2
    internal_error        = 
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.
*
* display the generate log from the memory.
CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'.


Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP MM SD Consultant
Contact : serkurumsal@yandex.com