Advertising:
Create application log in cloud development
From SAP Knowledge Base
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 ).