SAP ABAP Application Log 的应用场景:

(1) 当你想记录应用程序的执行进度,以便当前在须要时重建它;
(2) 当开发人员不确定/无奈调试代码(前台/后盾)并且想深刻理解谬误起因时,能够通过查看应用程序日志来确定问题所在的确切地位。

应用事物码 SLG0 创立一个对象,该对象用于辨认开发人员在 ABAP 代码里应用的 Application log.

这里创立一个名叫 ZHR_ENC 的应用程序日志对象。

再创立一个子对象 ZHRENC_SUB。

这里的场景是,为 COUNTRY 治理创立一个应用程序日志。Country 能够蕴含不同的 State,这些 State 的执行状况,通过子对象 ZHRENC_SUB 来记录。

测试代码:

type-pools: abap.data: l_log_handle type balloghndl,l_timestamp type tzntstmps,l_timezone type timezone value ‘UTC’,l_str_log type bal_s_log,l_str_balmsg type bal_s_msg,l_str_message type bapiret2,l_msg_logged type boolean,l_tab_messages type bapiret2_t.*-Building messages*–Use your own message which you want to Display in the logdo 1 times.call function ‘BALW_BAPIRETURN_GET2’exportingtype = ‘E’cl = ‘BPFS’number = ‘006’importingreturn = l_str_message.append l_str_message to l_tab_messages.clear l_str_message.enddo.*-Logging messagesconvert date sy–datum time sy–uzeitinto time stamp l_timestamp time zone l_timezone.l_str_log–extnumber = l_timestamp.condense l_str_log–extnumber.l_str_log–object = ‘ZHR_ENC’.l_str_log–subobject = ‘ZHRENC_SUB’.l_str_log–aldate_del = sy–datum + 5.call function ‘BAL_LOG_CREATE’exportingi_s_log = l_str_logimportinge_log_handle = l_log_handleexceptionslog_header_inconsistent = 1others = 2.if sy–subrc <> 0.message id sy–msgid type sy–msgty number sy–msgnowith sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4 into l_str_message–message.write: ‘Type’,sy–msgty, ‘Message’,l_str_message–message.else.loop at l_tab_messages into l_str_message.move: l_str_message–type       to l_str_balmsg–msgty,l_str_message–id         to l_str_balmsg–msgid,l_str_message–number     to l_str_balmsg–msgno,l_str_message–message_v1 to l_str_balmsg–msgv1,l_str_message–message_v2 to l_str_balmsg–msgv2,l_str_message–message_v3 to l_str_balmsg–msgv3,l_str_message–message_v4 to l_str_balmsg–msgv4.call function ‘BAL_LOG_MSG_ADD’exportingi_log_handle = l_log_handlei_s_msg = l_str_balmsgimportinge_msg_was_logged = l_msg_loggedexceptionslog_not_found = 1msg_inconsistent = 2log_is_full = 3others = 4.if sy–subrc <> 0.message id sy–msgid type sy–msgty number sy–msgnowith sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4 into l_str_message–message.write: ‘Type’,sy–msgty, ‘Message’,l_str_message–message.endif.endloop.if sy–subrc eq 0.call function ‘BAL_DB_SAVE’exportingi_save_all = abap_trueexceptionslog_not_found = 1save_not_allowed = 2numbering_error = 3others = 4.if sy–subrc <> 0.message id sy–msgid type sy–msgty number sy–msgnowith sy–msgv1 sy–msgv2 sy–msgv3 sy–msgv4 into l_str_message–message.write: ‘Type’,sy–msgty, ‘Message’,l_str_message–message.else.write: ‘Messages Saved in the log’.endif.endif.endif.write : ‘done with log number’,l_str_log–extnumber.

在下面的代码中:

  • BAL_LOG_CREATE:创立应用程序日志对象
  • BAL_LOG_MSG_ADD:它将音讯增加到应用程序日志记录对象/子对象
  • BAL_DB_SAVE:将音讯保留到数据库中。

能够应用事务代码 SLG1 来监督基于对象的应用程序日志记录。