The goal of the object is to create the Global Class and Method for Application Log so that it can be reused where necessary in any RICEF object.
Application Log provides an infrastructure for collecting messages and exceptions in a log, saving, reading and deleting logs in the database and displaying them.
Application Log provides multiple advantages:
- System-wide uniform event logging
- Accessible standardized UI based on ABAP List Viewer (ALV)
- High capabilities for analysis of log data
Application logging records the progress of the execution of an application. Whereas the system log records system events, you can use the application log to record application-specific events.
Application Log is designed to temporarily store messages. Logs should be deleted in intervals (e.g. weekly batch job for deleting logs) to avoid too high database load.
A typical use of the Application Log is within delivery processing. Negative results of a dangerous goods check are written to the Application Log. Application messages and reactions are collected based on a customer defined examination schema (reactions determine how the document will be handled further). This approach increases the transparency of the process for end users. Messages are collected temporarily and are not saved to the database.
Create Custom Application log objects
First create object and sub object, Go to transaction SLG0. Go to change mode and click on New entries button on the application tool bar.
Then define the sub object for the object: Select the object and double click on sub object:
Create Global Class and Method to define the Application log code:
Define that Class and method in the code wherever necessary:
The Method will create log with all the messages and generate log (Can also check in Transaction SLG1):
Processing Logic
Define the following Parameter in method
Define following Data in the Method
Populate the Header data with the following details
Create the log with FM BAL_LOG_CREATE
Add all the message in the log using FM BAL_LOG_MSG_ADD
Save the Log using FM BAL_DB_SAVE
Display the log using FM BAL_DSP_LOG_DISPLAY
Coding
METHOD CALL_APPLICATION_LOG.
***********************************************************************
* Data declaration
***********************************************************************
* Workarea
DATA: lwa_s_log TYPE bal_s_log, "Appl Log:header data
lwa_balloghndl TYPE balloghndl, "Appl Log: Log Handle
lt_balloghndl TYPE bal_t_logh.
CONSTANTS: lco_x TYPE char1 VALUE 'X'.
FIELD-SYMBOLS: <lfs_msg> TYPE bal_s_msg.
CLEAR lwa_s_log.
lwa_s_log-object = i_object.
lwa_s_log-subobject = i_subobject.
lwa_s_log-aldate = sy-datum. "Date
lwa_s_log-altime = sy-uzeit. "Time
lwa_s_log-aluser = sy-uname. "user name
lwa_s_log-altcode = sy-tcode. "Transaction code
lwa_s_log-alprog = sy-cprog. "Program name
*- create log
CALL FUNCTION 'BAL_LOG_CREATE'
EXPORTING
i_s_log = lwa_s_log
IMPORTING
e_log_handle = lwa_balloghndl
EXCEPTIONS
log_header_inconsistent = 1
OTHERS = 2.
IF sy-subrc <> 0.
CLEAR lwa_balloghndl.
ELSE.
*- Add all error messages
IF <lfs_msg> IS ASSIGNED.
UNASSIGN <lfs_msg>.
ENDIF.
LOOP AT i_msg ASSIGNING <lfs_msg>.
CALL FUNCTION 'BAL_LOG_MSG_ADD'
EXPORTING
i_log_handle = lwa_balloghndl
i_s_msg = <lfs_msg>
EXCEPTIONS
log_not_found = 1
msg_inconsistent = 2
log_is_full = 3
OTHERS = 4.
IF sy-subrc NE 0.
REFRESH lt_balloghndl.
ENDIF.
ENDLOOP.
* Save the log
CALL FUNCTION 'BAL_DB_SAVE'
EXPORTING
i_client = sy-mandt
i_save_all = lco_x
EXCEPTIONS
log_not_found = 1
save_not_allowed = 2
numbering_error = 3
OTHERS = 4.
IF sy-subrc EQ 0.
REFRESH lt_balloghndl.
ENDIF.
*-- Display Application Log
APPEND lwa_balloghndl TO lt_balloghndl.
CALL FUNCTION 'BAL_DSP_LOG_DISPLAY'
EXPORTING
i_t_log_handle = lt_balloghndl
i_amodal = space
EXCEPTIONS
profile_inconsistent = 1
internal_error = 2
no_data_available = 3
no_authority = 4
OTHERS = 5.
IF sy-subrc NE 0.
REFRESH lt_balloghndl.
ENDIF.
ENDIF.
ENDMETHOD.