Skip to Content

It’s been always a headache when customer raises a message and you are not sure what might have gone wrong!!!

At this point if you have application logging in place it can be really helpful to trace and check which code might have triggered the issue.

Application logging can be useful for the below scenarios too:
1…When you want to record the progress of the execution of an application so that you can reconstruct it later if necessary
2…when you are not sure/not able to debug a code (foreground/background) and you want to drill down the error cause. In this case you can easily check the application log and decide the exact location where the problem can be.


So let’s start how you can create an application logging.
Transaction code: SLG0 is used to create an object which is used to identify your logs.
So here I have created an application log Object named: ZHR_ENC.

And a Sub-object ZHRENC_SUB.
Its like: I want to create an application log for COUNTRY management. And inside the countries whenever application is related to a particular state. I create sub-object based on the states.

Once you are done with the creation of the Object and sub object it’s time to put the code which will actually create the application logs.
You can write the below code to your enhancements/User-Exit/reports (any custom code) so that when it is executed logging is done.
(For testing purpose just copy the below code in Report program ZTST_APPL_LOG)

*&———————————————————————*
*& Report  ZTST_APPL_LOG
*&
*&———————————————————————*
*&
*&
*&———————————————————————*

REPORT ZTST_APPL_LOG.

type-pools: abap.

data: l_log_handle type balloghndl,
l_timestamp
type tzntstmps,
l_timezone
type timezone value ‘UTC’,
l_str_log
type bal_s_log,
l_str_balmsg
type bal_s_msg,
l_str_message
type bapiret2,
l_msg_logged
type boolean,
l_tab_messages
type bapiret2_t.

*-Building messages
*–Use your own message which you want to Display in the log
do 1 times.
call function ‘BALW_BAPIRETURN_GET2’
exporting
type = ‘E’
cl
= ‘BPFS’
number = ‘006’
importing
return = l_str_message.
append l_str_message to l_tab_messages.
clear l_str_message.
enddo.

*-Logging messages
convert date sydatum time syuzeit
into time stamp l_timestamp time zone l_timezone.

l_str_logextnumber = l_timestamp.
condense l_str_logextnumber.
l_str_log
object = ‘ZHR_ENC’.
l_str_log
subobject = ‘ZHRENC_SUB’.
l_str_log
aldate_del = sydatum + 5.

call function ‘BAL_LOG_CREATE’
exporting
i_s_log
= l_str_log
importing
e_log_handle
= l_log_handle
exceptions
log_header_inconsistent
= 1
others = 2.
if sysubrc <> 0.
message id symsgid type symsgty number symsgno
with symsgv1 symsgv2 symsgv3 symsgv4 into l_str_messagemessage.
write: ‘Type’,symsgty, ‘Message’,l_str_messagemessage.
else.
loop at l_tab_messages into l_str_message.
move: l_str_messagetype       to l_str_balmsgmsgty,
l_str_message
id         to l_str_balmsgmsgid,
l_str_message
number     to l_str_balmsgmsgno,
l_str_message
message_v1 to l_str_balmsgmsgv1,
l_str_message
message_v2 to l_str_balmsgmsgv2,
l_str_message
message_v3 to l_str_balmsgmsgv3,
l_str_message
message_v4 to l_str_balmsgmsgv4.

call function ‘BAL_LOG_MSG_ADD’
exporting
i_log_handle
= l_log_handle
i_s_msg
= l_str_balmsg
importing
e_msg_was_logged
= l_msg_logged
exceptions
log_not_found
= 1
msg_inconsistent
= 2
log_is_full
= 3
others = 4.
if sysubrc <> 0.
message id symsgid type symsgty number symsgno
with symsgv1 symsgv2 symsgv3 symsgv4 into l_str_messagemessage.
write: ‘Type’,symsgty, ‘Message’,l_str_messagemessage.
endif.
endloop.
if sysubrc eq 0.
call function ‘BAL_DB_SAVE’
exporting
i_save_all
= abap_true
exceptions
log_not_found
= 1
save_not_allowed
= 2
numbering_error
= 3
others = 4.
if sysubrc <> 0.
message id symsgid type symsgty number symsgno
with symsgv1 symsgv2 symsgv3 symsgv4 into l_str_messagemessage.
write: ‘Type’,symsgty, ‘Message’,l_str_messagemessage.
else.
write: ‘Messages Saved in the log’.
endif.
endif.
endif.
write : ‘done with log number’,l_str_logextnumber.


In the above code three function modules are doing the full job.
‘BAL_LOG_CREATE’ it creates the application logging object
‘BAL_LOG_MSG_ADD’ it adds the messages to your application logging object/sub-object
‘BAL_DB_SAVE’ saves the messages into to database.
Note: Here I have used the standard message class BPFS (message number: 006).Incase if it’s not available in your system create new one using Transaction code SE91 or reuse any existing message.
Execute the created report .You can use the transaction Code: SLG1 to monitor the application logging based on the object.


So now when customers raise the Ticket the first step you can do is. Check the application logging in the production and you will have clear picture what might have been gone wrong.


To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply