ABAP Development, SAP BTP, SAP ABAP Environment, SAP S/4HANA Cloud

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 Log Catalog Entry

Create Application Job Template

New > Others > Application Job Template

Here provide your created catalog

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 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: