ERPClimb logoERPClimb

Code examples · Modern ABAP · Intermediate

Application log: write messages and a caught exception with the BAL API and read them in SLG1

The problem

A nightly import must leave a trace that support can read the next morning: when it started, which order had no customer and which technical error occurred.

How we solve it

  1. 1Create the logobject and external id
  2. 2Add entriestext and exception
  3. 3Save and readSLG1

The result

The log is saved and visible with three entries under object ZORDER, subobject IMPORT, external id RUN-001.

Overview drawn by ERPClimb from this example. The steps are the same as in the code below.

1. The requirement

A nightly import must leave a trace that support can read the next morning: when it started, which order had no customer and which technical error occurred. The trace must survive the end of the program and be searchable by an external id such as the run number.

2. How we design it

SAP's application log stores messages in database tables and shows them in transaction SLG1. In modern ABAP you use the BAL classes: CL_BALI_LOG creates the log, header and item setters describe what is logged, and CL_BALI_LOG_DB saves it. A log belongs to an application log object and a subobject, which you create once in ADT; the external id identifies one run. We add one status message, one error message and one caught exception, and save at the end.

Why we decided it this way

  1. 1Object and subobjectThey group logs: support filters SLG1 by object ZORDER and subobject IMPORT instead of reading every log.
  2. 2External id for the runA search for the run number finds the log of exactly that night.
  3. 3Log the exception object, not only its textThe exception setter keeps the full message and where it was raised, so the root cause can be found later.
  4. 4Save once at the endOne write per run keeps the import fast. The log is only visible after the save.

Before you start

  • An application log object ZORDER with a subobject IMPORT. In ADT: right-click the package > New > Other ABAP Repository Object > Application Log Object. On a classic system the same is done in transaction SLG0.
  • ADT and a package for the class.

3. The code, step by step

Step 1 of 3: Create the log

A log with a header that support can search for.

ABAP · Class · ZCL_LOG_DEMO (main method, part 1)
" Class frame
CLASS zcl_log_demo DEFINITION PUBLIC FINAL CREATE PUBLIC.
  PUBLIC SECTION.
    INTERFACES if_oo_adt_classrun.
ENDCLASS.

CLASS zcl_log_demo IMPLEMENTATION.
  METHOD if_oo_adt_classrun~main.
    TRY.
        " Log with header
        DATA(log) = cl_bali_log=>create_with_header(
          header = cl_bali_header_setter=>create(
            object      = 'ZORDER'
            subobject   = 'IMPORT'
            external_id = 'RUN-001' ) ).
Part by part (plain text)
  1. Class frame. A runnable class that prints to the console. All BAL methods can raise CX_BALI_RUNTIME, so everything sits in one TRY block.
  2. Log with header. CREATE_WITH_HEADER builds a log. The header setter names the application log object, the subobject and the external id. The object and subobject must exist, otherwise the log cannot be created or saved and CX_BALI_RUNTIME is raised.

Open in the portal

The SAP objects named in this code are linked in the code above and listed here. Each opens its page in the SAP Library.

Transaction codes

4. The final outcome

The log is saved and visible with three entries under object ZORDER, subobject IMPORT, external id RUN-001.

What you should see

Log saved In SLG1 (object ZORDER, subobject IMPORT, external id RUN-001): Status Import started Error Order 4711 has no customer Error Division by zero (CX_SY_ZERODIVIDE)

Try it and check the result

  • Create the application log object ZORDER with subobject IMPORT, activate the class and run it with F9. The console shows Log saved.
  • Open transaction SLG1, enter object ZORDER, subobject IMPORT and the external id RUN-001, and execute: the log with its three entries is displayed. On BTP or public cloud, which have no SLG1, use the Fiori app Application Logs.
  • Rename the subobject in the class to one that does not exist and run again: the class prints 'Logging failed' with the reason.

Common mistakes

  • Logging inside a loop with a database write per entry is slow. Add all entries first and save once.
  • Never put personal or secret data into log texts. Logs are read by support staff and kept for a long time.
  • A log that is never saved is lost when the program ends. Save before a step that can end the program with an error.

Keep learning

Checking status: Reviewed line by line against SAP documentation. Not executed on a live system. Reviewed 2026-11-25. Release: ABAP Environment on BTP, S/4HANA Cloud and recent S/4HANA on-premise releases. Check that CL_BALI_LOG exists in SE24 or ADT on your system.

Original ERPClimb teaching code with illustrative names. Not taken from a customer system. Syntax can differ by release; check the references below and activate in a sandbox first. ERPClimb is independent of SAP SE.