* |__/
* Class | zcl_log
* | Wrapper class for cl_bal_logobj, with added functionality
*
"!
Applicationlog service class
"! The zcl_log service class is a wrapper for the cl_bal_logobj. With this class a
"! Business Application log can be composed, for recording SLG1 messages. The log can be saved which
"! makes it available in transaction SLG1.
"! Use and added functionality
"!
"! - Singleton class, instantiate with get_instance( ).
"! - Exception handling is eliminated (captured) (we are assuming the logging itself is operational).
"! - Use method has_errors( ) to check whether errors were logged.
"! - All log messages can also be made available as text lines. Attribute gt_textlog, method gt_log_as_text( ).
"! This can be especially helpful with background jobs: when running in the foreground messages can be displayed in a simple ALV list, when
"! running in the background the messages can be displayed as batch job log entries (using MESSAGE .... TYPE 'S'.
"! - Method add( ) is added for single messages (e.g. add( iv_msgid = 'V2' iv_msgno = 005 )).
"! - Method add_bapiret( ) is added for processing a list of messages (e.g. from a BAPI call).
"! - In case multiple separate logs are used, method reset_instance( ) can be used for the new log.
"!
"! The class is an extension on cl_bal_logobj. Use the methods on cl_bal_logobj directly through the
"! go_log attribute. E.g. go_log->increment_detlevel( )
CLASS zcl_log DEFINITION
PUBLIC
FINAL
CREATE PRIVATE .
PUBLIC SECTION.
TYPES: BEGIN OF gty_textlog,
messagetype TYPE sy-msgty,
message TYPE c length 110,
END OF gty_textlog.
TYPES: gty_textlog_t type standard table of gty_textlog with default key.
DATA go_log TYPE REF TO cl_bal_logobj.
DATA gt_textlog type gty_textlog_t READ-ONLY.
"! The starting point for using this service class. All parameters are optional. If the log needs to
"! be saved, parameters iv_object and iv_subobject are mandatory. Check transaction
"! SLG0 for possible values.
CLASS-METHODS get_instance
IMPORTING
iv_object TYPE balobj_d OPTIONAL
iv_subobject TYPE balsubobj OPTIONAL
iv_extnumber TYPE balnrext OPTIONAL
iv_reorg_in_days TYPE alccmparam DEFAULT '3'
RETURNING VALUE(ro_return) TYPE REF TO zcl_log.
CLASS-METHODS reset_instance.
METHODS add IMPORTING
iv_msgty TYPE sy-msgty DEFAULT 'I'
iv_msgid TYPE sy-msgid
iv_msgno TYPE sy-msgno
iv_msgv1 TYPE sy-msgv1 OPTIONAL
iv_msgv2 TYPE sy-msgv2 OPTIONAL
iv_msgv3 TYPE sy-msgv3 OPTIONAL
iv_msgv4 TYPE sy-msgv4 OPTIONAL.
METHODS add_exception IMPORTING io_exception TYPE REF TO cx_root.
METHODS add_msg IMPORTING iv_probclass TYPE bal_s_msg-probclass DEFAULT space.
METHODS add_errortext IMPORTING iv_errortext TYPE string.
METHODS add_errortext_and_save IMPORTING iv_errortext TYPE string.
METHODS add_text IMPORTING iv_text TYPE string.
METHODS add_bapiret IMPORTING it_bapiret TYPE bapiret2_t.
METHODS save
IMPORTING
iv_only_when_errors type boolean default abap_false
iv_client TYPE sy-mandt DEFAULT sy-mandt
iv_in_update_task TYPE boolean OPTIONAL
iv_save_all TYPE boolean DEFAULT 'X'
iv_2th_connection TYPE boolean OPTIONAL
iv_2th_connect_commit TYPE boolean OPTIONAL
iv_link2job TYPE boolean DEFAULT 'X'
EXPORTING
et_lognumbers TYPE bal_t_lgnm.
METHODS display
IMPORTING
iv_display_type TYPE c DEFAULT 'P'
is_disp_profile TYPE bal_s_prof OPTIONAL .
METHODS has_errors RETURNING VALUE(rv_return) TYPE boolean.
METHODS get_log_as_text RETURNING VALUE(rt_return) type gty_textlog_t.
PROTECTED SECTION.
PRIVATE SECTION.
CLASS-DATA go_instance TYPE REF TO zcl_log.
DATA gv_errors_logged TYPE boolean.
METHODS check_for_errors IMPORTING iv_messagetype TYPE sy-msgty.
ENDCLASS.
CLASS zcl_log IMPLEMENTATION.
METHOD get_instance.
IF go_instance IS INITIAL.
CREATE OBJECT go_instance.
TRY.
CREATE OBJECT go_instance->go_log
EXPORTING
i_log_object = iv_object
i_default_subobject = iv_subobject
i_reorg_in_days = iv_reorg_in_days
i_extnumber = iv_extnumber.
CATCH cx_bal_exception INTO DATA(lx_exception).
MESSAGE lx_exception->get_text( ) TYPE 'E'.
ENDTRY.
go_instance->gv_errors_logged = abap_false.
ENDIF.
ro_return = go_instance.
ENDMETHOD.
METHOD reset_instance.
"Note: reset_instance is only effective when the new instance is for the same
"log object and sub object. The external number can be differenf for your next
"application log, but object and subobject need to be the same. Restriction of
"the cl_bal_logobj class.
CLEAR go_instance->gv_errors_logged.
CLEAR go_instance->gt_textlog.
CLEAR go_instance.
ENDMETHOD.
METHOD add.
"Move parameter values to sy values
MESSAGE ID iv_msgid TYPE iv_msgty NUMBER iv_msgno
INTO DATA(lv_message) WITH iv_msgv1 iv_msgv2 iv_msgv3 iv_msgv4.
add_msg( ).
ENDMETHOD.
METHOD add_exception.
TRY.
go_log->add_exception( i_exception = io_exception ).
APPEND VALUE #( messagetype = 'E'
message = io_exception->get_text( ) ) TO gt_textlog.
CATCH cx_bal_exception.
"Ignore the exception
ENDTRY.
gv_errors_logged = abap_true.
ENDMETHOD.
METHOD add_msg.
TRY.
go_log->add_msg( i_probclass = iv_probclass ).
CATCH cx_bal_exception.
".. and ignore
ENDTRY.
MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
INTO DATA(lv_message) WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
APPEND VALUE #( messagetype = sy-msgty message = lv_message ) TO gt_textlog.
check_for_errors( sy-msgty ).
ENDMETHOD.
METHOD add_errortext.
TRY.
APPEND VALUE #(
messagetype = 'E'
message = |{ iv_errortext }| ) TO gt_textlog.
go_log->add_errortext( i_errortext = iv_errortext ).
CATCH cx_bal_exception.
".. and ignore
ENDTRY.
gv_errors_logged = abap_true.
ENDMETHOD.
METHOD add_errortext_and_save.
add_errortext( iv_errortext ).
save( ).
ENDMETHOD.
METHOD add_text.
TRY.
APPEND VALUE #(
messagetype = 'I'
message = iv_text ) TO gt_textlog.
go_log->add_statustext( i_statustext = iv_text ).
CATCH cx_bal_exception.
".. and ignore
ENDTRY.
ENDMETHOD.
METHOD add_bapiret.
LOOP AT it_bapiret INTO DATA(ls_bapiret).
MESSAGE ID ls_bapiret-id TYPE ls_bapiret-type NUMBER ls_bapiret-number
INTO DATA(lv_message)
WITH ls_bapiret-message_v1 ls_bapiret-message_v2 ls_bapiret-message_v3 ls_bapiret-message_v4.
APPEND VALUE #(
messagetype = ls_bapiret-type
message = lv_message ) TO gt_textlog.
check_for_errors( ls_bapiret-type ).
add_msg( ).
ENDLOOP.
ENDMETHOD.
METHOD save.
if iv_only_when_errors = abap_true and not has_errors( ) = abap_false.
exit.
endif.
TRY.
go_log->save(
EXPORTING
i_client = iv_client
i_in_update_task = iv_in_update_task
i_save_all = iv_save_all
i_2th_connection = iv_2th_connection
i_2th_connect_commit = iv_2th_connect_commit
i_link2job = iv_link2job
IMPORTING
et_lognumbers = et_lognumbers ).
CATCH cx_bal_exception INTO DATA(lx_exception).
MESSAGE |Save failed: { lx_exception->get_text( ) }| TYPE 'E'.
ENDTRY.
ENDMETHOD.
METHOD display.
go_log->display( ).
ENDMETHOD.
METHOD has_errors.
rv_return = gv_errors_logged.
ENDMETHOD.
METHOD get_log_as_text.
rt_return = gt_textlog.
ENDMETHOD.
METHOD check_for_errors.
IF iv_messagetype = 'E'.
gv_errors_logged = abap_true.
ENDIF.
ENDMETHOD.
ENDCLASS.