Author: Xinpeng Lin
This Code Snippets will clarify some general process of working with application log when ABAP programmming. Such as creating, adding, deleting, displaying, formatting application logs and saving it to the database.
When we do ABAP programming,sometimes we need to bring the situation arised at runtime to the attention of the end users, which are usually errors, warnings and successful information.To do this, we can use Application Log which is a set of Function Modules provided by SAP for collecting messages, saving, reading and deleting logs in the database and displaying logs.
1.Define application log objects
Application log objects are defined in the system. The object definition assigns a work area. An object can be divided into sub-objects .Logging is performed object-specifically, via function modules.Using transaction code SLG0, we can define our own object and subobject.For example, object: ZTEST, subobject: ZTEST01.
2.Create a log
Using Function Module 'BAL_LOG_CREATE', we can create a log where all messages should be added to.
The function module 'BAL_LOG_CREATE' returns the log handle (LOG_HANDLE, CHAR22). The LOG_HANDLE is a globally unique identifier (GUID) which identifies a log uniquely. You can access this log with this handle, e.g. to change the header data or to put a message in the log.
3. Put a message in the log
Using Function Module 'BAL_LOG_MSG_ADD', we can add a message into the application log.This message is put in the log identified by the log handle GV_LOG_HANDLE, which is mostly the T100 information (message type, work area, message number, the 4 message variables).
4. Display log
Using Function Module 'BAL_DSP_LOG_DISPLAY', we can display the collected messages. This function module can be called without parameters. All messages in memory are displayed in a standard format (this standard format is used e.g. in the transaction SLG1).
5. Save logs in the database
Using Function Module 'BAL_DB_SAVE, we can save all memory data in the database. The importing parameter I_SAVE_ALL should be set as 'X'.
6. Find logs in the database
Using Function Module 'BAL_DB_SEARCH, we can find logs in the database. We should pass the log header filter criteria (structure BAL_S_LFIL), and a table of log headers (structure BALHDR) which satisfy the criteria is returned.
7. Load logs from the database
Using Function Module 'BAL_DB_LOAD', we can load logs from the database. By setting the importing parameter 'I_T_LOG_HEADER' with the return values from function module 'BAL_DB_SEARCH', we can specify which logs are to be loaded in memory.
8. Read log from database
Using Function Module 'APPL_LOG_READ_DB', we can read logs from database if we want to analyze the log ourselves. By setting the importing parameter OBJECT or SUBOBJECT etc., we can specify which logs are to be read.
9. Delete logs from the database
Using Function Module 'BAL_DB_DELETE', we can delete logs from the application. By setting the importing parameter I_T_LOGS_TO_DELETE ' with the return values from function module 'BAL_DB_SEARCH', we can specify which logs are to be loaded in memory.
10. Formatting Log Display
We can use the display profile (structure BAL_S_PROF) to specifiy how the logs are displayed. It contains field catalogs which describe which fields are to be in the list and in the levels of the navigation tree.
Application Log provides display profiles which you can get with the follwoing function modules:
If no display profile is specified, the standard display profile from transaction SLG1 is used.
Let us take a look at these function modules one by one.
The messages are inserted into the navigation tree. The tree-level in which they appear is determined by field DETLEVEL of structure BAL_S_MSG (which is used when adding a message to the log, eg. with BAL_LOG_MSG_ADD).
Presentation of a log whithout a navigation tree next to it. This kind of presentation is usefull when there are not that many messages and therefore a navigation tree would make no sense.
Presentation of a log in a popup. This is similar to the previous point. No tree is shown (does not make sense in a popup, not enough space).
the 'standard' profile to display one log:
In the tree the log header is displayed. One level below there are different categories for the problem classes of the messages. The user can easily select for example only
important messages. All messages of the log are displayed at once on the right side.
This profile is used in transaction SLG1 when only one log has been selected.
The 'standard' profile to display many logs.
This is similar to the previous profile, the only diffrences are:
- On the right side the overview of log headers is not expanded down to the problem class level.
- No messages ar shown initially. The user has to choose a log (or a part of a log, e.g. the very important messages of a log)
This profile is used in transaction SLG1 when more than one log has been selected.
As the process of these function module is similar, let us take function module 'BAL_DSP_PROFILE_DETLEVEL_GET' as a example.
11. Callback Routine
Using callback routines, we can affect the program flow at various events in Application Log. Callback routines can be FORM routines or function modules.
The following is a example which will popup a message whenever a message is sent to the application log.