Oracle лог ошибок

Уровень сложности
Средний

Время на прочтение
9 мин

Количество просмотров 17K

рис. Старый "дедовский" debug кода

рис. Старый «дедовский» debug кода

Добрый день! Работая разработчиком Oracle PL/SQL, часто ли вам приходилось видеть в коде dbms_output.put_line в качестве средства debug-а? Стоит признать, что к сожалению, большинство (по моему личному мнению и опыту) разработчиков Oracle PL/SQL не уделяет должного внимания логированию как к «спасательному кругу» в случае возникновения ошибок. Более того, большая часть разработчиков не совсем понимает зачем нужно логировать информацию об ошибках и самое главное, не совсем понимают что делать и как использовать эту информацию в будущем.

Предисловие

Данным постом хотел бы начать цикл статей посвященных «Логированию ошибок» в Oracle PL/SQL. В первую очередь донести мысль до многих разработчиков, о том как можно построить функционал фиксации, хранения логов в БД. На своем опыте продемонстрировать поэтапный процесс создания полноценного логирования в БД. Рассказать как нам удалось создать логирование ошибок, разработать единую нумерацию событий для их дальнейшей идентификации, как поверх логирования «натянуть» мониторинг событий, создать функционал позволяющий увидеть все текущие ошибки в БД в виде таблиц (с указанием частоты возникновения ошибок и кол-ва и т.д.), графиков (отразить динамику роста кол-ва ошибок) и правильно распределить ресурсы для устранения тех или иных ошибок.

Оговорюсь сразу, что на рынке возможно уже есть успешные существующие коммерческие продукты осуществляющие логирование гораздо лучше и качественнее хотя бы, потому что у них есть многолетний опыт внедрения и сопровождения своего продукта в различных компаниях. Я же хочу показать один из множества примеров реализации функционала логирования, которые вполне можно осуществить силами своих разработчиков.

Введение

В этой и следующих статьях будет показано как реализованный функционал «логирования» позволяет фиксировать факт возникновения не только ошибок (сообщение с типом «Error»), но и сообщений с предупреждением (тип «Warning»), информативных сообщений (с типом «Info») и т.д., поэтому, в рамках данных статей введём термин — «Модель логирования событий» (далее по тексту — «модель») или коротко «Логирование событий», где под «событием» подразумевается некоторое ожидаемое действие, возникшее в ходе штатной/внештатной работы алгоритма.

Модель логирования позволяет реализовать:

  1. Единый подход в обработке и хранении событий

  2. Собственную нумерацию и идентификацию событий происходящих в БД (статья)

  3. Единый мониторинг событий (статья в разработке)

  4. Анализ событий происходящих в БД (статья в разработке)

Описанные выше характеристики указаны в порядке нумерации и каждый следующий пункт (шаг) есть улучшение и усложнение существующей модели. Описание этой модели будет сложно выполнить в рамках одной статьи, поэтому опишем их последовательно. Начнём с первого пункта.

Единый подход в обработке и хранении событий

Основной идеей «Единого подхода в обработке и хранении событий» заключается в создании одного одновременно простого и в тоже время очень сложного правила: «Все объекты базы данных (функции, процедуры) в обязательном порядке должны завершаться блоком обработки исключений с последующим логированием события». Простота заключается в том, что легко, в команде разработчиков, на словах договориться об исполнении данного правила. Сложность же заключается в том, что данное правило должно быть установлено на ранних этапах создания вашей БД и выполняться обязательно на протяжении всего жизненного цикла. Внедрить функционал логирования в уже существующие и действующие БД очень сложно (практически не возможно).

Все объекты базы данных (функции, процедуры) в обязательном порядке должны завершаться блоком обработки исключений с последующим логированием события. Для этого можно использовать шаблон процедуры (функции) описанный во второй статье.

Наверное сейчас кто-то из читателей может возразить: «Зачем в обязательном порядке?». А всё очень просто, если вы разработчик PL/SQL и вы не согласны с этим правилом, то вот вам пример. Посмотрите на свой текущий проект более внимательно. Скорее всего вы найдете какое-нибудь логирование событий реализованное кем-то, когда-то. Вспомните сколько раз вы обращались к этому логированию при решении багов. Именно в таких ситуациях, когда есть срочность по времени в исправлении бага, вы или ваши коллеги начинают использовать dbms_output.put_line в качестве экспресс-дебага (быстрый способ получения значений переменных используемых в коде). Согласитесь, что для исправления бага мало знать в какой процедуре, в каком запросе и на какой строке возникла ошибка, необходимо знать параметры запроса на которых возникает ошибка. И вот тут нам на помощь приходит «Логирование событий», потому что помимо места возникновения ошибки мы узнаем параметры вызова процедуры, в которой возникает ошибка и это очень упрощает исправление бага.

Первая статья посвящена базовому функционалу «Логирования событий». В простейшей реализации это одна общая таблица и пакет процедур для работы с ней. Для создания и демонстрации логирования, нам необходимо реализовать следующие объекты БД (весь список объектов с их исходными кодами представлен в Git):

  1. Таблица messagelog — единая таблица логов. Именно в данной таблице будет храниться информация о дате и времени события, об объекте где происходит событие, типе события с указанием кода, текста и параметров. В нашем примере, столбец backtrace вынесен в отдельную таблицу messagelog_backtrace для удобства.

    Примечание. Представленное ниже описание таблицы является демонстрационным с минимальным набором столбцов для создания простейшего функционала логирования. Наличие дополнительных столбцов и их тип данных может меняться в зависимости от целей и задач логирования.

    Также, учитывайте пожалуйста, что создание партиции требует как минимум Oracle EE. Создание партиции вне указанной версии Oracle приведет к нарушению лицензионного соглашения.

    Структура таблицы

    Название столбца

    Тип данных

    Комментарий

    id

    number primary key

    Первичный ключ таблицы

    sessionid

    number(38)

    Идентификатор сессии для объединения группы ошибок

    objname

    varchar2(60)

    Содержит наименование объекта из которого было инициировано событие. Чаще всего это комбинация имя_пакета.имя_процедуры

    msgtype

    varchar2(3)

    Тип события, например: ERR — сообщение об ошибке; INF — информационное сообщение; WRN — сообщение с предупреждением и т.д.

    insertdate

    date

    Дата и время создания записи о событии

    msgcode

    varchar2(10)

    Код ошибки, чаще всего это SQLCODE, либо код ошибки из справочника ошибок (об этом будет отдельная статья)

    msgtext

    varchar2(500)

    Текст ошибки, чаще всего это SQLERRM, либо текст ошибки из справочника ошибок

    paramvalue

    varchar2(500)

    Наименования параметров процедуры/функции и их значения. При анализе ошибок удобно видеть с каким параметрами была запущена та или иная процедура, выполнить повторный запуск этой процедуры с указанными параметрами и воспроизвести ошибку

    backtrace

    varchar2(4000)

    Записывается результат функции dbms_utility.format_error_backtrace, либо иная дополнительная информация

    Исходный код таблицы

    create table messagelog(id         number(38)      not null,
                            sessionid  number(38)      not null,
                            msgtype    varchar2(3)     not null,
                            objname    varchar2(60)    default null,
                            insertdate date            default sysdate,
                            msgcode    varchar2(10)    default null,
                            msgtext    varchar2(4000)  default null,
                            paramvalue varchar2(4000)  default null,
                            constraint pk_messagelog_id primary key (id))
    partition by range (insertdate)
      interval (numtoyminterval(3, 'MONTH'))
        (partition p1 values less than (to_date('01.01.2020', 'DD.MM.YYYY'))); 

    *Исходный код других используемых объектов смотрите в Git

    В данной статье не будем подробно описывать способы индексирования и партицирования таблицы, всё зависит от целей и задач логирования. Самый предпочтительный вид хранения таблицы это партицирование по столбцу insertdate с интервалом 3 месяца.

    Отдельно стоит упомянуть про столбцы msgtext, paramvalue и backtrace. По своему опыту скажу, что указанные столбцы в 60% — 70% случаев будут пустыми и это очень сильно не нравится архитекторам и/или руководителям отделов разработки. Поэтому иногда есть смысл вынести указанные столбцы в отдельную таблицу с привязкой по id записи. В нашем примере столбец backtrace вынесен в отдельную таблицу messagelog_backtrace (см. исходный код в git).

  2. Пакет pkg_msglog — содержит набор процедур необходимых для логирования событий. В нашем примере будут представлены одна «корневая» и две процедуры для логирования событий: «ERR» — событие об ошибке; «WRN» — предупреждающее событие. Вызов корневой процедуры выполняется в автономной транзакции и делается это для того, чтобы была возможность выполнить сохранение данных об ошибке с откатом всех предыдущих изменений.

    Исходный код процедур пакета

    -- процедура логирования ошибок
    procedure p_log_err(p_objname    in varchar2,
                        p_msgcode    in varchar2,
                        p_msgtext    in varchar2 default null,
                        p_paramvalue in varchar2 default null,
                        p_backtrace  in varchar2 default null)
      is
      begin
        p_insert_log(p_msgtype_    => 'ERR',
                     p_sessionid_  => v_sid,
                     p_objname_    => p_objname,
                     p_insertdate_ => sysdate,
                     p_msgcode_    => p_msgcode,
                     p_msgtext_    => p_msgtext,
                     p_paramvalue_ => p_paramvalue,
                     p_backtrace_  => p_backtrace);
      end p_log_err;
      
      -- корневая процедура логирования
      procedure p_insert_log(p_msgtype_    in varchar2,
                             p_sessionid_  in number,
                             p_objname_    in varchar2,
                             p_insertdate_ in date,
                             p_msgcode_    in varchar2,
                             p_msgtext_    in varchar2 default null,
                             p_paramvalue_ in varchar2 default null,
                             p_backtrace_  in varchar2 default null)
      is
        v_id   messagelog.id%type;
        pragma autonomous_transaction;
      begin
        insert into messagelog(msgtype,
                               sessionid,
                               objname,
                               insertdate,
                               msgcode,
                               msgtext,
                               paramvalue)
            values(p_msgtype_,
                   p_sessionid_,
                   p_objname_,
                   p_insertdate_,
                   p_msgcode_,
                   p_msgtext_,
                   p_paramvalue_)
        return id
          into v_id;
        if trim(p_backtrace_) is not null then
          insert into messagelog_backtrace(id,
                                           backtrace)
          values(v_id,
                 trim(p_backtrace_));
        end if;
    
        commit;
      end p_insert_log;

    *Исходный код других используемых объектов смотрите в Git

  3. Тестовый пакет pkg_clients — пакет с демонстрационными процедурами поиска и создания пользователей. Обратите внимание, что процедура p_insert_user выполняет функции аудита события — «создание нового пользователя».

    Исходный код процедур поиска и создания пользователей

    create or replace package body pkg_clients 
    as
      procedure p_insert_user(p_login_     in varchar2,
                              p_firstname_ in varchar2,
                              p_lastname_  in varchar2,
                              p_id_        out number)
      is
        v_id clients.id%type;
      begin
        insert into clients(login,
                            firstname,
                            lastname)
            values(upper(p_login_),
                   p_firstname_,
                   p_lastname_)
        return id
          into v_id;
        if v_id > 0 then
          -- аудит события - создание нового пользователя
          pkg_msglog.p_log_wrn(p_objname    => 'pkg_clients.p_insert_user',
                               p_msgcode    => '101',
                               p_msgtext    => 'Создан новый пользователь с id = '||v_id,
                               p_paramvalue => 'p_login = '||p_login_
                                                 ||', p_firstname = '||p_firstname_
                                                 ||', p_lastname = '||p_lastname_);
        end if;
        commit;
      exception
        when others then
          pkg_msglog.p_log_err(p_objname    => 'pkg_clients.p_insert_user',
                               p_msgcode    => SQLCODE,
                               p_msgtext    => SQLERRM,
                               p_paramvalue => 'p_login_ = '||p_login_
                                                 ||', p_firstname_ = '||p_firstname_
                                                 ||', p_lastname_ = '||p_lastname_,
                               p_backtrace  => dbms_utility.format_error_backtrace);
          raise;
      end p_insert_user;
      
      procedure p_create_user(p_login     in varchar2,
                              p_firstname in varchar2,
                              p_lastname  in varchar2,
                              p_id        out number)
      is
        v_id clients.id%type;
      begin
        begin
          select id
            into v_id
            from clients
           where login = upper(p_login);
        exception
          when no_data_found then
            p_insert_user(p_login_     => p_login,
                          p_firstname_ => p_firstname,
                          p_lastname_  => p_lastname,
                          p_id_        => v_id);
        end;   
        p_id := v_id;
      exception
        when others then
          pkg_msglog.p_log_err(p_objname    => 'pkg_clients.p_create_user',
                               p_msgcode    => SQLCODE,
                               p_msgtext    => SQLERRM,
                               p_paramvalue => 'p_login = '||p_login
                                                 ||', p_firstname = '||p_firstname
                                                 ||', p_lastname = '||p_lastname,
                               p_backtrace  => dbms_utility.format_error_backtrace);
          raise;
      end p_create_user;
    end pkg_clients;

    *Исходный код других используемых объектов смотрите в Git

Демонстрация логирования событий

Для демонстрации работы выполним три стандартных кейса с помощью созданного ранее пакета pkg_clients.

  1. (Ошибочная ситуация) Создание пользователя с длиной имени превышающей допустимое значение в таблице

    Пример 1

    рис. пример запуска процедуры

    рис. пример запуска процедуры

    Результат

    рис. описание ошибки

    рис. описание ошибки
  2. (Ошибочная ситуация) Создание пользователя с пустым значением имени или фамилии (предположим, что есть такое требование)

    Пример 2

    рис. пример запуска процедуры

    рис. пример запуска процедуры

    Результат

    рис. описание ошибки

    рис. описание ошибки
  3. (Успешная ситуация) Создание нового пользователя с аудитом события

    Пример 3

    рис. пример запуска процедуры

    рис. пример запуска процедуры

    Результат

    рис. описание ошибки

    рис. описание ошибки

    Обратите внимание, что все демонстрационные процедуры завершаются блоком exception when others then с дальнейшим вызовом процедуры логирования события вне зависимости от «важности» процедуры.

Заключение

В заключении я повторюсь, что гораздо проще и правильнее добавлять логирование событий в обработку исключений при первом написании процедуры (функции) т.к. это не занимает много времени. Гораздо сложнее в будущем, внести правки в большое количество объектов БД и это займет гораздо больше времени на написание кода, тестирование и т.д.

По своему опыту отмечу, что пакет pkg_msglog (со всеми дополнительными объектами логирования) должен быть создан самым первым в проектируемой БД и в дальнейшем другие объекты создаются с логированием событий. Также, часто в командах появляются разработчики, которые говорят: «Зачем логировать все процедуры (функции)? Давайте вести логирование только важных и нужных процедур (функций)!». Вот здесь скрывается самая главная и распространенная ошибка. У всех нас разное понимание что есть «важная и нужная» процедура (функция). По сути, вы создаете логирование ошибок, которое «как бы и есть, но как бы его нет» т.е. все в команде знают, что в БД есть лог ошибок, но в нужный момент (например, при возникновении «блокера» на продакшн) вы обнаружите что в логах нет никакой информации об ошибке. Именно в такие моменты и возникает ошибочное мнение, что логировать события бесполезно и бессмысленно, а ведь это не так.

В следующей статье я расскажу как доработать «Логирование событий», чтобы мы могли идентифицировать события (установить принадлежность события к конкретному бизнес-процессу), установить приоритет события и максимально быстро реагировать на факт появления таких событий.

Спасибо за внимание.

Upd. 15.04.2021. Добавлен столбец sessionid для объединения группы ошибок возникших в рамках одной сессии.

Upd. 04.05.2021. Слияние, создание перекрестных ссылок со второй статьей.

how to check alert log errors in oracle

What is alert log file in Oracle

The alert log file (also referred to as the ALERT.LOG) is a chronological log of messages and errors written out by an Oracle Database. Typical messages found in this file are database startup, shutdown, log switches, space errors, etc. This file should constantly be monitored to detect unexpected messages and corruption.

Oracle will automatically create a new alert log file whenever the old one is deleted.

Alert log location

The location can find out using the parameter background_dump_dest

sqlplus / as sysdba
show parameter background_dump_dest

Beginning with Release 11g, the alert log file is written as XML formatted and as a text file (like in previous releases). The default location of both these files is the new ADR home (Automatic Diagnostic Repository, yet another new dump dest in 11g).

The ADR is set by using the DIAGNOSTIC_DEST initialization parameter. But you can still find the alert log location using the parameter background_dump_dest.
background_dump_dest is set like

$diagnostic_dest/diag/rdbms/<db_unique_name>/<instance_name>/trace

11g Alert log new features

Beginning with Release 11g of Oracle Database, the alert log is written as both an XML-formatted file and as a text file, as in earlier releases. Both these log files are stored inside the ADR home. The ADR root directory is known as ADR BASE. The Automatic Diagnostic Repository (ADR) is a directory structure that is stored outside of the database. This parameter is set by DIAGNOSTIC_DEST initialization parameter.

SQL> show parameter diagno
NAME TYPE VALUE
--------------------------- ----------- ------------------------------
diagnostic_dest string /u001/oracle/product/XPROD11g/diag

The location of an ADR home is given by the following path, which starts at the ADR base directory:

ADR_BASE/diag/product_type/product_id/instance_id

For example,
So for RDBMS oracle Home of Database name XPROD

ADR_base/diag/rdbms/XPROD/XPROD

Within the ADR home directory are subdirectories where the database instance stores diagnostic data.
alert Log file,The XML-formatted alert log, trace Background and server process trace files and SQL trace files , text alert.log file , cdump Core files

XML formatted alert.log
The alert log is named log.xml and is stored in the alert subdirectory of ADR home.
To get the log.xml path

ADR_BASE/diag/product_type/product_id/instance_id/alert

From Sqlplus

SQL> select value from v$diag_info where name ='Diag Alert';

ADRCI utility to view a text version of the alert log (with XML tags stripped)

Text formatted alert.log
The alert.log is named alertSID.log and is stored in the trace subdirectory of ADR home.
To view the text-only alert.log file

ADR_BASE/diag/product_type/product_id/instance_id/trace

from sqlplus

SQL> select value from v$diag_info where name ='Diag Trace';
or
SQL > Show parameter background_dump_dest

Open file alert_SID.log with a text editor

With 11g ,Oracle provides a way to look the alert log file from the database also. There is a fixed table X$DBGALERTEXT, when you query it, Oracle reads the log.xml from alert directory (which contains all the data what alert.log does), parses it and returns the details back as rows:

SQL> select message_text from X$DBGALERTEXT where rownum desc X$DBGALERTEXT
Name Null? Type
------------------------------- -------- ----------------------------
1 ADDR RAW(4)
2 INDX NUMBER
3 INST_ID NUMBER
4 ORIGINATING_TIMESTAMP TIMESTAMP(3) WITH TIME ZONE
5 NORMALIZED_TIMESTAMP TIMESTAMP(3) WITH TIME ZONE
6 ORGANIZATION_ID VARCHAR2(64)
7 COMPONENT_ID VARCHAR2(64)
8 HOST_ID VARCHAR2(64)
9 HOST_ADDRESS VARCHAR2(16)
10 MESSAGE_TYPE NUMBER
11 MESSAGE_LEVEL NUMBER
12 MESSAGE_ID VARCHAR2(64)
13 MESSAGE_GROUP VARCHAR2(64)
14 CLIENT_ID VARCHAR2(64)
15 MODULE_ID VARCHAR2(64)
16 PROCESS_ID VARCHAR2(32)
17 THREAD_ID VARCHAR2(64)
18 USER_ID VARCHAR2(64)
19 INSTANCE_ID VARCHAR2(64)
20 DETAILED_LOCATION VARCHAR2(160)
21 PROBLEM_KEY VARCHAR2(64)
22 UPSTREAM_COMP_ID VARCHAR2(100)
23 DOWNSTREAM_COMP_ID VARCHAR2(100)
24 EXECUTION_CONTEXT_ID VARCHAR2(100)
25 EXECUTION_CONTEXT_SEQUENCE NUMBER
26 ERROR_INSTANCE_ID NUMBER
27 ERROR_INSTANCE_SEQUENCE NUMBER
28 VERSION NUMBER
29 MESSAGE_TEXT VARCHAR2(2048)
30 MESSAGE_ARGUMENTS VARCHAR2(128)
31 SUPPLEMENTAL_ATTRIBUTES VARCHAR2(128)
32 SUPPLEMENTAL_DETAILS VARCHAR2(128)
33 PARTITION NUMBER
34 RECORD_ID NUMBER

There’s also a fixed table X$DBGDIREXT, which returns all file and directory names under [diagnostic_dest]/diag directory:

SQL> select lpad(' ',lvl,' ')||logical_file file_name
from X$DBGDIREXT
where rownum < 2;

12c or above Alert log new features

With 12c and above, background_dump_dest is depreciated. We can find the alert log location using below also

adrci
adrci> show alert
adrci> show alert -tail 100

how to check alert log errors in oracle using Unix Command

Go to the background dump directory to run these unix commands
Date and errors in alert.log

cat alert*log |
awk 'BEGIN{buf=""}
/[0-9]:[0-9][0-9]:[0-9]/{buf=$0}
/ORA-/{print buf,$0}'

How to find the Date of startups in the alert.log

cat alert*log |
awk 'BEGIN{buf=""}
/[0-9]:[0-9][0-9]:[0-9]/{buf=$0}
/Starting ORACLE/{print buf,$0}'

How to easily find the Oracle database startup and shutdown time using sqlplus

Here are the steps required on How to easily find the Oracle database startup and shutdown time using sqlplus
step 1)  Create a database directory object

 create or replace directory data_dir as 'Specify the Backgound dump Dest location'
 /
 Directory created.
 CREATE TABLE alert_log
 ( text_line varchar2(255))
 ORGANIZATION EXTERNAL
 (
 TYPE ORACLE_LOADER
 DEFAULT DIRECTORY data_dir
 ACCESS PARAMETERS
 (
 records delimited by newline fields
 REJECT ROWS WITH ALL NULL FIELDS
 )
 LOCATION
 (
 'alert_.log'
 )
 )
 REJECT LIMIT unlimited
 /
 Table created.

step 2)  Use the below query to find out the timing

select to_char(last_time,'dd-mon-yyyy hh24:mi') shutdown,
 to_char(start_time,'dd-mon-yyyy hh24:mi') startup,
 round((start_time-last_time)2460,2) mins_down,
 round((last_time-lag(start_time) over (order by r)),2) days_up,
 case when (lead(r) over (order by r) is null )
 then round((sysdate-start_time),2)
 end days_still_up
 from (
 select r,
 to_date(last_time, 'Dy Mon DD HH24:MI:SS YYYY') last_time,
 to_date(start_time,'Dy Mon DD HH24:MI:SS YYYY') start_time
 from (
 select r,
 text_line,
 lag(text_line,1) over (order by r) start_time,
 lag(text_line,2) over (order by r) last_time
 from (
 select rownum r, text_line
 from alert_log
 where text_line like '_  _ ::_ 20_'
 or text_line like 'Starting ORACLE instance %'
 )
 )
 where text_line like 'Starting ORACLE instance %'
 )
 /

Related Articles
ORA-00942 table or view does not exist
ora-29913: error in executing odciexttableopen callout
ORA-00257: archiver error. Connect internal only, until freed.
ORA-03113: end-of-file on communication channel
ORA-27154: post/wait create failed during startup
how to find archive log sequence number in oracle
https://docs.oracle.com/cd/B28359_01/server.111/b28310/diag005.htm#ADMIN11267

The Default Oracle Alert Log Location

Oracle alert log location may vary from version to version, and sometimes it’s not easily found. However, it usually locates at 3 locations.

For 11g and later releases (12c, 18c, 19c)

$ORACLE_BASE/diag/rdbms/$DB_UNIQUE_NAME/$ORACLE_SID/trace/

As we can notice, Oracle software divides alert logs to their own dedicated directories according to their database unique names, which is based on directory structure of diagnostic repository.

For 9i and 10g

$ORACLE_BASE/admin/$ORACLE_SID/bdump/

It’s in bdump.

For rest of cases without any clues

$ORACLE_HOME/rdbms/log/

Which is the same way to determine DIAGNOSTIC_DEST.

Filename Format of Alert Log

Even though its location may vary from version to version, the alert log file name is always:

alert_$ORACLE_SID.log

Occasionally, you cannot find the alert log file anywhere. Next, let’s see how we can find it.

3 Ways to Find Oracle Alert Log Location

Here I introduce three ways to find the right Oracle alert log location.

1. Background Dump Destination

If you are a DBA, you can query the value of BACKGROUND_DUMP_DEST, an initialization parameter, where you can find Oracle alert log.

SQL> show parameter background_dump_dest;

NAME                                 TYPE        VALUE
------------------------------------ ----------- ------------------------------
background_dump_dest                 string      /u01/app/oracle/diag/rdbms/orc
                                                 l/ORCL/trace

Or in this way:

SQL> select value from v$parameter where name = 'background_dump_dest';

VALUE
--------------------------------------------------------------------------------
/u01/app/oracle/diag/rdbms/orcl/ORCL/trace

In case that you cannot query the database, you can still find the location of alert log at OS-level.

2. Using Find Command

Almost all UNIX and Linux have find command, so using it to find some files is a very common way to do it.

[oracle@test ~]$ find $ORACLE_BASE -type f -name alert_$ORACLE_SID.log
/u01/app/oracle/diag/rdbms/orcl/ORCL/trace/alert_ORCL.log

We find a file named alert_$ORACLE_SID.log starting from $ORACLE_BASE. As we can see, the file was found.

3. Using Locate Command

Sometimes, using locate command will be faster than find to find the path of alert log file.

[oracle@test ~]$ locate alert_$ORACLE_SID.log
/u01/app/oracle/diag/rdbms/orcl/ORCL/trace/alert_ORCL.log

In some databases, the alert logs are big, maybe too big to make room for other files. We have to know ways to reduce the size of alert logs.

For your reference, the ways to find the location of listener log are slightly different from the ways we find Oracle alert log. You can take a look.

When you take your first look at an Oracle database, one of the first questions is often «where’s the alert log?». Grid Control can tell you, but its often not available in the environment.

I posted some bash and Perl scripts to find and tail the alert log on my blog some time back, and I’m surprised to see that post still getting lots of hits.

The technique used is to lookup background_dump_dest from v$parameter. But I only tested this on Oracle Database 10g.

Is there a better approach than this? And does anyone know if this still works in 11g?

brian d foy's user avatar

brian d foy

130k31 gold badges208 silver badges594 bronze badges

asked Oct 5, 2008 at 12:03

tardate's user avatar

Am sure it will work in 11g, that parameter has been around for a long time.

Seems like the correct way to find it to me.

If the background_dump_dest parameter isn’t set, the alert.log will be put in $ORACLE_HOME/RDBMS/trace

answered Oct 5, 2008 at 12:04

cagcowboy's user avatar

cagcowboycagcowboy

30k11 gold badges70 silver badges93 bronze badges

1

Once you’ve got the log open, I would consider using File::Tail or File::Tail::App to display it as it’s being written, rather than sleeping and reading. File::Tail::App is particularly clever, because it will detect the file being rotated and switch, and will remember where you were up to between invocations of your program.

I’d also consider locking your cache file before using it. The race condition may not bother you, but having multiple people try to start your program at once could result in nasty fights over who gets to write to the cache file.

However both of these are nit-picks. My brief glance over your code doesn’t reveal any glaring mistakes.

answered Oct 6, 2008 at 0:56

pjf's user avatar

pjfpjf

5,99326 silver badges42 bronze badges

1

Back to: Oracle DBA Tutorials

Alert Log Files in Oracle with Examples

In this article, I am going to discuss Alert log Files in Oracle with Examples. Please read our previous article where we discussed the Automatic Diagnostic Repository in Oracle with Examples.

What is an Alert Log File in Oracle?

The alert log is a chronological log of messages and errors, and includes the following items:

  • Any non-default initialization parameter used at startup.
  • All internal errors (ORA-00600), block corruption errors (ORA-01578), and deadlock errors (ORA-00060) occur.
  • Administrative operations, such as some CREATE, ALTER, and DROP statements and STARTUP, SHUTDOWN, and ARCHIVELOG statements. You can see create databases, alter databases, and not all of the DDL operations but some of the DDL operations. But you can enable other DDL operations. Using the alert log we can see the last time the database had started, the last time the database had rebooted, and the last time a new pluggable database was created.
  • Messages and errors relating to the functions of shared server and dispatcher processes.
  • Errors occur during the automatic refresh of the materialized view.
  • The locations of the various diagnostics directories can be displayed using the V$DIAG_INFO view. For example select name, the value from v$diag_info; Using this query, we can able to see all the directories present in the database of diagnostics like alert log files and trace files.
  • The alert log file can be viewed using a text editor or using ADRCI. ADRCI is a little bit complicated compared to the text editor. So, we can mostly use the text editor to view alert log files.
Examples to understand Alert log Files in Oracle:

Now, let us do an exercise on the alert log concept. Open the Linux machine and open the terminal and login into the database as “sqlplus / as sysdba”.

Examples to understand Alert log Files in Oracle

Use the column formatting to organize the query

Query:
Col name format a25
Col value format a50
Select name, value from v$diag_info;

Examples to understand Alert log Files in Oracle

We can see the directories as

  • ADR BASE is /u01/app/oracle/
  • ADR Home as /u01/app/oracle/diag/rdbms/jayadb/jayadb.
  • Diag Trace as /u01/app/oracle/dig/rdbms/jayadb/jayadb/alert/

There are other folders for cdump, incident, health monitor, and the default trace files. We use the above query to find the locations of diagnostic directories. We can see Active Problem Count is 0 and the Active Incident count is also 0 which indicates that until now there are no problems indicated in the database so that we can reach oracle support. If the count is not 0 then we have to connect to Oracle support. We have a folder for each and every diagnostic. There will be more information about their respective directories.

Examples to understand Alert log Files in Oracle:

From the above query, we can see all the diagnostic directories. We will select the location of the alert log file. The alert log can be viewed in two places. In the folder called alert and, in the folder, called Trace.

Examples to understand Alert log Files in Oracle

So, now we will be checking the alert folder /u01/app/oracle/dig/rdbms/jayadb/jayadb/alert/. This has the XML version of the alert log.

Examples to understand Alert log Files in Oracle

We have only one file log.xml. Let us open the file using the gedit application.

Examples to understand Alert log Files in Oracle

Once the file below is opened. The file will represent below.

Examples to understand Alert log Files in Oracle

The above-marked part of the file is the starting portion of the log.xml file. From the above-marked field, we can find out the time the database started, hostname, IP Address, and much more.

Examples to understand Alert log Files in Oracle

So, we can see all the alerts that occurred to the database in this log.xml file. We can even search for any specific type of alerts that occurred to the database. Now, let’s try to search how many times the database has started in the alert log file. We will be using ctrl+F to find any specific term here.

Examples to understand Alert log Files in Oracle

We can see the total number of terms “Starting Oracle instance” occurred 43 times. This indicates we have started the oracle instance 43 times.

Note: Searching in the editor is case-insensitive.

Let us try to search for the term “alter database”.

Examples to understand Alert log Files in Oracle

We can see there are 238 entries for alter database term. Here from the results, we have made the command “alter database mount” and the time the command is executed and the IP Address and the hostname. This information will be useful in the case of restoration.

Now, that we got a little bit idea about the XML version of the alert log. Let us open the original version of the alert log file in /u01/app/oracle/dig/rdbms/jayadb/jayadb/trace/

Examples to understand Alert log Files in Oracle

We can see there are many files. But the file with alert_jayadb.log is the alert log file. This is the text version of the alerts. Let’s go ahead and open with gedit application.

Examples to understand Alert log Files in Oracle

This is the entire file and what it looks like.

Examples to understand Alert log Files in Oracle

We will find the same information but in the text version. Now, let’s try to search for the shutdown, and let’s see how many times we have shut down the database.

Examples to understand Alert log Files in Oracle

We can see that the total count of shutdowns is 90. We can search for the entire file and find out the time when the shutdown and much more information such as what is the exact process of shutting down the database. Let us go to the bottom of the file and scroll a bit to the top until you find the term “system parameter with non-default values”.

Examples to understand Alert log Files in Oracle

In our case, we found it on 23-10-2022. We can see the parameters here and these are the parameters that are used to start the database. The Number of processes is 300 and the open_cursors is 410. Previously we have changed the value of the parameter open_cursors to 410. After that, we restarted the database and that is the reason, we can see the value 410 for the open_cursors parameter. If in case if the database is crashed, we can use these parameters to recover the database and restart the database again.

Examples to understand Alert log Files in Oracle

Note: Let us recall what we have discussed until now, we have two folders to find the alert log of the database. The first file is the XML version of the alert log and another version is the text version of the alert log file. We can open the file with any text editor and search for the terms we are looking.

In the next article, I am going to discuss Alert Log using ADRCI in Oracle with Examples. Here, in this article, I try to explain Alert log Files in Oracle with Examples and I hope you enjoy this Alert log Files in Oracle with Examples article.

Понравилась статья? Поделить с друзьями:
  • Oriel 963 ошибка ash как исправить
  • Oracle коды пользовательских ошибок
  • Oracle vm virtualbox ошибка при запуске виртуальной машины
  • Oriel 963 ash ошибка что делать
  • Oracle vm virtualbox ошибка 0x80004005