From b882ca0059cb147b2f7a6d5bf20aea23ed8bbd47 Mon Sep 17 00:00:00 2001 From: Matthias Neid Date: Sat, 20 Feb 2021 22:59:12 +0100 Subject: [PATCH] match all occurences of pattern in pattern analyser --- src/Analyser/PatternAnalyser.php | 29 +++++++++++++-------- test/data/problem.log | 4 ++- test/tests/Analyser/PatternAnalyserTest.php | 18 +++++++++++-- 3 files changed, 37 insertions(+), 14 deletions(-) diff --git a/src/Analyser/PatternAnalyser.php b/src/Analyser/PatternAnalyser.php index 2969d75..7d52cda 100644 --- a/src/Analyser/PatternAnalyser.php +++ b/src/Analyser/PatternAnalyser.php @@ -113,9 +113,11 @@ public function analyse() /** @var PatternInsightInterface $possibleInsightClass */ $patterns = $possibleInsightClass::getPatterns(); foreach ($patterns as $patternKey => $pattern) { - $insight = $this->analyseEntry($entry, $possibleInsightClass, $patternKey, $pattern); - if ($insight) { - $analysis->addInsight($insight); + $insights = $this->analyseEntry($entry, $possibleInsightClass, $patternKey, $pattern); + if ($insights) { + foreach ($insights as $insight) { + $analysis->addInsight($insight); + } } } } @@ -131,20 +133,25 @@ public function analyse() * @param string $possibleInsightClass * @param $patternKey * @param string $pattern - * @return bool|PatternInsightInterface + * @return bool|PatternInsightInterface[] */ protected function analyseEntry(EntryInterface $entry, string $possibleInsightClass, $patternKey, string $pattern) { - $result = preg_match($pattern, $entry, $matches); - if ($result !== 1) { + $result = preg_match_all($pattern, $entry, $matches, PREG_SET_ORDER); + if ($result === false || $result === 0) { return false; } - /** @var PatternInsightInterface $insight */ - $insight = new $possibleInsightClass(); - $insight->setMatches($matches, $patternKey); - $insight->setEntry($entry); + $return = []; + foreach ($matches as $match) { + /** @var PatternInsightInterface $insight */ + $insight = new $possibleInsightClass(); + $insight->setMatches($match, $patternKey); + $insight->setEntry($entry); - return $insight; + $return[] = $insight; + } + + return $return; } } \ No newline at end of file diff --git a/test/data/problem.log b/test/data/problem.log index 3a86d1a..e59715c 100644 --- a/test/data/problem.log +++ b/test/data/problem.log @@ -3,4 +3,6 @@ [01.01.1970 00:00:03] [Log/INFO] This is a message without any problem [01.01.1970 00:00:04] [Log/ERROR] I have a problem with XYZ [01.01.1970 00:00:05] [Log/ERROR] I have a problem with ABC -[01.01.1970 00:00:06] [Log/INFO] This log was generated by software v1.2.3 \ No newline at end of file +[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF +I have a problem with GHI +[01.01.1970 00:00:07] [Log/INFO] This log was generated by software v1.2.3 \ No newline at end of file diff --git a/test/tests/Analyser/PatternAnalyserTest.php b/test/tests/Analyser/PatternAnalyserTest.php index 1910e65..a56bb10 100644 --- a/test/tests/Analyser/PatternAnalyserTest.php +++ b/test/tests/Analyser/PatternAnalyserTest.php @@ -31,10 +31,24 @@ protected function getExpectedAnalysis() ->addLine((new Line())->setNumber(4)->setText("[01.01.1970 00:00:04] [Log/ERROR] I have a problem with XYZ")) ) ) + ->addInsight((new TestPatternProblem()) + ->setCause("DEF") + ->setEntry((new Entry())->setTime(6)->setLevel("ERROR") + ->addLine((new Line())->setNumber(6)->setText("[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF")) + ->addLine((new Line())->setNumber(7)->setText("I have a problem with GHI")) + ) + ) + ->addInsight((new TestPatternProblem()) + ->setCause("GHI") + ->setEntry((new Entry())->setTime(6)->setLevel("ERROR") + ->addLine((new Line())->setNumber(6)->setText("[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF")) + ->addLine((new Line())->setNumber(7)->setText("I have a problem with GHI")) + ) + ) ->addInsight((new TestPatternInformation()) ->setValue("v1.2.3") - ->setEntry((new Entry())->setTime(6)->setLevel("INFO") - ->addLine((new Line())->setNumber(6)->setText("[01.01.1970 00:00:06] [Log/INFO] This log was generated by software v1.2.3")) + ->setEntry((new Entry())->setTime(7)->setLevel("INFO") + ->addLine((new Line())->setNumber(8)->setText("[01.01.1970 00:00:07] [Log/INFO] This log was generated by software v1.2.3")) ) );