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. 
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.

Creating ABAP application Log object in SLG0

Click on New Entries and Add a new Object:

Creating ABAP application Log object in SLG0

Add a sub-object(Optional):

Creating ABAP application Log sub object in SLG0


Now our application code needs to start logging in to this new object and subobject.

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.
Simple Example:

Adding Messages to Logs:

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.
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:
Viewing logs in SLG1 input screen

Here is the Output:
Viewing logs in SLG1 output screen

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.

Popular posts from this blog

ABAP convert internal table to excel (.xlsx) format and Send email or download

ABAP read excel(.XLSX) file to internal table in ABAP using CL_FDT_XL_SPREADSHEET