SAP Application log – Writing to the Application log

Writing to the Application log

In previous posts, I gave you an introduction to the Application log in SAP. I showed you how to create a log and view/delete messages from the log.  In this installment, I will show you how to write entries to the log.  Application logging has been around for some time.  The “old” function modules all start with APPL_*.  SAP has provided better and easier-to-use function modules from 4.6C.  These newer function modules start with BAL_*

Using the BAL_* function modules is pretty straight forward: you create a log with function module BAL_LOG_CREATE and add messages to it with BAL_LOG_MSG_ADD.  To display the log programmatically, you call BAL_DSP_LOG_DISPLAY.  An example program provided by SAP is SBAL_DEMO_01.  Check out the function modules in function groups SBAL and SBAL_DISPLAY.

The basic steps are:

  1. Initialize the log:
log-extnumber = 'My log’.
log-aluser    = sy-uname.
log-alprog    = sy-repid.
call function 'BAL_LOG_CREATE'
   exporting
      i_s_log = log
   exceptions
      others  = 1.
  1. Write messages to the log:
 msg-msgty     = sy-msgty.
 msg-msgid     = sy-msgid.
 msg-msgno     = sy-msgno.
 msg-msgv1     = sy-msgv1.
 msg-msgv2     = sy-msgv2.
 msg-msgv3     = sy-msgv3.
 msg-msgv4     = sy-msgv4.
 l_high        = 1.
 msg-probclass = l_high.

 call function 'BAL_LOG_MSG_ADD'
    exporting
       i_s_msg       = msg
    exceptions
       log_not_found = 0
       others        = 1.
  1. Display the log:
call function 'BAL_DSP_LOG_DISPLAY'
     exceptions
        others                 = 1.

You will notice that in Step 1, we passed neither object, nor sub-object.  Yet we are able to see the log. But there is a catch: logs displayed this way are only available for the life of the program.  If you run program SBAL_DEMO_01, or a program based on the code above, you will see that the log number assigned is temporary:

To store the log in the database, we will have to modify the code to specify object and sub-object and flush the messages to the database with BAL_DB_SAVE.  Logs saved this way can be displayed in transaction SLG1.

Here is the modified code.  The additions are italicized.

  1. Initialize the log:
log-extnumber = 'My log’.
 log-aluser    = sy-uname.
 log-alprog    = sy-repid.
log-object    = 'ZOM_DEBUG'.
 log-subobject = 'SD/MM'.
 call function 'BAL_LOG_CREATE'
      exporting
        i_s_log = log
importing
        e_log_handle = handle
     EXCEPTIONS
others  = 1.
  1. Write messages to the log:
 msg-msgty     = sy-msgty.
 msg-msgid     = sy-msgid.
 msg-msgno     = sy-msgno.
 msg-msgv1     = sy-msgv1.
 msg-msgv2     = sy-msgv2.
 msg-msgv3     = sy-msgv3.
 msg-msgv4     = sy-msgv4.
 l_high        = 1.
 msg-probclass = l_high.

 call function 'BAL_LOG_MSG_ADD'
      exporting
        i_s_msg       = msg
        i_log_handle  = handle
      exceptions
        log_not_found = 0
        others        = 1.
  1. Save the log to the database:
 data: b_handle type BAL_T_LOGH.
 append handle to b_handle.
 CALLFUNCTION'BAL_DB_SAVE'EXPORTING
 i_t_log_handle = b_handle. 
  1. Display the log:
call function 'BAL_DSP_LOG_DISPLAY'
 exceptions
 others                 = 1.

Temporary logs like the ones created by SBAL_DEMO_01 are good to show the results of a program or a transaction during or at the end of the run.  This kind of logging is best suited for on-line programs. These logs are then discarded.  Running programs that have temporary logging in the background will still produce a list in the spool, but you lose the interactive features of SLG1.

Database logs are best suited for background runs and when there is a delay between the program run and displaying or analyzing the log.  If you have jobs that run in the middle of the night but you only check their status the next morning, you should write the messages to the database log.

You may not want to have logging active all the time.  You may only want to turn it on when want to see what is happening under the hood without having to fire up the debugger.  You may not be allowed to debug or even have access to the debugger in production systems.  To selectively turn logging on  and off, you may want to code conditional logic based on the value of a user parameter or a flag in table TVARV (or TVARVC).

Another good tip: Encapsulate the calls to the logging function modules inside macros and store the macros in an include.  This makes actual coding less voluminous and cleaner looking.  You logging code could look like this:

. . .
Create_log i_object i_subobject o_handle.
Add_message   o_handle l_message.
Save_messages_to_db t_handles.


References: http://help.sap.com/saphelp_erp60_sp/helpdata/EN/d6/5d7f38f52f923ae10000009b38f8cf/frameset.htm

Advertisements

2 comments

  1. AK

    Good information. Thanks for putting this together.

  2. Pingback: SAP Application log – Resources and links | SAP BW

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: