From http://blog.csdn.net/CompassButton/archive/2007/10/09/1816355.aspx
SAP provides standard functions to operate application logs.ArticleThis section describes in detail how to use application logs.
1. Related tcode
• Slg0-> used to maintain the log object
• Slg1-> used to view the log
2. configuration steps
1) Run slg0. A Prompt window is displayed, and click "OK;
2) Click "add", enter "ztestlog" in the object name, "test log" in the description, and click "save;
3. Development steps
1) Use SE11 to create a structure z_log_message. The structure fields are as follows:
Component |
Component Type |
Msgty |
Symsgty |
Msg_text_1 |
Symsgv |
Msg_text_2 |
Symsgv |
Msg_text_3 |
Symsgv |
Msg_text_4 |
Symsgv |
2) use se91 to create a message class zmessage and enter four placeholders (&) in step 999 (&)
3) Use se37 to create the function family "zlog"
4) Create the function "ziu_message_logging" after creating the function group"
Import parameters:
I _log_object type balobj_d-> Application Log: Object Name (application code)
I _extnumber type string-> Application Log: External ID
Export Parameters : None
Changing parameters: None
Tables parameters:
T_log_message type z_log_message
Exceptions:
Log_header_inconsistent
Logging Error
Code As follows:
Function ziu_message_logging.
*"----------------------------------------------------------------------
* "*" Local interface:
* "Importing
* "Reference (I _log_object) type balobj_d
* "Reference (I _extnumber) type string
* "Tables
* "T_log_message structure z_log_message
* "Exceptions
* "Log_header_inconsistent
* "Logging_error
*"----------------------------------------------------------------------
* Author: ashim Chowdhury
* Description: This function module is used insert messages in
* Application Log
Constants: c_message type syst-msgid value 'zmessage ',
C_999 type syst-msgno value '201312 '.
Data:
Rochelle _ HANDLE type balloghndl,
L_s_log type bal_s_log,
Rochelle dummy type string,
Rochelle ext_no type bal_s_log-extnumber,
L_s_mdef type bal_s_mdef.
If t_log_message [] is not initial.
L_s_log-object = I _log_object.
L_ext_no = I _extnumber.
L_s_log-extnumber = l_ext_no.
* Create the log with header data
Call function 'bal _ log_create'
Exporting
I _s_log = l_s_log
Importing
E_log_handle = l_log_handle
Exceptions
Log_header_inconsistent = 1
Others = 2.
If sy-subrc <> 0.
Case sy-subrc.
When 1.
Raise log_header_inconsistent.
When others.
Raise logging_error.
Endcase.
Endif.
The l_s_mdef-log_handle = l_log_handle.
* Set the default value
Call function 'bal _ glb_msg_defaults_set'
Exporting
I _s_msg_defaults = l_s_mdef
Exceptions
Others = 0.
* Loop the message table and write the messages into the log
Loop at t_log_message.
* Use the Message Type zmessage and MSG no 999
* Issue the message in a dummy variable
Message ID c_message type t_log_message-MSGTY number c_999
With t_log_message-MSG_TEXT_1 t_log_message-MSG_TEXT_2
T_log_message-MSG_TEXT_3 t_log_message-MSG_TEXT_4
Into l_dummy.
* The parameters set by message statement will be used
* Add the message in the log
Perform msg_add.
Endloop.
* Save logs in the database
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.
Endif.
Endfunction.
*--------------------------------------------------------------------
* Form msg_add
*--------------------------------------------------------------------
* Add the message to the log
*-------------------------------------------------------------------*
Form msg_add.
Data:
L_s_msg type bal_s_msg.
* Define data of message for Application Log
L_s_msg-msgty = sy-msgty.
L_s_msg-msgid = sy-msgid.
L_s_msg-msgno = sy-msgno.
L_s_msg-msgv1 = sy-msgv1.
L_s_msg-msgv2 = sy-msgv2.
L_s_msg-msgv3 = sy-msgv3.
L_s_msg-msgv4 = sy-msgv4.
* Add this message to log file
* (I _log_handle is not specified, we want to add to the default log.
* If it does not exist we do not care => exceptions log_not_found = 0)
Call function 'bal _ log_msg_add'
Exporting
* I _log_handle =
I _s_msg = l_s_msg
Exceptions
Log_not_found = 0
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.
5) in Program Use the ziu_message_logging function to maintain application logs. The Code is as follows:
Data Declaration->
* Internal table for message logging
Data: it_log_message type standard table of z_log_message,
Wa_log_message type z_log_message,
Rochelle ext_number type string.
Constants:
C_obj_zxiproxy type balobj_d value 'ztestlog '.
* Now populate the internal table with the log messages as shown below. wa_log_message-
* Msgty is the type of the message.
* E-> error, W-> warning, S-> success
* Logging code for Insert Message into log
Clear wa_log_message.
Wa_log_message-msgty = 'E'. "can use W or S
Wa_log_message-msg_text_1 = <message text 1>.
Wa_log_message-msg_text_2 = <message text 2>
Wa_log_message-msg_text_3 = <Message Text 3>
Wa_log_message-msg_text_4 = <message text 4>
* Append the message into the internal table
Append wa_log_message to it_log_message.
At the end transfer the log message to the system log by calling function module ziu_message_logging. l_ext_number will BT any string of your choice.
* Function module ziu_message_logging will do the logging
* I _log_object is the object type (to be configrd using txn slg0
Call function 'ziu _ message_logging'
Exporting
I _log_object = c_obj_zxiproxy
I _extnumber = l_ext_number
Tables
T_log_message = it_log_message
Exceptions
Log_header_inconsistent = 1
Logging_error = 2
Others = 3.
If sy-subrc <> 0.
* Message ID SY-MSGID type SY-MSGTY number SY-MSGNO
* With SY-MSGV1 SY-MSGV2 SY-MSGV3.
Endif.
Endif.
4. Use tcode: slg1 to view application logs.