Skip to content

Commit 8478afe

Browse files
committed
add prefix to entry and pattern parser
1 parent 700f2ca commit 8478afe

File tree

6 files changed

+52
-17
lines changed

6 files changed

+52
-17
lines changed

src/Log/Entry.php

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ class Entry implements EntryInterface
1515
protected array $lines = [];
1616
protected ?LevelInterface $level = null;
1717
protected ?int $time = null;
18+
protected ?string $prefix = null;
1819
protected int $iterator = 0;
1920

2021
/**
@@ -95,6 +96,28 @@ public function getTime(): ?int
9596
return $this->time;
9697
}
9798

99+
/**
100+
* Set the prefix
101+
*
102+
* @param string $prefix
103+
* @return $this
104+
*/
105+
public function setPrefix(string $prefix): static
106+
{
107+
$this->prefix = $prefix;
108+
return $this;
109+
}
110+
111+
/**
112+
* Get the prefix
113+
*
114+
* @return string|null
115+
*/
116+
public function getPrefix(): ?string
117+
{
118+
return $this->prefix;
119+
}
120+
98121
/**
99122
* Return the current element
100123
*
@@ -214,6 +237,7 @@ public function jsonSerialize(): array
214237
return [
215238
'level' => $this->getLevel(),
216239
'time' => $this->getTime(),
240+
'prefix' => $this->getPrefix(),
217241
'lines' => $this->getLines()
218242
];
219243
}

src/Parser/PatternParser.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ class PatternParser extends Parser
2121
/**
2222
* Match constants, see setMatches()
2323
*/
24-
const string TIME = "time";
25-
const string LEVEL = "level";
24+
public const string TIME = "time";
25+
public const string LEVEL = "level";
26+
public const string PREFIX = "prefix";
2627

2728
/**
2829
* @var class-string<EntryInterface>
@@ -172,6 +173,9 @@ protected function parseEntryMatch(Entry $entry, string $matchType, string $matc
172173
case static::LEVEL:
173174
$entry->setLevel($this->levelClass::fromString($matchString));
174175
break;
176+
case static::PREFIX:
177+
$entry->setPrefix($matchString);
178+
break;
175179
default:
176180
throw new InvalidArgumentException("Match type '" . $matchType . "' is not defined.");
177181
}

test/src/Log/TestPatternLog.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ class TestPatternLog extends AnalysableLog
2121
public static function getDefaultParser(): PatternParser
2222
{
2323
return (new PatternParser())
24-
->setPattern('/\[([^\]]+)\] \[[^\/]+\/([^\]]+)\].*/')
25-
->setMatches([PatternParser::TIME, PatternParser::LEVEL])
24+
->setPattern('/(\[([^\]]+)\] \[[^\/]+\/([^\]]+)\]).*/')
25+
->setMatches([PatternParser::PREFIX, PatternParser::TIME, PatternParser::LEVEL])
2626
->setTimeFormat('d.m.Y H:i:s');
2727
}
2828

test/tests/Analyser/PatternAnalyserTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,33 +27,33 @@ protected function getExpectedAnalysis(): Analysis
2727
->addInsight((new TestPatternProblem())
2828
->setCause("ABC")
2929
->increaseCounter()
30-
->setEntry((new Entry())->setTime(2)->setLevel(Level::ERROR)
30+
->setEntry((new Entry())->setTime(2)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:02] [Log/ERROR]")
3131
->addLine(new Line(2, "[01.01.1970 00:00:02] [Log/ERROR] I have a problem with ABC"))
3232
)
3333
)
3434
->addInsight((new TestPatternProblem())
3535
->setCause("XYZ")
36-
->setEntry((new Entry())->setTime(4)->setLevel(Level::ERROR)
36+
->setEntry((new Entry())->setTime(4)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:04] [Log/ERROR]")
3737
->addLine(new Line(4, "[01.01.1970 00:00:04] [Log/ERROR] I have a problem with XYZ"))
3838
)
3939
)
4040
->addInsight((new TestPatternProblem())
4141
->setCause("DEF")
42-
->setEntry((new Entry())->setTime(6)->setLevel(Level::ERROR)
42+
->setEntry((new Entry())->setTime(6)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:06] [Log/ERROR]")
4343
->addLine(new Line(6, "[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF"))
4444
->addLine(new Line(7, "I have a problem with GHI"))
4545
)
4646
)
4747
->addInsight((new TestPatternProblem())
4848
->setCause("GHI")
49-
->setEntry((new Entry())->setTime(6)->setLevel(Level::ERROR)
49+
->setEntry((new Entry())->setTime(6)->setLevel(Level::ERROR)->setPrefix("[01.01.1970 00:00:06] [Log/ERROR]")
5050
->addLine(new Line(6, "[01.01.1970 00:00:06] [Log/ERROR] I have a problem with DEF"))
5151
->addLine(new Line(7, "I have a problem with GHI"))
5252
)
5353
)
5454
->addInsight((new TestPatternInformation())
5555
->setValue("v1.2.3")
56-
->setEntry((new Entry())->setTime(7)->setLevel(Level::INFO)
56+
->setEntry((new Entry())->setTime(7)->setLevel(Level::INFO)->setPrefix("[01.01.1970 00:00:07] [Log/INFO]")
5757
->addLine(new Line(8, "[01.01.1970 00:00:07] [Log/INFO] This log was generated by software v1.2.3"))
5858
)
5959
);

test/tests/Log/EntryTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,14 @@ public function testSetGetTime(): void
4242
$this->assertEquals($time, $entry->getTime());
4343
}
4444

45+
public function testSetGetPrefix(): void
46+
{
47+
$entry = new Entry();
48+
$prefix = uniqid();
49+
$this->assertSame($entry, $entry->setPrefix($prefix));
50+
$this->assertEquals($prefix, $entry->getPrefix());
51+
}
52+
4553
public function testKey(): void
4654
{
4755
$entry = new Entry();

test/tests/Parser/PatternParserTest.php

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ protected function getSimpleExpectedLog(): Log
2222
{
2323
return (new TestPatternLog())
2424
->setLogFile(new PathLogFile(__DIR__ . '/../../data/simple.log'))
25-
->addEntry((new Entry())->setLevel(Level::INFO)->setTime(1)
25+
->addEntry((new Entry())->setLevel(Level::INFO)->setTime(1)->setPrefix("[01.01.1970 00:00:01] [Log/INFO]")
2626
->addLine(new Line(1, "[01.01.1970 00:00:01] [Log/INFO] This is the first message containing information.")))
27-
->addEntry((new Entry())->setLevel(Level::DEBUG)->setTime(2)
27+
->addEntry((new Entry())->setLevel(Level::DEBUG)->setTime(2)->setPrefix("[01.01.1970 00:00:02] [Log/DEBUG]")
2828
->addLine(new Line(2, "[01.01.1970 00:00:02] [Log/DEBUG] This is the second message containing a debug information.")))
29-
->addEntry((new Entry())->setLevel(Level::WARNING)->setTime(3)
29+
->addEntry((new Entry())->setLevel(Level::WARNING)->setTime(3)->setPrefix("[01.01.1970 00:00:03] [Log/WARN]")
3030
->addLine(new Line(3, "[01.01.1970 00:00:03] [Log/WARN] This is the third message containing a warning information.")))
31-
->addEntry((new Entry())->setLevel(Level::ERROR)->setTime(4)
31+
->addEntry((new Entry())->setLevel(Level::ERROR)->setTime(4)->setPrefix("[01.01.1970 00:00:04] [Log/ERROR]")
3232
->addLine(new Line(4, "[01.01.1970 00:00:04] [Log/ERROR] This is the third message containing an error information."))
3333
->addLine(new Line(5, "This line continues the error entry to add even more information."))
3434
->addLine(new Line(6, "This line is also part of the error entry.")))
35-
->addEntry((new Entry())->setLevel(Level::INFO)->setTime(5)
35+
->addEntry((new Entry())->setLevel(Level::INFO)->setTime(5)->setPrefix("[01.01.1970 00:00:05] [Log/INFO]")
3636
->addLine(new Line(7, "[01.01.1970 00:00:05] [Log/INFO] This is the last message of the log.")));
3737
}
3838

@@ -46,15 +46,14 @@ public function testParse(): void
4646
$this->assertEquals($this->getSimpleExpectedLog(), $log);
4747
}
4848

49-
5049
public function testParseWithCustomParser(): void
5150
{
5251
$logFile = new PathLogFile(__DIR__ . '/../../data/simple.log');
5352
$log = (new TestPatternLog())->setLogFile($logFile);
5453

5554
$patternParser = (new PatternParser())
56-
->setPattern('/\[([^\]]+)\] \[[^\/]+\/([^\]]+)\].*/')
57-
->setMatches([PatternParser::TIME, PatternParser::LEVEL])
55+
->setPattern('/(\[([^\]]+)\] \[[^\/]+\/([^\]]+)\]).*/')
56+
->setMatches([PatternParser::PREFIX, PatternParser::TIME, PatternParser::LEVEL])
5857
->setTimeFormat('d.m.Y H:i:s');
5958
$log->parse($patternParser);
6059

0 commit comments

Comments
 (0)