1

Я собираю множество файлов журналов для определенных ошибок, связанных с Oracle, с кодами ошибок ORA-xxxxx. Все записи журнала начинаются с отметки времени (например, 2017-11-29 23: 51: 46,372). Некоторые из записей журнала являются многострочными, например, с трассировкой стека исключений java, где коды ORA-xxxxx находятся глубоко под строкой записи журнала с отметкой времени.

Что такое регулярное выражение для поиска временной метки журнала вплоть до кода ORA-xxxxx?

Как только я получу приведенное выше регулярное выражение, следует отсортировать их по дате, чтобы найти последний раз, когда произошла определенная ошибка ORA-xxxxx.

PS: подойдет команда grep или perl regex.

Спасибо,

Пример файла журнала

2017-11-29 23:51:46,013  (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:76)     .   .   .

        at java.util.TimerThread.mainLoop(Timer.java:555)
        at java.util.TimerThread.run(Timer.java:505) Caused by: org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.exception.SQLStateConverter.handledNonSpecificException(SQLStateConverter.java:126)
        at org.hibernate.exception.SQLStateConverter.convert(SQLStateConverter.java:114)
        at org.hibernate.exception.JDBCExceptionHelper.convert(JDBCExceptionHelper.java:66)
        at org.hibernate.loader.Loader.doList(Loader.java:2235)
        at org.hibernate.loader.Loader.listIgnoreQueryCache(Loader.java:2129)
        at org.hibernate.loader.Loader.list(Loader.java:2124)
        at org.hibernate.loader.hql.QueryLoader.list(QueryLoader.java:401)
        at org.hibernate.hql.ast.QueryTranslatorImpl.list(QueryTranslatorImpl.java:363)
        at org.hibernate.engine.query.HQLQueryPlan.performList(HQLQueryPlan.java:196)
        at org.hibernate.impl.SessionImpl.list(SessionImpl.java:1149)
        at org.hibernate.impl.QueryImpl.list(QueryImpl.java:102)
        at org.hibernate.ejb.QueryImpl.getResultList(QueryImpl.java:67)
        ... 16 more Caused by: java.sql.SQLException: [Mercury][Oracle JDBC Driver][Oracle]
       ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")

1 ответ1

1

Этот perl one liner делает свою работу:

perl -0777 -nE 'say $1 while(/(\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+(?:(?!ORA-\d+)(?!\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+).)*ORA-\d+(?:(?!\d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+).)*)/sg)' file.txt

Это будет печатать содержимое группы 1 ($1) каждый раз, когда она его найдет.

Опции:

-0777   : slurp mode
-n      : add a loop around the script
-E      : enable features ("say" in this case)

Regex:

/                                           : regex delimiter
  (                                         : start group 1
    \d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+      : regex for date
    (?:                                     : start non capture group
      (?!                                   : negative look ahead, make sure we don't have the following
        ORA-\d+                             : literally "ORA-" followed by digits
      )                                     : end lookahead
      (?!                                   : negative look ahead, make sure we don't have the following
        \d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+  : regex for date
      )                                     : end lookahead
      .                                     : any character
    )*                                      : non capture group is present 0 or more times
    ORA-\d+                                 : literally "ORA-" followed by digits
    (?:                                     : start non capture group
      (?!                                   : negative look ahead, make sure we don't have the following
        \d{4}-\d\d-\d\d \d\d:\d\d:\d\d,\d+  : regex for date
      )                                     : end lookahead
      .                                     : any character
    )*                                      : non capture group is present 0 or more times
  )                                         : end group 1, contents in "$1"
/sg                                         : regex delimiter, s: single line, g: global

Пример входного файла:

2017-11-29 23:51:46,013  (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
       ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
2017-11-29 23:51:46,013  (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
      (NO ORA.....) unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")
2017-11-29 23:51:46,013  (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
       ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")

Результат:

2017-11-29 23:51:46,013  (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
       ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")

2017-11-29 23:51:46,013  (Foo.java:67) FATAL - foo.bar()-got exception
during load of zzz javax.persistence.PersistenceException:
org.hibernate.exception.GenericJDBCException: could not execute query
        at org.hibernate.ejb.AbstractEntityManagerImpl.throwPersistenceException(AbstractEntityManagerImpl.java:614)
       ORA-04031: unable to allocate 3896 bytes of shared
memory ("shared pool","selec t licensekey0_.ID as ID...","sga
heap(1,0)","kglsim object batch")

Второй блок не отображается

Всё ещё ищете ответ? Посмотрите другие вопросы с метками .