Creating an alert log via XML/SQL

I don’t know what it is today, but reading Tanel’s post about “Oracle 11g: Reading alert log via SQL“, I just wanted to see if I could manage to do it backwards.

In principle you don’t have to use the statements below, because I noticed that the DBMS_SYSTEM.KSDWRT will do it for you automatically.

The content of my Oracle 11g log.xml file is as follows

 
 Starting ORACLE instance (normal)
 


 LICENSE_MAX_SESSION = 0
 


 LICENSE_SESSIONS_WARNING = 0
 

So based on Tanel’s blog, could I do the same, but now with XMLDB functions (based on database version 11.1.0.7.0)…

Here it goes.

select XMLElement(NOENTITYESCAPING "msg", 
                     XMLAttributes( alt.ORIGINATING_TIMESTAMP as "time"
                                  , alt.ORGANIZATION_ID       as "org_id"
                                  , alt.COMPONENT_ID          as "comp_id"
                                  , alt.MESSAGE_ID            as "msg_id"
                                  , alt.MESSAGE_TYPE          as "type"
                                  , alt.MESSAGE_GROUP         as "group"
                                  , alt.MESSAGE_LEVEL         as "level"
                                  , alt.HOST_ID               as "host_id"
                                  , alt.HOST_ADDRESS          as "host_addr"
                                  , alt.PROCESS_ID            as "pid_id"
                                  , alt.VERSION               as "version"
                                  ),
                     XMLElement("txt", MESSAGE_TEXT)                         
                 ) as "LOG.XML"
from   X$DBGALERTEXT alt
where  rownum < = 3
/

SQL> set lines 74
SQL>
SQL> select XMLElement(NOENTITYESCAPING "msg",
  2                       XMLAttributes( alt.ORIGINATING_TIMESTAMP as "time"
  3                                    , alt.ORGANIZATION_ID       as "org_id"
  4                                    , alt.COMPONENT_ID          as "comp_id"
  5                                    , alt.MESSAGE_ID            as "msg_id"
  6                                    , alt.MESSAGE_TYPE          as "type"
  7                                    , alt.MESSAGE_GROUP         as "group"
  8                                    , alt.MESSAGE_LEVEL         as "level"
  9                                    , alt.HOST_ID               as "host_id"
 10                                    , alt.HOST_ADDRESS          as "host_addr"
 11                                    , alt.PROCESS_ID            as "pid_id"
 12                                    , alt.VERSION               as "version"
 13                                    ),
 14                       XMLElement("txt", MESSAGE_TEXT)
 15                   ) as "LOG.XML"
 16  from   X$DBGALERTEXT alt
 17  where  rownum < = 3
 18  /

LOG.XML
--------------------------------------------------------------------------
Starting ORACLE instance (normal)


LI
CENSE_MAX_SESSION = 0


LI
CENSE_SESSIONS_WARNING = 0


3 rows selected.

I comes close, but it uses still double quotes and doesn’t do the new lines on every third element, also the pretty print layout between “msg” and “txt” elements isn’t properly done yet. Trying to come a little bit close to the original log.xml output, I came up with the following…

select xmlserialize
       (CONTENT
        XMLElement(NOENTITYESCAPING "msg", 
                     XMLAttributes( alt.ORIGINATING_TIMESTAMP as "time"
                                  , alt.ORGANIZATION_ID       as "org_id"
                                  , alt.COMPONENT_ID          as "comp_id"
                                  , alt.MESSAGE_ID            as "msg_id"
                                  , alt.MESSAGE_TYPE          as "type"
                                  , alt.MESSAGE_GROUP         as "group"
                                  , alt.MESSAGE_LEVEL         as "level"
                                  , alt.HOST_ID               as "host_id"
                                  , alt.HOST_ADDRESS          as "host_addr"
                                  , alt.PROCESS_ID            as "pid_id"
                                  , alt.VERSION               as "version"
                                  ),
                     XMLElement("txt", MESSAGE_TEXT)                         
                 )
        INDENT SIZE=0 HIDE DEFAULTS
        ) as "LOG.XML"
from   X$DBGALERTEXT alt
where  rownum < = 3
/

SQL> set lines 1000
SQL> col "LOG.XML" for a100
SQL> select xmlserialize
  2         (CONTENT
  3          XMLElement(NOENTITYESCAPING "msg",
  4                       XMLAttributes( alt.ORIGINATING_TIMESTAMP as "time"
  5                                    , alt.ORGANIZATION_ID       as "org_id"
  6                                    , alt.COMPONENT_ID          as "comp_id"
  7                                    , alt.MESSAGE_ID            as "msg_id"
  8                                    , alt.MESSAGE_TYPE          as "type"
  9                                    , alt.MESSAGE_GROUP         as "group"
 10                                    , alt.MESSAGE_LEVEL         as "level"
 11                                    , alt.HOST_ID               as "host_id"
 12                                    , alt.HOST_ADDRESS          as "host_addr"
 13                                    , alt.PROCESS_ID            as "pid_id"
 14                                    , alt.VERSION               as "version"
 15                                    ),
 16                       XMLElement("txt", MESSAGE_TEXT)
 17                   )
 18          INDENT SIZE=0 HIDE DEFAULTS
 19          ) as "LOG.XML"
 20  from   X$DBGALERTEXT alt
 21  where  rownum < = 3
 22  /

LOG.XML
------------------------------------------------------------------------------

Starting ORACLE instance (normal)




LICENSE_MAX_SESSION = 0




LICENSE_SESSIONS_WARNING = 0



3 rows selected.

It doesn’t matter. It was just for fun. DBMS_SYSTEM.KSDWRT will do it for you automatically, anyway…

😎
.

Marco Gralike Written by:

One Comment

  1. March 25

    Be aware that my “code” wordpress plugin is not always very nice with handling xml tags. So it doesn’t look like valid xml, but if you try it in an Oracle 11g database, you will see that it is…

    HTH

    Marco

Comments are closed.