DATAL_VAR_LOGHNDL TYPE BALLOGHNDL,
          L_VAR_MESSAGE_DUMMY TYPE CHAR255,
          L_WA_MSG            TYPE BAL_S_MSG.

    "Generate the log
    DATAL_WA_LOG          TYPE BAL_S_LOG.
    "Basisdaten
    L_WA_LOG-EXTNUMBER  SY-CPROG.
    L_WA_LOG-ALUSER     SY-UNAME.
    L_WA_LOG-ALPROG     SY-REPID.
    L_WA_LOG-ALDATE     SY-DATUM.
    L_WA_LOG-ALTIME     SY-UZEIT.
    L_WA_LOG-ALTCODE    SY-TCODE.
    L_WA_LOG-OBJECT     'YARTSTAMM'.
    L_WA_LOG-SUBOBJECT  'STMMDWL'.
    "Modus setzen
    IF SY-BATCH EQ 'X'.
      L_WA_LOG-ALMODE 'B'.
    ELSEIF SY-BINPT EQ 'X'.
      L_WA_LOG-ALMODE 'I'.
    ELSE.
      L_WA_LOG-ALMODE 'D'.
    ENDIF.
    "Create
    CALL FUNCTION 'BAL_LOG_CREATE'
      EXPORTING
        I_S_LOG      L_WA_LOG
      IMPORTING
        E_LOG_HANDLE L_VAR_LOGHNDL
      EXCEPTIONS
        OTHERS       1.
    IF SY-SUBRC <> 0.
      " Ignore Error
    ENDIF.

Add Messages like this:

 "Insert Message
    MESSAGE E180(YIWITH I_VAR_MATNR I_VAR_MATKL
                        INTO L_VAR_MESSAGE_DUMMY.

    CLEAR L_WA_MSG.
    L_WA_MSG-MSGTY 'E'.
    L_WA_MSG-MSGID SY-MSGID.
    L_WA_MSG-MSGNO SY-MSGNO.
    L_WA_MSG-MSGV1 SY-MSGV1.
    L_WA_MSG-MSGV2 SY-MSGV2.
    L_WA_MSG-MSGV3 SY-MSGV3.
    L_WA_MSG-PROBCLASS '1'.

    CALL FUNCTION 'BAL_LOG_MSG_ADD'
      EXPORTING
        I_S_MSG          L_WA_MSG
      EXCEPTIONS
        LOG_NOT_FOUND    1
        MSG_INCONSISTENT 2
        LOG_IS_FULL      3
        OTHERS           4.
    IF SY-SUBRC <> 0.
      "Ignore Errors
    ENDIF.

Don't forget to save the Log in the End:

*   SAVE LOG
    DATA L_IT_LOG_HANDEL TYPE BAL_T_LOGH.
    APPEND L_VAR_LOGHNDL TO L_IT_LOG_HANDEL.
    CALL FUNCTION 'BAL_DB_SAVE'
      EXPORTING
        I_T_LOG_HANDLE L_IT_LOG_HANDEL
      EXCEPTIONS
        OTHERS         1.
    IF SY-SUBRC <> 0.
      "Ignore Errors
    ENDIF.

In Programms you can easily encapsulate everything inside Forms. Inside functions i would recommend creating a helper class and instanciate it.

We also are using function pools as Log Helper. The problem with them might be, that only instance exists. So if you use any kind of reset method and keep a handle inside the pool it might get reset by nested calls inside different functions.


Serkan AKKAVAK
Computer Engineer
ABAP Developer & SAP S/4 HANA Logistics Team Lead
Contact : serkurumsal@yandex.com