Reusable Logger Class in ABAP – Simple Logging in Reports

Estimated read time 7 min read

Introduction: 

In many ABAP reports or background jobs, we often write WRITE: or MESSAGE statements just for debugging or tracking execution. 

But what if we had a reusable logger class that: Keeps all logs organized Categorizes logs (INFO, ERROR, SUCCESS, etc.)  Works in any report or enhancement Allows us to download or display logs 

This post shows how to use a global logger class to make that happen — clean, simple, and reusable across your SAP system. 

What We’re Building 
We’ll create and use a global class ZGCL_LOGGER with these methods: ADD_LOGS( ) – Add a new log entry with type and message DOWNLOAD_LOGS( ) – Download the collected logs to a text file DISPLAY_LOGS( ) – Display the collected logs in a ALVCLEAR_LOGS( ) – Clear the collected logsWe can use this logger in any report, like this: 

             DATA(obj_log) = NEW zgcl_logger( ). “Create a object for Log class and access the methods of it”
             obj_log->add_logs( i_type = ‘INFO’ i_message = ‘Program started’ ). 

Demo Program: ZREP_TEST_LOGS 
Below is a simple report that fetches flight records (SFLIGHT) and logs the steps.

REPORT ZREP_TEST_LOGS.

DATA: lt_flights TYPE TABLE OF sflight.

START-OF-SELECTION.

data(obj_log) = new zgcl_logger( ).

obj_log->add_logs( i_type = ‘INFO’ i_message = ‘Program started’ ).

SELECT * FROM sflight INTO TABLE lt_flights UP TO 10 ROWS.

IF sy-subrc <> 0.
obj_log->add_logs(
EXPORTING
i_type = ‘ERROR’
i_message = ‘No flight records found’ ). ” adding the logs into Log Table
ELSE.
obj_log->add_logs(
EXPORTING
i_type = ‘SUCCESS’
i_message = |Fetched { lines( lt_flights ) } flight records| ). ” adding the logs into Log Table
ENDIF.

obj_log->add_logs( EXPORTING
i_type = ‘INFO’
i_message = ‘Processing complete’ ).

obj_log->display_logs( ). ” displaying the logs in ALV

* obj_log->download_logs( ). ” downloading the logs into local system

obj_log->clear_logs( ). ” clear the logs / optional

Sample Log Output: 

 

About the Logger Class – ZGCL_LOGGER 
Here’s a simplified overview of what the class contains: 

 Private Attributes

LT_LOG – Internal table to store logs (timestamp, type, message, user)LS_LOG – Work area used to build individual log entries

Public Instance Methods                                     

 

ADD_LOGS( ) – Appends a new log entry to the internal log table

 

DOWNLOAD_LOGS( ) – Uses CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD to download logs as a .txt file 

 

DISPLAY_LOGS( ) – Displays logs in ALV using REUSE_ALV_GRID_DISPLAY

 

CLEAR_LOGS( ) – Clears the log table after display/download

 

Why we declare LT_LOG and LS_LOG as private to: Internal data is protected from outside modification.Only methods of the class (like ADD_LOGS, DOWNLOAD_LOGS) can modify or read the logs.Prevents accidental or uncontrolled changes to the log data from outside the class.This keeps your class robust and safe.Reuse It Anywhere 
Once your class is in SE24 (ZGCL_LOGGER), we can reuse it in: Reports (SE38) Enhancements (BADI/User-Exit)  BAPIs or RFCs  Background Jobs Unit Tests 

  No need to rewrite log logic again! 

Final Thoughts 
A simple global logger like this improves: 
Code readability Debugging experience Standardization across your team

 Why we Use Logs Instead of  MESSAGES?

Messages go away after execution — logs can be saved or reviewed later.Messages stop execution (sometimes) — logs don’t interrupt the user. Logs help in debugging batch jobs or background processes (where we can’t see messages directly).

  Final Summary:

The ZGCL_LOGGER/any Logger class is a reusable ABAP utility that helps developers log messages (info, errors, success, etc.) during program execution in a clean, structured way.
It simplifies debugging, improves traceability, and replaces scattered WRITE: or MESSAGE statements with organized logging that can be displayed or downloaded.
Perfect for reports, enhancements, and background jobs where we need clear execution tracking. 

​ Introduction: In many ABAP reports or background jobs, we often write WRITE: or MESSAGE statements just for debugging or tracking execution. But what if we had a reusable logger class that: Keeps all logs organized Categorizes logs (INFO, ERROR, SUCCESS, etc.)  Works in any report or enhancement Allows us to download or display logs This post shows how to use a global logger class to make that happen — clean, simple, and reusable across your SAP system. What We’re Building We’ll create and use a global class ZGCL_LOGGER with these methods: ADD_LOGS( ) – Add a new log entry with type and message DOWNLOAD_LOGS( ) – Download the collected logs to a text file DISPLAY_LOGS( ) – Display the collected logs in a ALVCLEAR_LOGS( ) – Clear the collected logsWe can use this logger in any report, like this:              DATA(obj_log) = NEW zgcl_logger( ). “Create a object for Log class and access the methods of it”             obj_log->add_logs( i_type = ‘INFO’ i_message = ‘Program started’ ). Demo Program: ZREP_TEST_LOGS Below is a simple report that fetches flight records (SFLIGHT) and logs the steps.REPORT ZREP_TEST_LOGS.

DATA: lt_flights TYPE TABLE OF sflight.

START-OF-SELECTION.

data(obj_log) = new zgcl_logger( ).

obj_log->add_logs( i_type = ‘INFO’ i_message = ‘Program started’ ).

SELECT * FROM sflight INTO TABLE lt_flights UP TO 10 ROWS.

IF sy-subrc <> 0.
obj_log->add_logs(
EXPORTING
i_type = ‘ERROR’
i_message = ‘No flight records found’ ). ” adding the logs into Log Table
ELSE.
obj_log->add_logs(
EXPORTING
i_type = ‘SUCCESS’
i_message = |Fetched { lines( lt_flights ) } flight records| ). ” adding the logs into Log Table
ENDIF.

obj_log->add_logs( EXPORTING
i_type = ‘INFO’
i_message = ‘Processing complete’ ).

obj_log->display_logs( ). ” displaying the logs in ALV

* obj_log->download_logs( ). ” downloading the logs into local system

obj_log->clear_logs( ). ” clear the logs / optionalSample Log Output:  About the Logger Class – ZGCL_LOGGER Here’s a simplified overview of what the class contains:  Private AttributesLT_LOG – Internal table to store logs (timestamp, type, message, user)LS_LOG – Work area used to build individual log entriesPublic Instance Methods                                      ADD_LOGS( ) – Appends a new log entry to the internal log table DOWNLOAD_LOGS( ) – Uses CL_GUI_FRONTEND_SERVICES=>GUI_DOWNLOAD to download logs as a .txt file  DISPLAY_LOGS( ) – Displays logs in ALV using REUSE_ALV_GRID_DISPLAY CLEAR_LOGS( ) – Clears the log table after display/download Why we declare LT_LOG and LS_LOG as private to: Internal data is protected from outside modification.Only methods of the class (like ADD_LOGS, DOWNLOAD_LOGS) can modify or read the logs.Prevents accidental or uncontrolled changes to the log data from outside the class.This keeps your class robust and safe.Reuse It Anywhere Once your class is in SE24 (ZGCL_LOGGER), we can reuse it in: Reports (SE38) Enhancements (BADI/User-Exit)  BAPIs or RFCs  Background Jobs Unit Tests   No need to rewrite log logic again! Final Thoughts A simple global logger like this improves: Code readability Debugging experience Standardization across your team Why we Use Logs Instead of  MESSAGES?Messages go away after execution — logs can be saved or reviewed later.Messages stop execution (sometimes) — logs don’t interrupt the user. Logs help in debugging batch jobs or background processes (where we can’t see messages directly).  Final Summary:The ZGCL_LOGGER/any Logger class is a reusable ABAP utility that helps developers log messages (info, errors, success, etc.) during program execution in a clean, structured way.It simplifies debugging, improves traceability, and replaces scattered WRITE: or MESSAGE statements with organized logging that can be displayed or downloaded.Perfect for reports, enhancements, and background jobs where we need clear execution tracking.   Read More Application Development and Automation Blog Posts articles 

#SAP

You May Also Like

More From Author