Generate Application Log

By | October 2, 2008 | Application Log | 30,489 | 17

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.

Background

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:

These are the important transaction codes used for Application Log:

  • SLG0 – Create a new Log Object and subobject
  • SLG1 – Display Application Log
  • SLG2 – Delete the Application Log

Code Snippet

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.

*&---------------------------------------------------------------------*
*& This code snippet will show how to generate the Application log
*&   by using some function modules
*&
*&---------------------------------------------------------------------*
*
DATAlf_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'.

The generated application Log will look like this:

TIP

When we generate the some type of application log in the background job, we would like to set the appropriate job status based on the messages in the log table. For example, if the Application log has any error we want to set the status of the job as the “Cancelled”. To achieve this, we have to add this type of code after we append our log to database using the FM APPL_LOG_WRITE_DB:

*&---------------------------------------------------------------------*
*& This code snippet will show how to set the status of the background
*&   job if the application log has been generated in the background.
*&---------------------------------------------------------------------*
  read table lt_msg into la_msg with key msgty 'E'.
  if sy-subrc 0.
    message e398(00with 'Some error occured. Application Log'
                          'has generated. Check in tcode SLG1'.
  endif.

You can find more about the Application Log in SAP Library at: Application Log

When to use Application Log?

One of the reader has asked this question: When to use Application Log?. Application Log Should be used for these situations:

  • When we want to save the List/Log for the long time: Generally, we have the spool retention preiod of 8 days. So, the list or log will be deleted automatically.
  • When we want more information compared to Log generated with WRITE: Application Log has more information like User, date of creation, Severarity of the message.
  • In ALE / EDI Processing: When we do the cross client processing (IDoc Processing), we can generate the Application log to keep track of the generated errors or the messages.

UPDATE

The FMs used here to generate Application Log are replaced by new set of FMs. Check out the New FMs to generate Application Log.

Like It? Share!!

Don't miss an Update

Get notified of the new post, right into your inbox

Naimesh Patel{272 articles}

I'm SAP ABAP Consultant for more than a decade. I like to experiment with ABAP especially OO. I have been SDN Top Contributor.
Follow :

Explore all of his 272 articles.

Load comments

17 Comments

  • Anonymous

    hi naimesh,

    thanks for sharing the knowledge…

    can u explain in which situations generation of application log is needed… i mean when

    thanks

  • Hello…

    I have updated the post with regards of your question – When to use the application log. Thanks for pointing me out to that missing point.

    Regards,
    Naimesh Patel

  • Anonymous

    "In my openion, you can use the Application log for this purpose.

    I assume that you have some triggering point (some Zprogram) which will trigger ME51N , than ME21N, and so on and so forth. In the Zprogram you can generate the application log at starting point, after finishing each point and ending point.

    FM BAL_GLB_SEARCH_LOG might help you to get the information you want. Than you can use the GUI_DOWNLOAD to download data."

    Thanks for your comment. I don't have a "triggering point". Are you saying that I need to capture every group

    Process ID eg document number
    Event for example Me21N Create PO
    Date & time
    User

    then link them?

    Regards,
    George

  • Anonymous

    Hi Nelmesh,

    Your blog is fantastic!

    Have a transaction AUT10 where I get data about log of tables.

    Do you known how I get this information?

    Sorry, if I don’t write very well. This because I live in Brazil.

    Regards,

    Cícero

  • Hello Cicero,

    Sorry I don’t have much knowledge about that transaction. Try posting your question forum of sdn.sap.com

    And don’t be sorry for your English :)

    Regards,
    Naimesh Patel

  • Anonymous

    Tks Neimesh!

  • Hi i am looking for a similar solution. currently i can see the log in SLg1 trans, but i would like to write a separate report the listing out the detailed messages with the data involved in that.

    My requirement is i need to get an EPC number in the current SAP AII system, and list it out in a report. I am not able to find where the data is coming from and how it is being attached to these messages.

    example messages :

    1. Processing activity DEVICE_CREATE_DOCUMENT_INTERN in rule ZUNLOAD_A
    2. Activity DEVICE_CREATE_DOCUMENT_INTERN executed – activity status: OK
    3. Processing activity DEVICE_VALIDATE_40 in rule ZUNLOAD_A
    4. 1 urn:epc:tag:grai-96:4.662510.000801.3100114602 is in the message
    5. Processing w/o document, action type AUBO ‘Unload’
    6. Activity DEVICE_VALIDATE_40 executed – activity status: OK

    i need to pick up the number in the 4th line

    Thanks for your help!

    Regards,
    Naveen Annamreddy

  • Anonymous

    Nimesh,

    I have a z program that generates the application log successfully and I can also display the log via SLG1. Now, I would like to email this log automatically when the report is run in the background. What would be the best option in your opinion ?

    thanks,

    suresh

  • Hello Suresh,

    You asked:I would like to email this log automatically when the report is run in the background. What would be the best option in your opinion ?

    Option 1:
    You can use the BAL_GLB_SEARCH_LOG to get the Log messages into internal table. Once you have it in internal table, you can send it as attachement.

    Option 2:
    You can use the BAL_DSP_LOG_PRINT to generate the Spool and than read the spool and set that data as attachement.

    Regards,
    Naimesh Patel

  • Hello Naimesh ,

    I also have a z program that generates the application log successfully. I want to add the selection variant (input variables) to this log, is it possible? (As we could see it in SM37 background jobs).

    Regards,
    Görkem

  • Hello Görkem,

    You asked:I want to add the selection variant (input variables) to this log, is it possible? (As we could see it in SM37 background jobs).

    I guess, there is no direct way to bring the Selection Parameters in the Application Log. But, you can treat it as message and append in the log with FM APPL_LOG_WRITE_MESSAGES.

    Regards,
    Naimesh Patel

  • Hi Nimesh,

    Will this SLG1 logging will cause any out of space if they are too many in production? Appreciate your response at the earliest.

    ~Krishna

  • Anonymous

    Can you tell me if AUT10 is a read only transaction? I am trying to determine if there is any risk in granting access to this transaction.

    Thanks
    dennis beausoleil 508 509 2914

  • Hello Dennis,

    It looks like transaction AUT10 is the display transaction. Frankly, I haven't worked on this transaction.

    Regards,
    Naimesh Patel

  • Although this is long time ago…

    Very useful!

    Just one thing:

    * 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'.

    This way of error message will not occur in a where-used list and will give no tranparency in debugger. Better use
    Data: lv_msg type string.
    message E00(398) with 'Error Testing 1234' into lv_msg.
    and perform put_symsg changing ls_msg.
    * In the form
    ls_msg-msgty = sy-msgty.
    ls_msg-msgid = sy-msgid.
    ls_msg-msgno = sy-msgno.
    ls_msg-msgv1 = sy-msgv1.
    ls_msg-msgv1 = sy-msgv2.
    ls_msg-msgv1 = sy-msgv3.
    ls_msg-msgv1 = sy-msgv4.

    In debugger, you can check lv_string to get the full message.

    Regards,

    Clemens

  • Jaydeep

    Hi Nimesh,

    Your blog is fantastic… you have very nicely explained the steps. However, I have one query.
    I have been asked to update the error logs which are present in the menu of VL01N transaction.I will write the logic in include MV50AFZ1 which gets called at the time of delivery creation or change.

    But my concern is that if I create log as per your code, will that be available for user on runtime as user wants the delivery creation to be stopped. They dont want a traditional error message but they want us to update the error logs of the transaction. Is this possible by your code on this site page ?

    Please reply soon. Thanks once again.

  • sudip Chakraborty

    Hi,
    Thanks for this blog.
    I have a requirement, I have to generate a Z report, which display the error messages created when creating collective PO through Tx. WF10.
    This report will run once in a day, so it should display all the errors created during the day.
    Now please let me know how I could retreive the error from the application log inside the report.

    Regards,
    Sudipta

Comments on this Post are now closed. If you have something important to share, you can always contact me.

You seem to be new here. Subscribe to stay connected.