专栏首页SAP TechnicalSAP 应用日志(Application Log)的使用
原创

SAP 应用日志(Application Log)的使用

Application log(应用日志)是SAP内部用于记录application运行的事件日志,通常可用作message collector。

-

在PackageSZAL下,SAP提供了一系列的标准函数,通过复用这些函数,可以轻松地根据项目需求,实现application log的创建、保存、查询、删改等功能。通常,在3.0 release的版本中的SAP的application log相关的Function Modle 以APPL_LOG_*开始;Function Module以BAL_*为前缀从Release 4.6开始。

常用的transaction -

  1. SLG0: 创建Application Log的Object 和sub-object, 一个object下,可以有多个sub-object。在程序中保存log时,会指定具体保存在哪个object上。
  2. SLG1: 查看Application log
  3. SLGD: 查看Application log(详细的筛选条件)

常用的的3个function module:

  1. BAL_LOG_CREATE:创建
  2. BAL_LOG_MSG_ADD: 写入一条log
  3. BAL_DSP_LOG_DISPLAY: 显示log

常用的demo程序:

  1. SBAL_DEMO_06 (最简单的create,save, delete)
  2. SBAL_DEMO_04 (花式display log): log可以display成不同的样式,例如单条log,多条log,树状显示,层级显示等等。

显示多条Log

多层级记录Log

在某个sub-screen中显示Log

示例代码

***********************************************************************
******************** SELECTION SCREEN *********************************
***********************************************************************
PARAMETERS:
  p_create  RADIOBUTTON GROUP par,
  p_disp    RADIOBUTTON GROUP par,
  p_delete  RADIOBUTTON GROUP par.
***********************************************************************
******************** CONSTANTS, TYPES, DATA ***************************
***********************************************************************
SET EXTENDED CHECK OFF.
INCLUDE sbal_constants.
SET EXTENDED CHECK ON.
TABLES:
  bal_indx.
CONSTANTS:
  const_example_object    TYPE bal_s_log-object    VALUE 'BCT1',
  const_example_extnumber TYPE bal_s_log-extnumber VALUE 'BAL_INDX',
  const_name_msg_ident(9) TYPE c                   VALUE 'MSG_IDENT'.
DATA:
  g_identifier(10)        TYPE n,
  g_lognumber             TYPE balhdr-lognumber.
* these are our own data we want to save with the application log:
DATA:
  g_my_header_data        TYPE bal_s_ex05 OCCURS 0 WITH HEADER LINE,
  BEGIN OF g_my_message_data OCCURS 0,
    identifier            LIKE g_identifier,
    t_my_data             TYPE bal_s_ex06 OCCURS 0,
  END OF g_my_message_data.

***********************************************************************
******************** MAIN PROGRAM *************************************
***********************************************************************
END-OF-SELECTION.

* create log
  IF NOT p_create IS INITIAL.
    PERFORM log_create.
  ENDIF.

* display log
  IF NOT p_disp IS INITIAL.
    PERFORM log_display.
  ENDIF.

* delete log
  IF NOT p_delete IS INITIAL.
    PERFORM log_delete.
  ENDIF.


***********************************************************************
************** FORMS FOR CREATION OF THE LOG *************************
***********************************************************************
*--------------------------------------------------------------------
* FORM log_create.
*--------------------------------------------------------------------
FORM log_create.
  DATA:
    l_log_handle TYPE balloghndl.

* create log header with information about the carriers and
* connection which are calculated in this transaction
  PERFORM log_header_create
            CHANGING
              l_log_handle.

* create the message
  PERFORM log_message_create
            USING
              l_log_handle.

* save the application log and our data
  PERFORM log_save
            USING
              l_log_handle.

ENDFORM.
*--------------------------------------------------------------------
* FORM log_header_create
*--------------------------------------------------------------------
FORM log_header_create
       CHANGING
         c_log_handle   TYPE balloghndl.

  DATA:
    l_s_log     TYPE bal_s_log.


* create log header data
  CLEAR l_s_log.
  l_s_log-object    = const_example_object.
  l_s_log-extnumber = const_example_extnumber.

* define callback routine
  l_s_log-params-callback-userexitp = sy-repid.
  l_s_log-params-callback-userexitf = 'CALLBACK_LOG_DETAIL'.
  l_s_log-params-callback-userexitt = const_callback_form.

* create the log header
  CALL FUNCTION 'BAL_LOG_CREATE'
       EXPORTING
            i_s_log      = l_s_log
       IMPORTING
            e_log_handle = c_log_handle
       EXCEPTIONS
            OTHERS       = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* we want to store some information in the log header
* to describe which carriers and flight were handled in this log
  g_my_header_data-carrid     = 'AB'.  "#EC NOTEXT
  g_my_header_data-txt_carrid = 'Airways AB'.           "#EC NOTEXT
  g_my_header_data-connid     = '0003'."#EC NOTEXT
  g_my_header_data-txt_connid = 'Hamburg - New York'(001).
  APPEND g_my_header_data.
  g_my_header_data-carrid     = 'XY'.  "#EC NOTEXT
  g_my_header_data-txt_carrid = 'XY Lines'.             "#EC NOTEXT
  g_my_header_data-connid     = '0002'."#EC NOTEXT
  g_my_header_data-txt_connid = 'Walldorf - Tokio'(002).
  APPEND g_my_header_data.
  g_my_header_data-carrid     = 'ZZ'.  "#EC NOTEXT
  g_my_header_data-txt_carrid = 'ZZ Wings'.             "#EC NOTEXT
  g_my_header_data-connid     = '0014'."#EC NOTEXT
  g_my_header_data-txt_connid = 'Paris - Frankfurt'(003).
  APPEND g_my_header_data.

ENDFORM.
*--------------------------------------------------------------------
* FORM log_message_create
*--------------------------------------------------------------------
FORM log_message_create
       USING
         i_log_handle   TYPE balloghndl.

  DATA:
    l_s_msg     TYPE bal_s_msg,
    l_s_par     TYPE bal_s_par,
    l_s_my_data TYPE bal_s_ex06.


* create a message
* 327(BL): "&1 customers were allowed to fly for free (see detail)"
  CLEAR l_s_msg.
  l_s_msg-msgty = 'E'.
  l_s_msg-msgid = 'BL'.
  l_s_msg-msgno = '327'.
  l_s_msg-msgv1 = '3'.

* define callback routine
  l_s_msg-params-callback-userexitp = sy-repid.
  l_s_msg-params-callback-userexitf = 'CALLBACK_MSG_DETAIL'.
  l_s_msg-params-callback-userexitt = const_callback_form.

* define an identifer. This is used to establish the link between
* the message and its additional data
  ADD 1 TO g_identifier.

* put his identifier into the parameters of the message
  l_s_par-parname = const_name_msg_ident.
  l_s_par-parvalue   = g_identifier.
  APPEND l_s_par TO l_s_msg-params-t_par.

* create the message
  CALL FUNCTION 'BAL_LOG_MSG_ADD'
       EXPORTING
            i_log_handle = i_log_handle
            i_s_msg      = l_s_msg
       EXCEPTIONS
            OTHERS       = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* we want to store information for this message about the customers
* which were allowed to fly for free:
  g_my_message_data-identifier  = g_identifier.
  l_s_my_data-id          = '00000002'.
  l_s_my_data-txt_id      = 'Peter Smith'.          "#EC NOTEXT
  APPEND l_s_my_data TO g_my_message_data-t_my_data.
  l_s_my_data-id          = '00000013'.
  l_s_my_data-txt_id      = 'Paula Jones'.          "#EC NOTEXT
  APPEND l_s_my_data TO g_my_message_data-t_my_data.
  l_s_my_data-id          = '00001345'.
  l_s_my_data-txt_id      = 'Jane Meyer'.           "#EC NOTEXT
  APPEND l_s_my_data TO g_my_message_data-t_my_data.
  APPEND g_my_message_data.

ENDFORM.

*--------------------------------------------------------------------
* FORM log_save
*--------------------------------------------------------------------
FORM log_save
       USING
         i_log_handle    TYPE balloghndl.

  DATA:
    l_t_log_handle       TYPE bal_t_logh,
    l_s_new_lognumber    TYPE bal_s_lgnm,
    l_t_new_lognumbers   TYPE bal_t_lgnm.


* save this log
  INSERT i_log_handle INTO TABLE l_t_log_handle.
  CALL FUNCTION 'BAL_DB_SAVE'
       EXPORTING
            i_t_log_handle   = l_t_log_handle
       IMPORTING
            e_new_lognumbers = l_t_new_lognumbers
       EXCEPTIONS
            OTHERS           = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* find out the lognumber of this saved log
  READ TABLE l_t_new_lognumbers INTO l_s_new_lognumber
             WITH KEY log_handle = i_log_handle.
  CHECK sy-subrc = 0.
  g_lognumber = l_s_new_lognumber-lognumber.

* also save our own, complex data:
  EXPORT g_my_header_data g_my_message_data
         TO DATABASE bal_indx(al)
         ID g_lognumber.

ENDFORM.

***********************************************************************
************** FORMS FOR DISPLAY OF THE LOG **************************
***********************************************************************
*--------------------------------------------------------------------
* FORM log_display
*--------------------------------------------------------------------
FORM log_display.
  DATA:
    l_s_log_filter     TYPE bal_s_lfil,
    l_s_obj            TYPE bal_s_obj,
    l_s_extn           TYPE bal_s_extn,
    l_t_log_header     TYPE balhdr_t.

* create filter to search for this log on db
  CLEAR l_s_log_filter-object.
  CLEAR l_s_obj.
  l_s_obj-sign = 'I'.
  l_s_obj-option = 'EQ'.
  l_s_obj-low    = const_example_object.
  APPEND l_s_obj TO l_s_log_filter-object.
  CLEAR l_s_extn.
  l_s_extn-sign = 'I'.
  l_s_extn-option = 'EQ'.
  l_s_extn-low    = const_example_extnumber.
  APPEND l_s_extn TO l_s_log_filter-extnumber.

* search for this log
  CALL FUNCTION 'BAL_DB_SEARCH'
       EXPORTING
            i_s_log_filter = l_s_log_filter
       IMPORTING
            e_t_log_header = l_t_log_header
       EXCEPTIONS
            OTHERS         = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* load these messages into memory
  CALL FUNCTION 'BAL_DB_LOAD'
       EXPORTING
            i_t_log_header = l_t_log_header
       EXCEPTIONS
            OTHERS         = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* show this log:
* - we do not specify the display profile I_DISPLAY_PROFILE since
*   we want to use the standard profile
* - we do not specify any filter (like I_S_LOG_FILTER, ...,
*   I_T_MSG_HANDLE) since we want to display all messages available
  CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
*      EXPORTING
*           I_S_LOG_FILTER         =
*           I_T_LOG_CONTEXT_FILTER =
*           I_S_MSG_FILTER         =
*           I_T_MSG_CONTEXT_FILTER =
*           I_T_LOG_HANDLE         =
*           I_T_MSG_HANDLE         =
*           I_S_DISPLAY_PROFILE    =
       EXCEPTIONS
            OTHERS = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.
*--------------------------------------------------------------------
* FORM CALLBACK_LOG_DETAIL
*--------------------------------------------------------------------
FORM callback_log_detail               "#EC CALLED
       TABLES
         i_params  STRUCTURE spar.

* load my specififc data from database
  PERFORM load_my_data
             TABLES
               i_params.

* display header data
  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
       EXPORTING
            i_structure_name      = 'BAL_S_EX05'
            i_screen_start_column = 1
            i_screen_start_line   = 1
            i_screen_end_column   = 80
            i_screen_end_line     = 10
       TABLES
            t_outtab              = g_my_header_data
       EXCEPTIONS
            OTHERS                = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.
*--------------------------------------------------------------------
* FORM CALLBACK_MSG_DETAIL
*--------------------------------------------------------------------
FORM callback_msg_detail               "#EC CALLED
       TABLES
         i_params     STRUCTURE spar.

  DATA:
    l_my_message_data TYPE bal_s_ex06 OCCURS 0.


* load my specififc data from database
  PERFORM load_my_data
             TABLES
               i_params.

* find out the identifier for this message
  READ TABLE i_params WITH KEY param = const_name_msg_ident.
  CHECK sy-subrc = 0.
  g_identifier = i_params-value.

* search for those entries which belong to thgis message
  READ TABLE g_my_message_data WITH KEY identifier = g_identifier.
  CHECK sy-subrc = 0.
  l_my_message_data = g_my_message_data-t_my_data.

* display header data
  CALL FUNCTION 'REUSE_ALV_LIST_DISPLAY'
       EXPORTING
            i_structure_name      = 'BAL_S_EX06'
            i_screen_start_column = 1
            i_screen_start_line   = 1
            i_screen_end_column   = 80
            i_screen_end_line     = 10
       TABLES
            t_outtab              = l_my_message_data
       EXCEPTIONS
            OTHERS                = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
           WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.

*--------------------------------------------------------------------
* FORM LOAD_MY_DATA
*--------------------------------------------------------------------
FORM load_my_data
       TABLES
         i_params  STRUCTURE spar.

  DATA:
    l_lognumber TYPE balhdr-lognumber.

* find out the log number of this log which is displayed
* (this number is automatically added by the display module)
  READ TABLE i_params WITH KEY param = bal_param_lognumber.
  IF sy-subrc = 0.
    l_lognumber = i_params-value.
  ENDIF.

* when number has changed, load these data
  IF g_lognumber NE l_lognumber.
    g_lognumber = l_lognumber.
    IMPORT g_my_header_data g_my_message_data
    FROM DATABASE bal_indx(al)
    ID g_lognumber.
    IF sy-subrc NE 0.
      CLEAR:
       g_my_header_data[],
       g_my_message_data[].
    ENDIF.
  ENDIF.

ENDFORM.

***********************************************************************
************** FORMS FOR DELETION OF THE LOG *************************
***********************************************************************
*--------------------------------------------------------------------
* FORM log_delete
*--------------------------------------------------------------------
FORM log_delete.
  DATA:
    l_s_log_filter     TYPE bal_s_lfil,
    l_s_obj            TYPE bal_s_obj,
    l_s_extn           TYPE bal_s_extn,
    l_t_log_header     TYPE balhdr_t.

* create filter to search for this log on db
  CLEAR l_s_log_filter-object.
  CLEAR l_s_obj.
  l_s_obj-sign = 'I'.
  l_s_obj-option = 'EQ'.
  l_s_obj-low    = const_example_object.
  APPEND l_s_obj TO l_s_log_filter-object.
  CLEAR l_s_extn.
  l_s_extn-sign = 'I'.
  l_s_extn-option = 'EQ'.
  l_s_extn-low    = const_example_extnumber.
  APPEND l_s_extn TO l_s_log_filter-extnumber.

* search for this log
  CALL FUNCTION 'BAL_DB_SEARCH'
       EXPORTING
            i_s_log_filter = l_s_log_filter
       IMPORTING
            e_t_log_header = l_t_log_header
       EXCEPTIONS
            OTHERS         = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

* delete these logs
  CALL FUNCTION 'BAL_DB_DELETE'
       EXPORTING
            i_t_logs_to_delete = l_t_log_header
       EXCEPTIONS
            OTHERS             = 1.
  IF sy-subrc <> 0.
    MESSAGE ID sy-msgid TYPE sy-msgty NUMBER sy-msgno
            WITH sy-msgv1 sy-msgv2 sy-msgv3 sy-msgv4.
  ENDIF.

ENDFORM.

原创声明,本文系作者授权云+社区发表,未经许可,不得转载。

如有侵权,请联系 yunjia_community@tencent.com 删除。

我来说两句

0 条评论
登录 后参与评论

相关文章

  • ABAP 应用日志(Application Log)的使用

    Application log(应用日志)是SAP内部用于记录application运行的事件日志,通常可用作message collector。 - 在P...

    matinal
  • SAP日志LOG查看:SLG0,SLG1

    *----------------------------------------------------------------------*

    matinal
  • 如何在abap中使用日志管理

    1、相关TCODE •SLG0 -> Used to maintain the log object •SLG1 -> Used to view the l...

    matinal
  • ABAP 应用日志(Application Log)的使用

    Application log(应用日志)是SAP内部用于记录application运行的事件日志,通常可用作message collector。 - 在P...

    matinal
  • JIT中的LogCompilation

    我们知道在JVM中为了加快编译速度,引入了JIT即时编译的功能。那么JIT什么时候开始编译的,又是怎么编译的,作为一个高傲的程序员,有没有办法去探究JIT编译的...

    程序那些事
  • linux每日命令(35):grep命令

    Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来。grep全称是Global Regular Expre...

    用户1214487
  • SAP日志LOG查看:SLG0,SLG1

    *----------------------------------------------------------------------*

    matinal
  • [PHP] PHP在CLI环境下的错误日志

    1. display_errors = Off;//控制php是否输出错误;在生产环境中输出会泄露敏感信息;建议记录错误而不是将它们发送到STDOUT off ...

    陶士涵
  • 数据库PostrageSQL-开发者选项

    下面的参数目的是用在PostgreSQL源代码上, 并且在某些情况下可以帮助恢复严重损坏了的数据库。在一个生产数据库中没有理由使用它们。同样,它们被从例子pos...

    cwl_java
  • AtomicReference原子性引用

    入门小站

扫码关注云+社区

领取腾讯云代金券