如何使用應用日誌(Application Log)
阿新 • • 發佈:2018-12-14
轉載:
SAP的應用日誌(Application Log)是用於建立,儲存和分析系統訊息的工具. 相關TCODE: SLG0: Creation of Object and Sub object SLG1: Display Application Logs 相關建立應用日誌函式 BAL_LOG_CREATE --> Create log with header data BAL_LOG_MSG_ADD --> Put message in log BAL_DB_SAVE --> Save logs in the database 建立應用日誌的處理步驟: 1: 使用TCODE:SLG0建立物件和子物件. 2: 建立物件,物件名以Z或Y開頭. 3: 建立物件後,你將建立子物件. 4: 如果相應的子物件不存在,則建立子物件. 5: 這樣物件和子物件就可以在應用日誌中使用了. 6: 使用下面三個函式建立和儲存應用日誌 7: 使用'BAL_LOG_CREATE' 建立日誌控制代碼(log handle) 8: 使用'BAL_LOG_MSG_ADD' 新增訊息, 9: 使用'BAL_DB_SAVE' 儲存日誌 如何檢視應用日誌? 1. 輸入TCODE: SLG1.系統將出現分析應用日誌的螢幕. 2. 輸入物件,子物件和外部標示符. 3. 輸入時間. 4. 規定日誌的原因 5. 選擇日誌類別和建立日誌. 6. 執行. 系統將顯示結果. SAP的程式碼例項: SBAL_DEMO_06 ---------------------
report sbal_demo_06 . *********************************************************************** *********************************************************************** * REPORT SBAL_DEMO_06 * * The application log allows to add application specific data to * a log header or a message. * * One simple possibility is to use the context. This allows to * to add the content of a (flat, non-hierarchical) DDIC-structure * to a log header or a message (sie sub-structure 'CONTEXT' in * structure BAL_S_LOG and BAL_S_MSG). * There is already an example in Report SBAL_DEMO_02 for this * (see FORM msg_add_with_context). * * But sometimes a simple, flat DDIC-structure is not sufficient. * If you want to add more complex data (like an internal table, * a complex data type, etc.) to a log or a message, * you can use table BAL_INDX. * * BAL_INDX is an INDX-like table which can be filled and read * with the ABAP-statement EXPORT/IMPORT. * This report shows an example how to use BAL_INDEX. * * This report has three options: * o create log * o display log * o delete log * * create log: * ========== * The log which is created consists of a log header * and only one message. For both, log header and message * the parameters are defined (see sub-structure 'PARAMS' in * BAL_S_LOG and BAL_S_MSG). * The parameters are filled and callback routines are defined. * When the log is saved, also some internal tables containing * further data are saved via EXPORT TO BAL_INDX * (see FORM log_save) * * display log: * =========== * The log is searched on the database, loaded and displayed. * When the detail of a message or the log header is selected * by the user, the callback-routines are called. * In this callback-routine the internal tables are read * with 'IMPORT FROM BAL_INDX'. * (see FORM CALLBACK_LOG_DETAIL or FORM CALLBACK_MSG_DETAIL, both * call FORM LOAD_MY_DATA). * * delete log: * =========== * The log is searched on the database and deleted. * This deletion also deletes the data in table BAL_INDX for this * 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. ---------------------