Friday, August 11, 2023

How to handle Log messages in stored objects (Functions, Procedure, Package Body)


Normally we handle log messages using FND_FILE.PUT_LINE(fnd_file.LOG,'Msg');

But in some cases we need to handle Log messages based on the Parameter value.

If we give the parameter value for DEBUG as Y (Yes), then the program must print a message in the log file. And if the N (No) value is entered, the program cannot print the message.

We can do it using following code logic.. (You can modify it according to your requirement and logic)

For Example -

     -- Package Specification

     CREATE OR REPLACE Package XXC_GET_DATA
     IS
         Procedure write_log(p_message IN VARCHAR2
                                        ,  p_debug_flag IN VARCHAR2 Default 'N');
         --
         Procedure employee_details(errbuf OUT VARCHAR2
                                                  , retcode OUT VARCHAR2
                                                  , p_debug_flag IN VARCHAR2
                                                  , p_emp_id IN NUMBER);
          --
    END XXC_GET_DATA;
   /

     -- Package Body

     CREATE OR REPLACE Package Body XXC_GET_DATA
     IS
         PROCEDURE write_log(p_message IN VARCHAR2
                                        ,  p_debug_flag IN VARCHAR2 Default 'N')
          IS
               lc_time VARCHAR2(20);  -- It will store log print Date with time.
          BEGIN
               SELECT to_char(SYSDATE, 'MM-DD-YYYY HH24:MI:SS') INTO lc_time
               FROM dual;
               --
               IF (p_debug_flag = 'Y') THEN
                    fnd_file.put_line(fnd_file.LOG,lc_time||'-'||p_message);
               END IF;
          EXCEPTION
               WHEN others THEN
                    fnd_file.put_line(fnd_file.LOG,'Error in Write_Log procedure: '||SQLERRM);
          END write_log;
          --
          PROCEDURE employee_details(errbuf OUT VARCHAR2
                                                  , retcode OUT VARCHAR2
                                                  , p_debug_flag IN VARCHAR2
                                                  , p_emp_id IN NUMBER)
          IS
               lc_emp_name emp.emp_name%TYPE;
               lc_address  emp.address%TYPE;
               ln_emp_num  emp.emp_number%TYPE;
          BEGIN
               BEGIN
                    SELECT emp_name, address, emp_number
                    INTO lc_emp_name, lc_address, ln_emp_num
                    FROM emp
                    WHERE emp_id = p_emp_id;
               EXCEPTION
                    WHEN no_data_found THEN
                         write_log('Employee record not found.', p_debug_flag);
                    WHEN others THEN
                         write_log('Error while getting emp details: '||SQLERRM, p_debug_flag);
               END;
               --
          EXCEPTION
               WHEN others THEN
               write_log('Error in employee_details procedure: '||SQLERRM, p_debug_flag);                   
          END employee_details;
          --
    END XXC_GET_DATA;
   /

Steps to get ZPL code output using Zebra viewer - Online

Introduction ZPL is a print language used by many label printers. A print language is a set of commands that can be used to draw elements li...