Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to develop an after serverror trigger in Oracle?

I'm trying to log all the errors in my database into a table. So as user sys i wrote the following code:

CREATE TABLE servererror_log (
    error_datetime  TIMESTAMP,
    error_user      VARCHAR2(30),
    db_name         VARCHAR2(9),
    error_stack     VARCHAR2(2000),
    captured_sql    VARCHAR2(1000));
/
CREATE OR REPLACE TRIGGER log_server_errors
AFTER SERVERERROR
ON DATABASE
DECLARE
 captured_sql VARCHAR2(1000);
BEGIN
  SELECT q.sql_text
  INTO captured_sql
  FROM gv$sql q, gv$sql_cursor c, gv$session s
  WHERE s.audsid = audsid
  AND s.prev_sql_addr = q.address
  AND q.address = c.parent_handle;

  INSERT INTO servererror_log
  (error_datetime, error_user, db_name,
   error_stack, captured_sql)
  VALUES
  (systimestamp, sys.login_user, sys.database_name,
  dbms_utility.format_error_stack, captured_sql);
END log_server_errors;

But when i force an error like trying to select from a non-existing table it doesn´t log the error in the table.

Is there any way to check that the trigger fires at all? Also, I tried creating a test table to insert there but it doesn't work either, even if a define the trigger as an autonomous transaction and commit inside the trigger.

Thanks, Joaquin

like image 504
xocasdashdash Avatar asked Feb 11 '09 12:02

xocasdashdash


2 Answers

Do not query v$sql; get the statement using ora_sql_txt.

CREATE OR REPLACE TRIGGER log_server_errors
AFTER SERVERERROR
ON DATABASE
DECLARE
sql_text ora_name_list_t;
stmt clob;
n number;
BEGIN
  n := ora_sql_txt(sql_text);
  if n > 1000 then n:= 1000; end if ;
  FOR i IN 1..n LOOP
     stmt := stmt || sql_text(i);
   END LOOP;

   INSERT INTO servererror_log
   (error_datetime, error_user, db_name,
    error_stack, captured_sql)
   VALUES
   (systimestamp, sys.login_user, sys.database_name,
   dbms_utility.format_error_stack, stmt);
   commit;
 END log_server_errors;
 /

Then:

SQL> select * from c;

This produces:

select * from c
              *
ERROR at line 1:
ORA-00942: table or view does not exist

That can now be queried:

select * from servererror_log;

To produce:

ERROR_DATETIME
---------------------------------------------------------------------------
ERROR_USER                     DB_NAME
------------------------------ ---------
ERROR_STACK
--------------------------------------------------------------------------------
CAPTURED_SQL
--------------------------------------------------------------------------------
11-FEB-09 02.55.35.591259 PM
SYS                            TS.WORLD
ORA-00942: table or view does not exist
select * from c
like image 54
Yasin B Avatar answered Nov 17 '22 15:11

Yasin B


To see if the trigger is firing, add one or more lines to it like this:

DBMS_OUTPUT.PUT_LINE( 'Got this far' );

In SQLPlus, SET SERVEROUTPUT ON then execute a command to generate an error. You should get output like this:

dev> select * from aldfjh;
select * from aldfjh
              *
ERROR at line 1:
ORA-00942: table or view does not exist


ORA-00942: table or view does not exist

Got this far
like image 29
Dave Costa Avatar answered Nov 17 '22 15:11

Dave Costa