Advertising:

Create application log in cloud development: Difference between revisions

From SAP Knowledge Base
No edit summary
 
(14 intermediate revisions by the same user not shown)
Line 1: Line 1:
{{Template:MetaDesc|For T1 / cloud developments the familiar function modules or classic classes for creating application logs can no loger be used.Released classes should be used.}}
[[category:code_snippets]]
For T1 / cloud developments the familiar function modules(FMs) or classic classes for creating application logs can no loger be used.
For T1 / cloud developments the familiar function modules(FMs) or classic classes for creating application logs can no loger be used.
For this purpose, SAP offers several released classes for cloud development. The object and sub-object should not be created via SLG0, but via Eclipse with ABAP Development Tools.
For this purpose, SAP offers several released classes for cloud development. The object and sub-object should not be created via SLG0, but via Eclipse with ABAP Development Tools.


= How to use the classes =
== How to use the classes ==


== Create new business application log ==
* cl_bali_log
* cl_bali_message_setter
* cl_bali_free_text_setter
*cl_bali_log_db
 
=== Create new business application log object ===
<syntaxhighlight lang="abap" line copy>
<syntaxhighlight lang="abap" line copy>
TRY.
TRY.
Line 12: Line 19:
</syntaxhighlight>
</syntaxhighlight>


== Create header data for application log ==
=== Create header data ===
<syntaxhighlight lang="abap" line copy>
<syntaxhighlight lang="abap" line copy>
  log->set_header( header = cl_bali_header_setter=>create( object = 'YOBJECT'
  log->set_header( header = cl_bali_header_setter=>create( object = 'YOBJECT'
Line 19: Line 26:
</syntaxhighlight>
</syntaxhighlight>


== Nachrichten zum Anwendungsprotokoll hinzufügen ==
=== Add messages ===
<syntaxhighlight lang="abap" line copy>  
<syntaxhighlight lang="abap" line copy>
  TRY.  
  TRY.  
   DATA(message) = cl_bali_message_setter=>create( severity = if_bali_constants=>c_severity_error
   DATA(message) = cl_bali_message_setter=>create( severity = if_bali_constants=>c_severity_error
Line 39: Line 46:
  ENDTRY.
  ENDTRY.
</syntaxhighlight>
</syntaxhighlight>
=== Save to database ===
There are two methods for saving the log:
* SAVE_LOG: uses the standard database connection. This means that the log is only written to the database when the application calls COMMIT_WORK.
* SAVE_LOG_2ND_DB_CONNECTION: uses a service for saving that commits the log directly to the database
<syntaxhighlight lang="abap" line copy>
TRY.
  cl_bali_log_db=>get_instance( )->save_log( log = log ).
  cl_bali_log_db=>get_instance( )->save_log_2nd_db_connection( log = log ).
CATCH cx_bali_runtime INTO DATA(runtime_exception).
ENDTRY.
</syntaxhighlight>
=== Delete business application log from memory / Check deleting in memory ===
<syntaxhighlight lang="abap" line copy>
TRY.
  log->release_memory( ). 
  IF log->is_invalidated( ).
  ENDIF
CATCH cx_bali_runtime INTO DATA(runtime_exception).
  runtime_exception->get_text( ).
ENDTRY.
</syntaxhighlight>
=== Delete business application log ===
<syntaxhighlight lang="abap" line copy>
TRY.
  cl_bali_log_db=>get_instance( )->delete_log( log = log ).
  CATCH cx_bali_runtime INTO DATA(runtime_exception).
ENDTRY.
</syntaxhighlight>
=== Add business application log to Fiori Business Application Job ===
The advantage of the classes used above when saving the log is that they are attached directly to the application job created via the Fiori app “Application job” if the corresponding parameter “ASSIGN_TO_CURRENT_APPL_JOB” is set to abap_true at cl_bali_log_db=>get_instance( )->save_log( log = log ).

Latest revision as of 17:05, 31 December 2024

For T1 / cloud developments the familiar function modules(FMs) or classic classes for creating application logs can no loger be used. For this purpose, SAP offers several released classes for cloud development. The object and sub-object should not be created via SLG0, but via Eclipse with ABAP Development Tools.

How to use the classes

  • cl_bali_log
  • cl_bali_message_setter
  • cl_bali_free_text_setter
  • cl_bali_log_db

Create new business application log object

TRY.
DATA(log) = cl_bali_log=>create( ).
CATCH cx_bali_runtime INTO DATA(runtime_exception).
ENDTRY.

Create header data

 log->set_header( header = cl_bali_header_setter=>create( object = 'YOBJECT'
                                                          subobject = 'YSUBOBJECT'
                                                          external_id = 'Ext ID' ) ).

Add messages

 TRY. 
  DATA(message) = cl_bali_message_setter=>create( severity = if_bali_constants=>c_severity_error
                                                     id = 'YMC_ABC'
                                                     number = '000' ).
   log->add_item( item = lo_message ).
  
   log->add_item( item = cl_bali_message_setter=>create_from_sy( ) ).
  
   DATA(free_text) = cl_bali_free_text_setter=>create( severity = if_bali_constants=>c_severity_error
                                                           text = 'Some Error Text' ).
   log->add_item( item = free_text ).
  
   DATA(exception) = cl_bali_exception_setter=>create(  severity = if_bali_constants=>c_severity_error
                                                       exception = ref ).
   lo_log->add_item( item = exception ).
  CATCH cx_bali_runtime INTO DATA(runtime_exception).
 ENDTRY.

Save to database

There are two methods for saving the log:

  • SAVE_LOG: uses the standard database connection. This means that the log is only written to the database when the application calls COMMIT_WORK.
  • SAVE_LOG_2ND_DB_CONNECTION: uses a service for saving that commits the log directly to the database
 TRY. 
  cl_bali_log_db=>get_instance( )->save_log( log = log ).
  cl_bali_log_db=>get_instance( )->save_log_2nd_db_connection( log = log ).
 CATCH cx_bali_runtime INTO DATA(runtime_exception).
 ENDTRY.

Delete business application log from memory / Check deleting in memory

 TRY.
  log->release_memory( ).  
  IF log->is_invalidated( ).
  ENDIF
 CATCH cx_bali_runtime INTO DATA(runtime_exception).
  runtime_exception->get_text( ).
 ENDTRY.

Delete business application log

 TRY.
   cl_bali_log_db=>get_instance( )->delete_log( log = log ).
  CATCH cx_bali_runtime INTO DATA(runtime_exception).
 ENDTRY.

Add business application log to Fiori Business Application Job

The advantage of the classes used above when saving the log is that they are attached directly to the application job created via the Fiori app “Application job” if the corresponding parameter “ASSIGN_TO_CURRENT_APPL_JOB” is set to abap_true at cl_bali_log_db=>get_instance( )->save_log( log = log ).


This is a wiki created in the spare time of a private person working in the SAP ERP area. The aim is to collect knowledge for the own use. The wiki is maintained to the best of knowledge and belief.
All products shown, including in form of screenshots, belong to SAP SE. Their trademarks are, among others: SAP®,ABAP®,SAP Fiori®,SAP HANA®,SAP NetWeaver®,SAP® R/3®,SAP S/4HANA®,SAP S/4HANA® Cloud