Quantcast
Channel: SCN: Message List - ABAP Switching, Enhancing, and Adapting Standard Programs
Viewing all articles
Browse latest Browse all 2464

Re: How to create new Application logs in SAP by using FM BAL_LOG_CREATE.

$
0
0

Hi,

 

here is a simple Test-Report that uses the class "zvis_appl_logger" to wrap the BAL-Function-Calls for create, add message and save. Look at the implementation of the class-methods: "init", "add_messagetext", "close",

 

Regards Steffen

 

 

The Test-Report:

 

*&---------------------------------------------------------------------*
*& Report  ZSPAHR_TEST_APPLICATION_LOGGER
*&
*&---------------------------------------------------------------------*

report  zspahr_test_application_logger.

call method zvis_appl_logger=>init
   exporting
     iv_object    = 'ZSPAHR_PHV_DUNKEL'
     iv_subobject = 'ZSPAHR_PHV_BACKEND'
     iv_extnumber = 'Class.methode() / Nummer'.

call method zvis_appl_logger=>add_message_text
   exporting
     iv_msgty         = 'A'
     iv_text          = 'Dunkeltarifierung Privathaftpflicht Abbruch'
     iv_add_timestamp = 'X'.

call method zvis_appl_logger=>add_message_text
   exporting
     iv_msgty         = 'I'
     iv_text          = 'Dunkeltarifierung Privathaftpflicht Information'
     iv_add_timestamp = 'X'.

call method zvis_appl_logger=>add_message_text
   exporting
     iv_msgty         = 'W'
     iv_text          = 'Dunkeltarifierung Privathaftpflicht Warnung'
     iv_add_timestamp = 'X'.

call method zvis_appl_logger=>add_message_text
   exporting
     iv_msgty         = 'E'
     iv_text          = 'Dunkeltarifierung Privathaftpflicht Error'
     iv_add_timestamp = 'X'.

call method zvis_appl_logger=>add_message_text
   exporting
     iv_msgty         = 'S'
     iv_text          = 'Dunkeltarifierung Privathaftpflicht Success'
     iv_add_timestamp = 'X'.

call method zvis_appl_logger=>add_message_text
   exporting
     iv_msgty         = 'D'
     iv_text          = 'Dunkeltarifierung Privathaftpflicht Debug'
     iv_add_timestamp = 'X'.

call method zvis_appl_logger=>add_message_text
   exporting
     iv_msgty         = ' '
     iv_text          = 'Dunkeltarifierung Privathaftpflicht Space'
     iv_add_timestamp = 'X'.

data ls_bapiret2 type bapiret2.
data lt_bapiret2 type bapiret2_t.

ls_bapiret2-type = 'E'.
ls_bapiret2-message = 'Fehler !'.
append ls_bapiret2 to lt_bapiret2.

ls_bapiret2-type = 'W'.
ls_bapiret2-message = 'Warnung !'.
append ls_bapiret2 to lt_bapiret2.

ls_bapiret2-type = 'S'.
ls_bapiret2-message = 'Erfolg!'.
append ls_bapiret2 to lt_bapiret2.

ls_bapiret2-type = 'I'.
ls_bapiret2-message = 'Info !'.
append ls_bapiret2 to lt_bapiret2.

call method zvis_appl_logger=>add_messages
   exporting
     it_bapiret2 = lt_bapiret2.

call method zvis_appl_logger=>close.



and the Wrapper-Class I am using for Logging:



class zvis_appl_logger definition
   public
   final
   create public .

*"* public components of class ZVIS_APPL_LOGGER
*"* do not include other source files here!!!
public section.

   class-methods init
     importing
       value(iv_object) type balobj_d
       value(iv_subobject) type balsubobj
       value(iv_extnumber) type balnrext optional
       value(iv_active) type boolean optional .
   class-methods close .
   class-methods add_message_text
     importing
       value(iv_msgty) type symsgty
       value(iv_text) type char128
       value(iv_add_timestamp) type boolean optional .
   class-methods add_messages
     importing
       value(it_bapiret2) type bapiret2_t .
   class-methods activate .
   class-methods deactivate .
   class-methods set_active
     importing
       value(iv_active) type boolean .
*"* protected components of class ZVIS_APPL_LOGGER
*"* do not include other source files here!!!
protected section.
*"* private components of class ZVIS_APPL_LOGGER
*"* do not include other source files here!!!
private section.

   class-data gv_log_handle type balloghndl .
   class-data gv_active type boolean value 'X'. "#EC NOTEXT .
endclass.



class zvis_appl_logger implementation.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZVIS_APPL_LOGGER=>ACTIVATE
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
method activate.

   gv_active = 'X'.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZVIS_APPL_LOGGER=>ADD_MESSAGES
* +-------------------------------------------------------------------------------------------------+
* | [--->] IT_BAPIRET2                    TYPE        BAPIRET2_T
* +--------------------------------------------------------------------------------------</SIGNATURE>
method add_messages.

   data lr_bapiret2 type ref to bapiret2.
   data lv_text type char128.

   if gv_active = 'X' and not gv_log_handle is initial.

     loop at it_bapiret2 reference into lr_bapiret2.

       concatenate lr_bapiret2->id lr_bapiret2->number lr_bapiret2->message
         lr_bapiret2->message_v1 lr_bapiret2->message_v2 lr_bapiret2->message_v3
         lr_bapiret2->message_v4 into lv_text separated by space.

       call function 'BAL_LOG_MSG_ADD_FREE_TEXT'
         exporting
          i_log_handle = gv_log_handle
          i_msgty      = lr_bapiret2->type
*   I_PROBCLASS               = '4'
           i_text                    = lv_text
*   I_S_CONTEXT               =
*   I_S_PARAMS                =
* IMPORTING
*   E_S_MSG_HANDLE            =
*   E_MSG_WAS_LOGGED          =
*   E_MSG_WAS_DISPLAYED       =
    exceptions
      log_not_found             = 1
      msg_inconsistent          = 2
      log_is_full               = 3
      others                    = 4.
       .
       if sy-subrc <> 0.
* MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
*         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
       endif.
     endloop.
   endif.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZVIS_APPL_LOGGER=>ADD_MESSAGE_TEXT
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_MSGTY                       TYPE        SYMSGTY
* | [--->] IV_TEXT                        TYPE        CHAR128
* | [--->] IV_ADD_TIMESTAMP               TYPE        BOOLEAN(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
method add_message_text.

   data lv_time_stamp type timestampl.
   data lv_atime(24) type c.
   data lv_tstr type string.


   if gv_active = 'X' and not gv_log_handle is initial.

     if iv_add_timestamp = 'X'.
       get time stamp field lv_time_stamp.
       write lv_time_stamp time zone sy-zonlo to lv_atime.
       concatenate lv_atime ': ' iv_text into iv_text respecting blanks.
     endif.

     call function 'BAL_LOG_MSG_ADD_FREE_TEXT'
       exporting
        i_log_handle              = gv_log_handle
         i_msgty                   = iv_msgty
*   I_PROBCLASS               = '4'
         i_text                    = iv_text
*   I_S_CONTEXT               =
*   I_S_PARAMS                =
* IMPORTING
*   E_S_MSG_HANDLE            =
*   E_MSG_WAS_LOGGED          =
*   E_MSG_WAS_DISPLAYED       =
  exceptions
    log_not_found             = 1
    msg_inconsistent          = 2
    log_is_full               = 3
    others                    = 4.

*IF sy-subrc <> 0.
** MESSAGE ID SY-MSGID TYPE SY-MSGTY NUMBER SY-MSGNO
**         WITH SY-MSGV1 SY-MSGV2 SY-MSGV3 SY-MSGV4.
*ENDIF.
   endif.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZVIS_APPL_LOGGER=>CLOSE
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
method close.

   if gv_active = 'X' and not gv_log_handle is initial.

     call function 'BAL_DB_SAVE'
       exporting
         i_save_all       = 'X'
       exceptions
         log_not_found    = 1
         save_not_allowed = 2
         numbering_error  = 3
         others           = 4.

*    IF sy-subrc <> 0.
*      MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
*      WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
*    ENDIF.

     clear gv_log_handle.
   endif.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZVIS_APPL_LOGGER=>DEACTIVATE
* +-------------------------------------------------------------------------------------------------+
* +--------------------------------------------------------------------------------------</SIGNATURE>
method deactivate.

   clear gv_active.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZVIS_APPL_LOGGER=>INIT
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_OBJECT                      TYPE        BALOBJ_D
* | [--->] IV_SUBOBJECT                   TYPE        BALSUBOBJ
* | [--->] IV_EXTNUMBER                   TYPE        BALNREXT(optional)
* | [--->] IV_ACTIVE                      TYPE        BOOLEAN(optional)
* +--------------------------------------------------------------------------------------</SIGNATURE>
method init.

   data: ls_log type bal_s_log. "Log header data

   if iv_active is supplied.
     gv_active = iv_active.
   endif.

   if gv_active = 'X'.
* define some header data of this log
     ls_log-object = iv_object.
     ls_log-subobject = iv_subobject.
     ls_log-extnumber = iv_extnumber.
     ls_log-aldate = sy-datum.
     ls_log-altime = sy-uzeit.
     ls_log-aluser = sy-uname.
     ls_log-alprog = sy-repid.
     ls_log-aldate_del = sy-datum.
     ls_log-aldate_del+4(2) = ls_log-aldate_del+4(2) + 3.
     if ls_log-aldate_del+4(2) > '12'.
       ls_log-aldate_del+4(2) = ls_log-aldate_del+4(2) - 12.
       ls_log-aldate_del+0(4) = ls_log-aldate_del+0(4) + 1.
     endif.

     call function 'BAL_LOG_CREATE'
       exporting
         i_s_log                 = ls_log
       importing
         e_log_handle            = gv_log_handle
       exceptions
         log_header_inconsistent = 1
         others                  = 2.
   endif.

endmethod.


* <SIGNATURE>---------------------------------------------------------------------------------------+
* | Static Public Method ZVIS_APPL_LOGGER=>SET_ACTIVE
* +-------------------------------------------------------------------------------------------------+
* | [--->] IV_ACTIVE                      TYPE        BOOLEAN
* +--------------------------------------------------------------------------------------</SIGNATURE>
method set_active.

   gv_active = iv_active.

endmethod.
endclass.


Viewing all articles
Browse latest Browse all 2464

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>