Application Logging in ABAP
Whether you are working with integration or a critical application or a complex
report, logging becomes an obvious choice and activity.
Now our application code needs to start logging in to this new object and
subobject.
Traditionally, Abapers have been logging information using a custom DB table
defined specifically for a particular application.
While useful, The approach has some flaws,
- Every application needs to have its own logging table
- Record purging/archiving needs to be set up
in order to make sure old records are either deleted
or archived.
- There is no reusability of either the setup
or the logic to log.
With the introduction of Application Logging from SAP, the traditional
approach can be left out.
In this post, I will be talking about how we can leverage the SAP application
Logging Library to make logging easy.
What is an application log?
An application log is a rich type log. It contains information like User,
Time, Expiry date of the log along with the actual message raised by the
application. You can categorise between success, error, and warning or provide
extra information in a form of a structure of no more than 255 characters.
Each application log is tied to an application object/subobject which defines
the application activity/subactivity.
There are three main tcodes to Application Logging:
1. SLG0: - Defining the object/subobject.
2. SLG1: - Viewing the logs
3. SLG2: - Deleting the logs.
Let's start with defining an Object.
Starting the log:
You open the log Using Function Module BAL_LOG_CREATE. You can pass the
log header data to this function module in the importing parameter
I_S_LOG_HEADER with the structure BAL_S_LOG.
The function module returns the log handle which is a GUID and
identifies a log uniquely. This log handle can be used either to change the
log header data later or to put messages in the log.
Adding Messages to Logs:
Viewing the Log:
Simple Example:
You can add messages to your newly created log header identified by its
handle.
The message data is provided in the function module BAL_LOG_MSG_ADD by the importing parameter I_S_MSG (structure BAL_S_MSG). This data is mostly the T100 information (message type, work area, message number, the 4 message variables). You can also pass other information, e.g. application-specific data (context), parameters for a long text and a callback routine. Simple Example:
Saving the Log to DB.
The message data is provided in the function module BAL_LOG_MSG_ADD by the importing parameter I_S_MSG (structure BAL_S_MSG). This data is mostly the T100 information (message type, work area, message number, the 4 message variables). You can also pass other information, e.g. application-specific data (context), parameters for a long text and a callback routine. Simple Example:
Saving the Log to DB.
You can save the log to the database using the function module:
BAL_DB_SAVE by providing the log handle.
Example:
Viewing the Log:
You can view the log in SLG1 Tcode.
Input screen:
Here is the Output:
Below is the code snippet that generated the above log with all three
components explained earlier.
As always, Hope you like the post and use the information in your day to day
work.