简体   繁体   中英

Regex for finding specific text in Jenkins console logs

I am writing Python script for filtering Jenkins console log for a Jenkins job and I want to search for a specific pattern/text with regular expressions. Below is the text that I am looking for:

somename.SomeTest.testSomeName()

This is a part of following paragraph or sentence:

Failed to find the annotation and the status of the test public void 
com.somename.qa.mobile.tests.somename.SomeTest.testSomeName().
The result is not deployed to Platform but we will proceed 
with further tests.......

Above such sentences are present in the console log for more than 20 times. I want to find each instance of this and get that method name somename.SomeTest.testSomeName() . I have attempted this so far \\[([^\\]]+)\\] not deployed ([^ ]+) and something is missing/incorrect as a part of this regular expression

Following is the small snippet of the log and I want to find out test method name from each of below shown Failed to find the annotation.... sentences:

21:18:19    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:170)
21:18:19    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:84)
21:18:19    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:92)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.somename.SomeTest.testSomeName(). The result is not deployed to Platform but we will proceed with further tests
21:18:26 somename.client.test.utilities.Platform.PlatformApiException: Platform API returned HTTP 400("Field :case_id is not a valid test case.")
21:18:26    at somename.client.test.utilities.platform.PlatformApiClient.sendRequest(PlatformApiClient.java:197)
21:18:26    at

Above is just a small snippet; imagine occurrence of above snippet in the log like more than 20 times.

Description

^.*?(\[Error\]).*?\.((?:[a-z]+\.){2}[a-z]+\(\))

正则表达式可视化

This regex will do the following:

  • find all the lines with [error]
  • capture the method name which are the three blobs of text preceding the ()

Example

Live Example

https://regex101.com/r/kI4cL2/1

Sample Text

21:18:19    at org.apache.maven.surefire.testng.TestNGExecutor.run(TestNGExecutor.java:170)
21:18:19    at org.apache.maven.surefire.testng.TestNGXmlTestSuite.execute(TestNGXmlTestSuite.java:84)
21:18:19    at org.apache.maven.surefire.testng.TestNGProvider.invoke(TestNGProvider.java:92)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:200)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:153)
21:18:19    at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:103)
21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.alpha.SomeTest.testSomeName(). The result is not deployed to Platform but we will proceed with further tests
21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.bravo.SomeTest.testSomeName(). The result is not deployed to Platform but we will proceed with further tests
21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.charlie.SomeTest.testSomeName(). The result is not deployed to Platform but we will proceed with further tests
21:18:26 somename.client.test.utilities.Platform.PlatformApiException: Platform API returned HTTP 400("Field :case_id is not a valid test case.")
21:18:26    at somename.client.test.utilities.platform.PlatformApiClient.sendRequest(PlatformApiClient.java:197)
21:18:26    at

Sample Matches

[0][0] = 21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.alpha.SomeTest.testSomeName()
[0][1] = [ERROR]
[0][2] = alpha.SomeTest.testSomeName()

[1][0] = 21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.bravo.SomeTest.testSomeName()
[1][1] = [ERROR]
[1][2] = bravo.SomeTest.testSomeName()

[2][0] = 21:18:26 2016-05-12 21:18:25.238 [ERROR] (1): Failed to find the annotation and the status of the test public void com.somename.qa.mobile.tests.charlie.SomeTest.testSomeName()
[2][1] = [ERROR]
[2][2] = charlie.SomeTest.testSomeName()

Explanation

NODE                     EXPLANATION
----------------------------------------------------------------------
  ^                        the beginning of a "line"
----------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
----------------------------------------------------------------------
  (                        group and capture to \1:
----------------------------------------------------------------------
    \[                       '['
----------------------------------------------------------------------
    Error                    'Error'
----------------------------------------------------------------------
    \]                       ']'
----------------------------------------------------------------------
  )                        end of \1
----------------------------------------------------------------------
  .*?                      any character except \n (0 or more times
                           (matching the least amount possible))
----------------------------------------------------------------------
  \.                       '.'
----------------------------------------------------------------------
  (                        group and capture to \2:
----------------------------------------------------------------------
    (?:                      group, but do not capture (2 times):
----------------------------------------------------------------------
      [a-z]+                   any character of: 'a' to 'z' (1 or
                               more times (matching the most amount
                               possible))
----------------------------------------------------------------------
      \.                       '.'
----------------------------------------------------------------------
    ){2}                     end of grouping
----------------------------------------------------------------------
    [a-z]+                   any character of: 'a' to 'z' (1 or more
                             times (matching the most amount
                             possible))
----------------------------------------------------------------------
    \(                       '('
----------------------------------------------------------------------
    \)                       ')'
----------------------------------------------------------------------
  )                        end of \2

I assume you can scan the output per-line. Try this regexp

.* ([^ ]+\(\)).*not deployed.*

Example how this works with your example using Perl (regexp is the same)

$ cat example | perl -ne 'print $1 if /([^ ]+\(\)).*not deployed/;'
com.somename.qa.mobile.tests.somename.SomeTest.testSomeName()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM