Enterprise Resource Planning Blogs by SAP
Get insights and updates about cloud ERP and RISE with SAP, SAP S/4HANA and SAP S/4HANA Cloud, and more enterprise management capabilities with SAP blog posts.
cancel
Showing results for 
Search instead for 
Did you mean: 
Bunny
Advisor
Advisor

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:


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.
15 Comments
Sandra_Rossi
Active Contributor
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?
MioYasutake
Active Contributor
This is what I was looking for. Thanks for sharing!
Bunny
Advisor
Advisor
0 Kudos
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):





Sandra_Rossi
Active Contributor
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 😉
I056086
Advisor
Advisor
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...

 
Sandra_Rossi
Active Contributor
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 use of Function Module BAL_LOG_CREATE is not permitted. Use Class CL_BALI_LOG instead.

Jelena
Active Contributor

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

pragupta14
Explorer
0 Kudos
Hi bunny71c 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

 
Bunny
Advisor
Advisor
0 Kudos
I don't think so. If you're creating the template, you should be able to see it in launchpad.. For others we have to design roles..Try repeating the steps maybe you missed something.
deveshsin
Explorer
0 Kudos
Hello bunny71c ,

Thanks for your blog, it was an informative read. I wanted to ask below points,

  1. Once the logs are saved in standard tables, how can we view them in a UI, is there an out of the box screen (like SLG1 in SAP on premise) where I can go and see logs based on log object and log sub object.

  2. If there is no such opportunity, what do you think SAP developers should do to view the logged logs.

  3. In order to view the logs in "Application Job", do I have to schedule an application job (through runtime artifacts) everytime I want to log something.


Thanks in advance.
AleGuarneri
Explorer
0 Kudos
deveshsin
Explorer
0 Kudos
Thanks @a.guarneri
deveshsin
Explorer
0 Kudos
@a.guarneri I want to enable viewing of application logs in a custom Fiori app and tried following the freestyle and fiori elements approach....I tried browsing for a reference blog but could not find any....do you have any reference blog....where I can take logic reference from
Bunny
Advisor
Advisor
0 Kudos
Thanks Devesh !

  1. There is an application called "Application Logs" , You can set a variant for this application as per your Object, Sub Object. (I hope i am understanding your question, if not please explain more)

  2. 1.

  3. No.

Bunny
Advisor
Advisor
0 Kudos