Build an Oracle 19c+ PL/SQL error logging package with log_error, display_error, backtraces, UTL_CALL_STACK, ORA-06512 diagnosis, and practical developer examples.

When an Oracle 19c or later PL/SQL procedure fails, SQLERRM alone rarely gives enough information to solve the problem. A useful error record should identify the compiled unit, source line, error code, complete error stack, original raise location, call path, module, and action.
This Oracle 19c+ guide builds a reusable PL/SQL error logging package with procedures for logging and displaying errors. The examples are intended for procedures, functions, packages, triggers, scheduler jobs, and API entry points. They preserve the original exception, help diagnose ORA-06512, and avoid exposing sensitive values.
Oracle 19c+ error diagnostics to capture
$$PLSQL_UNITidentifies the compiled PL/SQL unit.$$PLSQL_LINEidentifies the source line where the handler runs.SQLCODEreturns the numeric error code.SQLERRMreturns the current error message.DBMS_UTILITY.FORMAT_ERROR_STACKreturns the error stack.DBMS_UTILITY.FORMAT_ERROR_BACKTRACEidentifies where the exception was originally raised.DBMS_UTILITY.FORMAT_CALL_STACKreturns the runtime call stack.UTL_CALL_STACKprovides structured call-stack information.DBMS_APPLICATION_INFOassociates the failure with a module and action.
Create an Oracle 19c+ error log table
The table stores diagnostic context, not secrets or complete request payloads. A CLOB is used for stack values because error and call-stack text can be longer than a normal display column.
CREATE TABLE app_error_log ( error_id NUMBER GENERATED BY DEFAULT AS IDENTITY PRIMARY KEY, error_time TIMESTAMP WITH TIME ZONE DEFAULT SYSTIMESTAMP NOT NULL, module_name VARCHAR2(128), action_name VARCHAR2(128), context_text VARCHAR2(1000), unit_name VARCHAR2(128), unit_line NUMBER, error_code NUMBER, error_message VARCHAR2(4000), error_stack CLOB, error_backtrace CLOB, call_stack CLOB, database_user VARCHAR2(128), client_identifier VARCHAR2(256) ); CREATE INDEX app_error_log_time_ix ON app_error_log (error_time); CREATE INDEX app_error_log_code_ix ON app_error_log (error_code);
Grant insert and sequence-related privileges according to your deployment model. If the package is owned by a dedicated logging schema, expose only the package execute privilege to application schemas.
Build the Oracle 19c+ error logging package specification
The package exposes three focused procedures:
log_errorstores the current exception and safe context.display_errorprints the current exception for development or SQL*Plus-style troubleshooting.log_and_raiselogs the current exception and raises a stable application error.
CREATE OR REPLACE PACKAGE app_error_log_pkg AS
PROCEDURE log_error(
p_context IN VARCHAR2 DEFAULT NULL,
p_module IN VARCHAR2 DEFAULT NULL,
p_action IN VARCHAR2 DEFAULT NULL
);
PROCEDURE display_error(
p_context IN VARCHAR2 DEFAULT NULL
);
PROCEDURE log_and_raise(
p_error_number IN PLS_INTEGER DEFAULT -20000,
p_user_message IN VARCHAR2 DEFAULT 'The operation failed',
p_context IN VARCHAR2 DEFAULT NULL,
p_module IN VARCHAR2 DEFAULT NULL,
p_action IN VARCHAR2 DEFAULT NULL
);
END app_error_log_pkg;
/
Implement log_error and display_error
log_error uses an autonomous transaction so that a diagnostic record can survive a rollback in the business transaction. This is useful for failures, but it must be documented because the log commit is independent of the caller transaction.
CREATE OR REPLACE PACKAGE BODY app_error_log_pkg AS
PROCEDURE log_error(
p_context IN VARCHAR2 DEFAULT NULL,
p_module IN VARCHAR2 DEFAULT NULL,
p_action IN VARCHAR2 DEFAULT NULL
) IS
PRAGMA AUTONOMOUS_TRANSACTION;
BEGIN
INSERT INTO app_error_log (
module_name,
action_name,
context_text,
unit_name,
unit_line,
error_code,
error_message,
error_stack,
error_backtrace,
call_stack,
database_user,
client_identifier
) VALUES (
p_module,
p_action,
SUBSTR(p_context, 1, 1000),
$$PLSQL_UNIT,
$$PLSQL_LINE,
SQLCODE,
SUBSTR(SQLERRM, 1, 4000),
DBMS_UTILITY.FORMAT_ERROR_STACK,
DBMS_UTILITY.FORMAT_ERROR_BACKTRACE,
DBMS_UTILITY.FORMAT_CALL_STACK,
SYS_CONTEXT('USERENV', 'SESSION_USER'),
SYS_CONTEXT('USERENV', 'CLIENT_IDENTIFIER')
);
COMMIT;
EXCEPTION
WHEN OTHERS THEN
ROLLBACK;
DBMS_OUTPUT.PUT_LINE(
'ERROR LOGGER FAILURE: ' || SUBSTR(SQLERRM, 1, 4000)
);
END log_error;
PROCEDURE display_error(
p_context IN VARCHAR2 DEFAULT NULL
) IS
l_depth PLS_INTEGER;
BEGIN
DBMS_OUTPUT.PUT_LINE('context: ' || p_context);
DBMS_OUTPUT.PUT_LINE('unit: ' || $$PLSQL_UNIT);
DBMS_OUTPUT.PUT_LINE('line: ' || $$PLSQL_LINE);
DBMS_OUTPUT.PUT_LINE('sqlcode: ' || SQLCODE);
DBMS_OUTPUT.PUT_LINE('sqlerrm: ' || SQLERRM);
DBMS_OUTPUT.PUT_LINE('error stack:');
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_STACK);
DBMS_OUTPUT.PUT_LINE('error backtrace:');
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_ERROR_BACKTRACE);
DBMS_OUTPUT.PUT_LINE('call stack:');
DBMS_OUTPUT.PUT_LINE(DBMS_UTILITY.FORMAT_CALL_STACK);
l_depth := UTL_CALL_STACK.DYNAMIC_DEPTH;
FOR i IN 1 .. l_depth LOOP
DBMS_OUTPUT.PUT_LINE(
'depth=' || i ||
', subprogram=' ||
UTL_CALL_STACK.CONCATENATE_SUBPROGRAM(
UTL_CALL_STACK.SUBPROGRAM(i)
) ||
', line=' || UTL_CALL_STACK.UNIT_LINE(i)
);
END LOOP;
END display_error;
END app_error_log_pkg;
/
The logger deliberately reports its own failure with DBMS_OUTPUT and does not replace the original business exception. In production, monitor logger failures separately and consider a controlled fallback such as an external alert or database diagnostic destination.
Use the package in an Oracle 19c+ procedure
CREATE OR REPLACE PROCEDURE create_order(p_order_id NUMBER) IS
BEGIN
DBMS_APPLICATION_INFO.SET_MODULE(
module_name => 'order-api',
action_name => 'create-order'
);
INSERT INTO orders(order_id, status)
VALUES (p_order_id, 'NEW');
DBMS_APPLICATION_INFO.SET_MODULE(NULL, NULL);
EXCEPTION
WHEN OTHERS THEN
app_error_log_pkg.log_error(
p_context => 'create_order failed',
p_module => 'order-api',
p_action => 'create-order'
);
app_error_log_pkg.display_error('create_order diagnostic output');
DBMS_APPLICATION_INFO.SET_MODULE(NULL, NULL);
RAISE;
END;
/
Use RAISE; after logging when the caller must receive the original Oracle error and backtrace.
Use log_and_raise at an application boundary
Use this procedure when an API or application needs a stable error number and safe user-facing message. The technical stack is stored before the application error is raised.
CREATE OR REPLACE PROCEDURE import_customer(p_customer_id NUMBER) IS
BEGIN
INSERT INTO customer_import(customer_id, imported_at)
VALUES (p_customer_id, SYSTIMESTAMP);
EXCEPTION
WHEN DUP_VAL_ON_INDEX THEN
app_error_log_pkg.log_and_raise(
p_error_number => -20010,
p_user_message => 'Customer import failed: customer already exists',
p_context => 'import_customer duplicate key',
p_module => 'customer-api',
p_action => 'import-customer'
);
END;
/
Keep the user message stable and free of SQL text, schema names, credentials, bind values, and internal stack details. Developers can use the generated log record and error identifier to investigate the failure.
Display an error while testing a package
SET SERVEROUTPUT ON SIZE UNLIMITED;
BEGIN
order_api.create_order(1001);
EXCEPTION
WHEN OTHERS THEN
app_error_log_pkg.display_error('manual package test');
RAISE;
END;
/
In an application connection pool, DBMS_OUTPUT may not be collected. Treat display_error as a development and controlled troubleshooting tool; use log_error for durable diagnostics.
Find the logged Oracle 19c+ error
SELECT error_id,
error_time,
module_name,
action_name,
unit_name,
unit_line,
error_code,
error_message,
database_user,
client_identifier
FROM app_error_log
WHERE error_time >= SYSTIMESTAMP - INTERVAL '1' HOUR
ORDER BY error_time DESC;
SELECT DBMS_LOB.SUBSTR(error_backtrace, 4000, 1) AS backtrace,
DBMS_LOB.SUBSTR(call_stack, 4000, 1) AS call_stack
FROM app_error_log
WHERE error_id = :error_id;
Diagnose ORA-06512 with the deployed source
The line displayed in ORA-06512 is part of the propagation path. Compare the backtrace with the exact source deployed in the failing schema.
SELECT line, text
FROM user_source
WHERE name = UPPER('ORDER_API')
AND type = 'PACKAGE BODY'
AND line BETWEEN 35 AND 70
ORDER BY line;
SELECT name,
type,
line,
position,
text
FROM user_errors
WHERE name = UPPER('ORDER_API')
ORDER BY sequence;
SELECT object_name,
object_type,
status,
last_ddl_time
FROM user_objects
WHERE object_name = UPPER('ORDER_API')
ORDER BY object_type;
Compile and validate the logging package
ALTER PACKAGE app_error_log_pkg COMPILE SPECIFICATION;
ALTER PACKAGE app_error_log_pkg COMPILE BODY;
SELECT name,
type,
line,
position,
text
FROM user_errors
WHERE name = UPPER('APP_ERROR_LOG_PKG')
ORDER BY sequence;
SELECT object_name,
object_type,
status
FROM user_objects
WHERE object_name IN (
UPPER('APP_ERROR_LOG_PKG'),
UPPER('APP_ERROR_LOG')
)
ORDER BY object_type;
Oracle 19c+ logging mistakes to avoid
- Using
WHEN OTHERS THEN NULLand hiding the failure. - Logging only
SQLERRMwithout the unit, line, backtrace, or call stack. - Calling
RAISE_APPLICATION_ERRORbefore saving the original diagnostics. - Assuming the last
ORA-06512line is always the root cause. - Allowing the logger to store passwords, access tokens, personal data, full SQL statements, or unrestricted request payloads.
- Using autonomous transactions without understanding that the log commit is independent of the business transaction.
- Depending only on
DBMS_OUTPUTwhen the application does not collect it. - Ignoring invalid dependencies, privileges, synonyms, edition, or schema differences between environments.
Oracle 19c+ error-solving checklist
- Reproduce the problem with the smallest possible input.
- Call
display_errorduring controlled development testing. - Call
log_errorinside the exception handler. - Capture
$$PLSQL_UNIT,$$PLSQL_LINE,SQLCODE, andSQLERRM. - Inspect
FORMAT_ERROR_BACKTRACEbefore translating the error. - Query the log table by error time, module, action, and error code.
- Compare the backtrace with
USER_SOURCEin the failing environment. - Check
USER_ERRORS, object status, dependencies, privileges, and deployment version. - Fix the root cause and repeat both the failing and successful test cases.
- Verify that production diagnostics contain useful context without exposing sensitive data.
Summary: An Oracle 19c+ error package should log the compiled unit, handler line, original backtrace, stack, call path, module, and action. Use log_error for durable diagnostics, display_error for controlled troubleshooting, and RAISE; or log_and_raise so failures are never silently hidden.
