2015-09-14 48 views
5

我目前正在處理程序中的日誌記錄錯誤。此過程的目標是在數據庫中的其他包中的異常處理程序中調用並記錄每個程序遇到的錯誤。下面是我的代碼。oracle中的日誌行號

CREATE OR REPLACE PROCEDURE APMS.test_procedure AS 

    procedure write_error_log (errcode number, errstr varchar2) is 
    pragma autonomous_transaction; 
    -- this procedure stays in its own new private transaction 
    begin       
      INSERT INTO error_log 
       (ora_err_tmsp, 
         ora_err_number, 
         ora_err_msg, 
         ora_err_line_no) 
      values (CURRENT_TIMESTAMP, 
        errcode, 
        errstr, 
        'line number'); 
      COMMIT; -- this commit does not interfere with the caller's transaction. 
    end write_error_log; 

BEGIN 
    INSERT INTO mockdata 
     VALUES ('data1', 'mockname', 'mockcity'); 

    exception when others then    
    write_error_log(sqlcode,sqlerrm); 
    raise; 
END test_procedure; 
/

我目前只是誘導我mock_data表的錯誤記錄會在error_log表中的錯誤,看看它的功能我只是無法弄清楚如何記錄行數列。我是一個完整的初學者,所以任何幫助將不勝感激。特別是,如果有人知道如何在其他軟件包/過程中使用此過程來記錄其他軟件包中的錯誤,那麼這些錯誤也很棒。我在這裏學習,所以任何反饋意見,我可以進一步擴大這篇文章,如果我不清楚。

+0

我使用此包來自Steven Feurstein。顯然從蟾蜍不再可用,但可以在這裏找到http://awads.net/wp/2007/08/08/new-oracle-plsql-error-management-framework-released/爲什麼寫時,你可以使用一個建立包? – kevinsky

+0

感謝您的資源,不幸的是,我使用蟾蜍,我的上司會讓我學習並編寫其他程序在其異常處理程序中運行的過程。 – Jules

回答

2

嘗試使用DBMS_UTILITY.FORMAT_ERROR_BACKTRACE。你可以看看here瞭解更多信息。

像這樣的東西應該讓你的工作代碼:

CREATE OR REPLACE PROCEDURE APMS.test_procedure AS 

    procedure write_error_log (errcode number, errstr varchar2,errline varchar2) is 
    pragma autonomous_transaction; 
    -- this procedure stays in its own new private transaction 
    begin       
      INSERT INTO error_log 
       (ora_err_tmsp, 
         ora_err_number, 
         ora_err_msg, 
         ora_err_line_no) 
      values (CURRENT_TIMESTAMP, 
        errcode, 
        errstr, 
        errline); 
      COMMIT; -- this commit does not interfere with the caller's transaction. 
    end write_error_log; 

BEGIN 
    INSERT INTO mockdata 
     VALUES ('data1', 'mockname', 'mockcity'); 

    exception when others then    
    write_error_log(sqlcode,sqlerrm,DBMS_UTILITY.FORMAT_ERROR_BACKTRACE); 
    raise; 
END test_procedure; 
+0

我試圖寫在值部分以及異常處理程序...不斷收到錯誤。雖然我會做更多的閱讀。有可能我只是不正確地使用它。 – Jules

+0

玩過這個工具,真棒,謝謝。 – Jules

+0

不客氣。樂意效勞。 :) – Aramillo

1

使用以下方法來獲取調用堆棧:

  • dbms_utility.format_error_stack
  • dbms_utility.format_error_backtrace

例如,

SQL> declare 
    2 v1 integer := 1; 
    3 v2 integer := 0; 
    4 v3 integer; 
    5 procedure p1 (v1 in integer, v2 in integer, v3 out integer) is 
    6 begin 
    7  v3 := v1/v2; 
    8 end; 
    9 procedure p2 (v1 in integer, v2 in integer, v3 out integer) is 
10 begin 
11  p1 (v1, v2, v3); 
12 end; 
13 begin 
14 p2 (v1, v2, v3); 
15 exception 
16 when others then 
17  dbms_output.put_line ('---------------------'); 
18  dbms_output.put_line ('This is what you record in log table:'); 
19  dbms_output.put (dbms_utility.format_error_stack); 
20  dbms_output.put (dbms_utility.format_error_backtrace); 
21  dbms_output.put_line ('---------------------'); 
22  raise; 
23 end; 
24/
--------------------- 
This is what you record in log table: 
ORA-01476: divisor is equal to zero 
ORA-06512: at line 7 
ORA-06512: at line 11 
ORA-06512: at line 14 
--------------------- 
declare 
* 
ERROR at line 1: 
ORA-01476: divisor is equal to zero 
ORA-06512: at line 22