Skip to Content
Technical Articles
Author's profile photo Bunny Arora

Create Application Logs and attach it to Application Job in ABAP Cloud

In this Blog Post, we will see how to create Application Logs and attach to an Application Job. Application Logs is a reuse library and can be used with other applications as well. This reuse library gives you a clearly structured overview of all errors that might have occurred during runtime.

In your Fiori Launchpad search for application :- Application Logs

Key Features

You can use this reuse library to:

  • view application log details
  • filter the logs by severity
  • search for message texts
  • display the message details

Steps Involved:-

  1. Create Application log Object, Subobject and External reference in ADT.
  2. Create Job Catalog entry.
  3. Create Application Job template.
  4. Writing code in job template class using provided interfaces- IF_APJ_DT_EXEC_OBJECT, IF_APJ_RT_EXEC_OBJECT.
  5. Writing code for Application Logs
  6. Execute Job.

Details:

Log Object, SubObject in ADT.

New Object > Others > Application Log Object.

Add your object and Subobject like below code

{
  "formatVersion": "1",
  "header": {
    "description": "new log",
    "originalLanguage": "en",
    "abapLanguageVersion": "cloudDevelopment"
  },
  "subobjects": [
    {
      "name": "ZSUBOBJECT1",
      "description": "Subobject 1"
    }
  ]
}

Create Job Catalog

New > Others > Application Job Catalog Entry 

Here add a custom class

Application%20Log%20Catalog%20Entry

Application Log Catalog Entry

Create Application Job Template

New > Others > Application Job Template

Here provide your created catalog

Application%20Job%20Template

Application Job Template

Now let’s add very simple required code in the class

Interfaces: The two interfaces used are related to job execution, one gets parameters for the job other executes the code, If_oo_adt_classrun is just a reuse interface to run code as an ABAP application to debug or test.

With cl_bali_log=>create_with_header, cl_bali_header_setter=>, Provide your Object and subobject to initiate log object.

Then with cl_bali_message_setter,cl_bali_freetext_setter,cl_bali_exception_setter we have formed all type of logging type messages.

l_log->add_item, adds item to logs.

Then method save_log will write the logs in database.

it may require COMMIT WORK.

Also notice in the code we have a parameter assign_to_current_appl_job, This will attach the logs to the running Application Job itself.

CLASS z_new_job_class_ec DEFINITION
  PUBLIC
  FINAL
  CREATE PUBLIC .

  PUBLIC SECTION.
    INTERFACES if_apj_dt_exec_object.
    INTERFACES if_apj_rt_exec_object.
    INTERFACES if_oo_adt_classrun.
  PROTECTED SECTION.
  PRIVATE SECTION.
ENDCLASS.

CLASS z_new_job_class_ec IMPLEMENTATION.

  METHOD if_apj_dt_exec_object~get_parameters.
* You can Insert code here for getting Job parameters 
  ENDMETHOD.


  METHOD if_apj_rt_exec_object~execute.
        DATA(l_log) = cl_bali_log=>create_with_header( cl_bali_header_setter=>create( object = 
                 'ZNEWLOG' subobject = 'ZSUBOBJECT1' ) ).

        "Add a message as item to the log
        DATA(l_message) = cl_bali_message_setter=>create( severity = 
                          if_bali_constants=>c_severity_error
                                                          id = 'PO'
                                                          number = '000' ).
        l_log->add_item( item = l_message ).

        "Add a second message, this time from system fields SY-MSGID, ...
        MESSAGE ID 'ZTEST' TYPE 'S' NUMBER '058' INTO DATA(l_text).

        l_log->add_item( item = cl_bali_message_setter=>create_from_sy( ) ).

        "Add a free text to the log

        DATA(l_free_text) = cl_bali_free_text_setter=>create( severity = 
                            if_bali_constants=>c_severity_error
                            text = 'Some Error Text' ).

        l_log->add_item( item = l_free_text ).

        " Add an exception to the log
        DATA: i TYPE i.
        TRY.
            i = 1 / 0.
          CATCH cx_sy_zerodivide INTO DATA(l_ref).
        ENDTRY.

        DATA(l_exception) = cl_bali_exception_setter=>create( severity = 
                                   if_bali_constants=>c_severity_error
                                   exception = l_ref ).
        l_log->add_item( item = l_exception ).

        "Save the log into the database
        cl_bali_log_db=>get_instance( )->save_log( log = l_log assign_to_current_appl_job = abap_true ).
        COMMIT WORK.

      CATCH cx_bali_runtime INTO DATA(l_runtime_exception).
    ENDTRY.


  ENDMETHOD.

Let’s Test it now.

Create Job from Job template in application:- Application Job

Create%20Job

Create Job

Select your template here via job template description.

Check Scheduling Options

Parameters coming from code in interface:

IF_APJ_DT_EXEC_OBJECT.

Hit Schedule

Log Generated. Click on it.

Job logs:

Summary:

Here you can find some documentation:

https://help.sap.com/docs/btp/sap-business-technology-platform/application-logs?q=Application%20Los

This short blog can help ABAP devs create Application logs and attach it to the Application Jobs with newer approach. Also since this is a reuse logging mechanism this can leveraged in other applications as well.

Since i am also new to ABAP cloud , Expert suggestions & feedbacks are much appreciated.

If you like this blog post you can follow me for more blogs i will try to make in coming future specifically related to ABAP in cloud.

Assigned Tags

      8 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Sandra Rossi
      Sandra Rossi

      Thanks. There's an official documentation about CL_BALI_LOG only in SAP BTP documentation (as far as I can find), but I can find the class in ABAP 7.57 on premise, do you know if the class is officially supported in on premise systems? Generally speaking, how to know if something allowed in SAP BTP is supported in ABAP On Premise?

      Author's profile photo Bunny Arora
      Bunny Arora
      Blog Post Author

      Hi Sandra ,

      That was one of the reason for me to write this blog because even i was unable to relate the documentation properly back to ABAP cloud. To your question for on-premise i belive it should work. Haven't tried.

      I generally look at the propoerties of the object in question.

      The ABAP language version and the API state(Release Contracts)- (For cloud or Key-User):

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Thanks. I wanted to know if there's an official SAP recommendation for these Cloud-released objects which also exist in on premise systems, would SAP Support help if we use them and there is a bug. Or is it recommended to continue using the old supported BAL function modules (BAL_LOG_XXXX and others).

      The only thing I can say is that your code works fine in on premise systems (7.57), the log appears via transaction code SLG1, but it's not my question.

      No worry, we'll see 😉

      Author's profile photo Mohit Chopra
      Mohit Chopra

      Hello Sandra,

      With 2022 on prem/pce release SAP recommends using Embedded steampunk (ABAP Cloud) even in On Premise and PCE implementations where only cloud released and whitelisted objects can work so definitely you should get support in case of issues.

      you can also follow Boris's blog for more details.

      How to use Embedded Steampunk in SAP S/4HANA Cloud, private edition and in on-premise – The new ABAP extensibility guide | SAP Blogs

       

      Author's profile photo Sandra Rossi
      Sandra Rossi

      Thanks, got it now, so it's officially supported: if I try to use it with S/4HANA 2022 (ABAP 7.57), custom class created with ABAP Language Version "ABAP for Cloud Development", I've got below  syntax error when trying to call function module BAL_LOG_CREATE - There's no syntax error if I keep the default "Standard ABAP" language version, and of course CL_BALI_LOG can be used in this context too.

      The use of Function Module BAL_LOG_CREATE is not permitted. Use Class CL_BALI_LOG instead.

      The%20use%20of%20Function%20Module%20BAL_LOG_CREATE%20is%20not%20permitted.%20Use%20Class%20CL_BALI_LOG%20instead.

      The use of Function Module BAL_LOG_CREATE is not permitted. Use Class CL_BALI_LOG instead.

      Author's profile photo Mio Yasutake
      Mio Yasutake

      This is what I was looking for. Thanks for sharing!

      Author's profile photo Jelena Perfiljeva
      Jelena Perfiljeva

      Thanks for sharing! There was another blog post recently about application log (not in Cloud), just posting a link here for reference: link

      Author's profile photo Pratik Gupta
      Pratik Gupta

      Hi Bunny Arora I was trying to replicate this but the Job Template which I have created in ADT is not reflecting in S4 Hana Launchpad in Application Job.

      Do I need to do something extra to do that ?

      Thanks in Advance