Protocol messages into application log

Here an example how to protocol messages into application log from ABAP (transaction SLG1). Thanks to such approach you can report system and your own messages with common central functions together. Don't forget to add your Z* object and subobject in transaction SLG0.
REPORT zsprot2.

START-OF-SELECTION.

  PERFORM main.

*&---------------------------------------------------------------------*
*&      Form  main
*&---------------------------------------------------------------------*
FORM main.

  DATA:
    lv_log_handle TYPE balloghndl.

* the object and subject must exist if protocol should be saved in the db
* they can be created by transaction SLG0
  PERFORM appl_log_init USING 'ZOBJECT' 'ZSUBOBJ' CHANGING lv_log_handle.

  PERFORM appl_log_add_msg USING lv_log_handle 'S' 'B1' '999' 'a' 'b' 'c' 'd'.
  PERFORM appl_log_add_msg USING lv_log_handle 'S' 'B1' '999' '111' '222' '333' '444'.

  PERFORM appl_log_display USING lv_log_handle.

  PERFORM appl_log_save USING lv_log_handle.
ENDFORM.                    "main

*&---------------------------------------------------------------------*
*&      Form  appl_log_init
*&---------------------------------------------------------------------*
FORM appl_log_init USING pi_object pi_subobject CHANGING po_log_handle.

  DATA:
    ls_log_header TYPE bal_s_log.

  ls_log_header-object    = pi_object.
  ls_log_header-subobject = pi_subobject.
  ls_log_header-alprog    = sy-repid.

  CALL FUNCTION 'BAL_LOG_CREATE'
    EXPORTING
      i_s_log      = ls_log_header
    IMPORTING
      e_log_handle = po_log_handle.

ENDFORM.                    "appl_log_init

*&---------------------------------------------------------------------*
*&      Form  appl_log_add_msg
*&---------------------------------------------------------------------*
FORM appl_log_add_msg USING pi_log_handle
                            pi_msgty pi_msgid pi_msgno
                            pi_msgv1 pi_msgv2 pi_msgv3 pi_msgv4.

  DATA:
    ls_log_message TYPE bal_s_msg.

  ls_log_message-msgty = pi_msgty.
  ls_log_message-msgid = pi_msgid.
  ls_log_message-msgno = pi_msgno.
  ls_log_message-msgv1 = pi_msgv1.
  ls_log_message-msgv2 = pi_msgv2.
  ls_log_message-msgv3 = pi_msgv3.
  ls_log_message-msgv4 = pi_msgv4.

  CALL FUNCTION 'BAL_LOG_MSG_ADD'
    EXPORTING
      i_log_handle = pi_log_handle
      i_s_msg      = ls_log_message.

ENDFORM.                    "appl_log_add_msg

*&---------------------------------------------------------------------*
*&      Form  appl_log_display
*&---------------------------------------------------------------------*
FORM appl_log_display USING pi_log_handle.

  DATA:
    lt_log_handle      TYPE bal_t_logh,
    ls_display_profile TYPE bal_s_prof.

* simplest display profile
  CALL FUNCTION 'BAL_DSP_PROFILE_NO_TREE_GET'
    IMPORTING
      e_s_display_profile = ls_display_profile.

  APPEND pi_log_handle TO lt_log_handle.

  CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
    EXPORTING
      i_s_display_profile = ls_display_profile
      i_t_log_handle      = lt_log_handle.

ENDFORM.                    "appl_log_display

*&---------------------------------------------------------------------*
*&      Form  appl_log_save
*&---------------------------------------------------------------------*
FORM appl_log_save USING pi_log_handle.

  DATA:
    ls_statistics  TYPE bal_s_scnt,
    lt_log_handle  TYPE bal_t_logh.

* check if protocol has messages
  CALL FUNCTION 'BAL_LOG_HDR_READ'
    EXPORTING
      i_log_handle   = pi_log_handle
    IMPORTING
      e_statistics   = ls_statistics.

  CHECK ls_statistics-msg_cnt_al > 0.

  APPEND pi_log_handle TO lt_log_handle.

  CALL FUNCTION 'BAL_DB_SAVE'
    EXPORTING
      i_save_all     = 'X'
      i_t_log_handle = lt_log_handle.

ENDFORM.                    "appl_log_save
See other related notes in my infodepot:
Create and display protocol in style of transport tools
Full list of examples in my infodepot

If you have a question, have found an error or just want to contact me, please use this form.

Copyright (C) 2012 http://www.kerum.pl/infodepot/

Disclaimer: I am not affiliated or related to any division or subsidiary of SAP AG.
Trademarks or registered trademarks of any products or companies referred to on this site belong to those companies.
Anyone using the given solutions, is doing it under his/her own responsibility and at own risk.