SAP ABAP LOG

From SapWiki

Los log de aplicación se crean en la transacción SLG0 y se ven en la SLG1

Ejemplo de programa que graba mensajes en log

FUNCTION ZSD_MESSAGE_LOGGING.
*"----------------------------------------------------------------------
*"*"Interfase local
*"  IMPORTING
*"     REFERENCE(I_LOG_OBJECT) TYPE  BALOBJ_D
*"     REFERENCE(I_LOG_OBJECT_INF) TYPE  BALSUBOBJ
*"     REFERENCE(I_EXTNUMBER) TYPE  STRING
*"     REFERENCE(I_TCOD) TYPE  BALTCODE OPTIONAL
*"     REFERENCE(I_PROG) TYPE  BALPROG OPTIONAL
*"  TABLES
*"      T_LOG_MESSAGE STRUCTURE  BDCMSGCOLL
*"  EXCEPTIONS
*"      LOG_HEADER_INCONSISTENT
*"      LOGGING_ERROR
*"----------------------------------------------------------------------
 DATA:
 l_log_handle TYPE balloghndl,
 l_s_log TYPE bal_s_log,
 l_dummy type string,
 l_ext_no type bal_s_log-extnumber,
 l_s_mdef TYPE bal_s_mdef.
 DATA lt_log_handle TYPE bal_t_logh.
 if T_LOG_MESSAGE[] is not initial.
   l_s_log-object = I_LOG_OBJECT.
   l_S_LOG-subobject  = I_LOG_OBJECT_inf.
   l_ext_no = I_EXTNUMBER.
   l_s_log-extnumber = l_ext_no.
   l_S_LOG-ALUSER     = SY-UNAME.
   l_S_LOG-ALPROG     = i_prog.
   l_S_LOG-aldate     = sy-datum.
   l_S_LOG-altime     = sy-uzeit.
   l_S_LOG-aluser     = sy-uname.
   l_S_LOG-altcode    = i_tcod.
* Create the log with header data
   CALL FUNCTION 'BAL_LOG_CREATE'
     EXPORTING
       i_s_log                 = l_s_log
     IMPORTING
       E_LOG_HANDLE            = l_log_handle
     EXCEPTIONS
       LOG_HEADER_INCONSISTENT = 1
       OTHERS                  = 2.
   IF sy-subrc <> 0.
     case sy-subrc.
       when 1.
         raise LOG_HEADER_INCONSISTENT.
       when others.
         raise LOGGING_ERROR.
     endcase.
   ENDIF.
   l_s_mdef-log_handle = l_log_handle.
* Set the default value
   CALL FUNCTION 'BAL_GLB_MSG_DEFAULTS_SET'
     EXPORTING
       i_s_msg_defaults = l_s_mdef
     EXCEPTIONS
       OTHERS           = 0.
* Loop the message table and write the messages into the log
   loop at T_LOG_MESSAGE.
* Issue the message in a dummy variable
     message ID T_LOG_MESSAGE-MSGID type t_log_message-MSGTYP number T_LOG_MESSAGE-MSGNR
     with t_log_message-MSGV1 t_log_message-MSGV2
     t_log_message-MSGV3 t_log_message-MSGV4
     into l_dummy.
* The parameters set by message statement will be used
* Add the message in the log
     PERFORM msg_add USING l_log_handle.
   endloop.
   append l_log_handle to lt_log_handle.
* save logs in the database
   CALL FUNCTION 'BAL_DB_SAVE'
     EXPORTING
*        I_SAVE_ALL       = 'X'
*       I_IN_UPDATE_TASK   = 'X'
      I_T_LOG_HANDLE = lt_log_handle
     EXCEPTIONS
       LOG_NOT_FOUND    = 1
       SAVE_NOT_ALLOWED = 2
       NUMBERING_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.
   commit work.
 endif.
ENDFUNCTION.


*--------------------------------------------------------------------
* FORM MSG_ADD
*--------------------------------------------------------------------
* Add the message to the log
*-------------------------------------------------------------------*
FORM msg_add USING i_log_handle TYPE balloghndl.
 DATA:
 l_s_msg TYPE bal_s_msg.
* define data of message for Application Log
 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.
* add this message to log file
* (I_LOG_HANDLE is not specified, we want to add to the default log.
* If it does not exist we do not care =>EXCEPTIONS log_not_found = 0)
 CALL FUNCTION 'BAL_LOG_MSG_ADD'
   EXPORTING
     I_LOG_HANDLE  = i_log_handle
     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

Nota para leer todos los log desde la SLG1

Al parecer no hay forma standard de visualizar todos los logs sin tener que navegar, entonces, les presento un pequeño truco: Ir a la funcion APPL_LOG_DISPLAY (SE37), buscar el siguiente codigo

   IF NOT i_s_display_profile IS INITIAL.
     l_s_display_profile = i_s_display_profile.
   ELSE.
     IF number_of_protocols = 1.
       CALL FUNCTION 'BAL_DSP_PROFILE_SINGLE_LOG_GET'
         IMPORTING
           e_s_display_profile = l_s_display_profile
         EXCEPTIONS
           OTHERS              = 0.
     ELSE.
       CALL FUNCTION 'BAL_DSP_PROFILE_STANDARD_GET'
         IMPORTING
           e_s_display_profile = l_s_display_profile
         EXCEPTIONS
           OTHERS              = 0.
     ENDIF.
   ENDIF.

colocar un BREKPOINT. Entonces ejecutamos la SLG1 y cuando lleguemos a la linea, cambiamos al valor de la variable number_of_protocols por '1' y voilà

Programas ejemplo

SBAL_DEMO*, por ejemplo SBAL_DEMO_04_DETLEVEL