Skip to content

Commit

Permalink
Log time in debug CSV
Browse files Browse the repository at this point in the history
  • Loading branch information
Toflar committed Jun 9, 2020
1 parent 47dd820 commit 448c0ca
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 11 deletions.
4 changes: 4 additions & 0 deletions core-bundle/src/Crawl/Monolog/CrawlCsvLogHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@

class CrawlCsvLogHandler extends StreamHandler
{
public const DATETIME_FORMAT = 'Y-m-d H:i:s.u';

/**
* @var string
*/
Expand Down Expand Up @@ -52,6 +54,7 @@ protected function streamWrite($resource, array $record): void

if (0 === $size) {
fputcsv($resource, [
'Time',
'Source',
'URI',
'Found on URI',
Expand All @@ -62,6 +65,7 @@ protected function streamWrite($resource, array $record): void
}

$columns = [
$record['datetime']->format(self::DATETIME_FORMAT),
$record['context']['source'],
null === $crawlUri ? '---' : (string) $crawlUri->getUri(),
null === $crawlUri ? '---' : (string) $crawlUri->getFoundOn(),
Expand Down
35 changes: 24 additions & 11 deletions core-bundle/tests/Crawl/Monolog/CrawlCsvLogHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class CrawlCsvLogHandlerTest extends TestCase
/**
* @dataProvider writesCsvStreamProvider
*/
public function testWritesCsvStream(array $context, string $expectedContent, string $existingCsvContent = '', $message = 'foobar'): void
public function testWritesCsvStream(\DateTime $dt, array $context, string $expectedContent, string $existingCsvContent = '', $message = 'foobar'): void
{
$stream = fopen('php://memory', 'r+');

Expand All @@ -32,7 +32,7 @@ public function testWritesCsvStream(array $context, string $expectedContent, str
}

$handler = new CrawlCsvLogHandler($stream);
$handler->handle(['level' => Logger::DEBUG, 'message' => $message, 'extra' => [], 'context' => $context]);
$handler->handle(['level' => Logger::DEBUG, 'message' => $message, 'extra' => [], 'context' => $context, 'datetime' => $dt]);

rewind($stream);
$content = stream_get_contents($stream);
Expand All @@ -42,10 +42,14 @@ public function testWritesCsvStream(array $context, string $expectedContent, str

public function testSourceFilter(): void
{
$dt = new \DateTime();
$formattedDt = '"'.$dt->format(CrawlCsvLogHandler::DATETIME_FORMAT).'"';

$record = [
'level' => Logger::DEBUG,
'message' => 'foobar',
'extra' => [],
'datetime' => $dt,
'context' => [
'source' => 'source',
'crawlUri' => new CrawlUri(new Uri('https://contao.org'), 0),
Expand All @@ -68,42 +72,51 @@ public function testSourceFilter(): void
rewind($stream);
$content = stream_get_contents($stream);

$this->assertSame('Source,URI,"Found on URI","Found on level",Tags,Message'."\n".'source,https://contao.org/,,0,,foobar'."\n", $content);
$this->assertSame('Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n".$formattedDt.',source,https://contao.org/,,0,,foobar'."\n", $content);
}

public function writesCsvStreamProvider(): \Generator
{
$dt = new \DateTime();
$formattedDt = '"'.$dt->format(CrawlCsvLogHandler::DATETIME_FORMAT).'"';

yield 'Should not write anything if the source is missing' => [
$dt,
[],
'',
];

yield 'Correctly logs with no CrawlUri provided and empty log file (should write headlines)' => [
$dt,
['source' => 'source'],
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n".'source,---,---,---,---,foobar'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n".$formattedDt.',source,---,---,---,---,foobar'."\n",
];

yield 'Correctly logs with CrawlUri provided and empty log file (should write headlines)' => [
$dt,
['source' => 'source', 'crawlUri' => new CrawlUri(new Uri('https://contao.org'), 0)],
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n".'source,https://contao.org/,,0,,foobar'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n".$formattedDt.',source,https://contao.org/,,0,,foobar'."\n",
];

yield 'Correctly logs with no CrawlUri provided and a non-empty log file (should not write headlines)' => [
$dt,
['source' => 'source'],
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n".'source,---,---,---,---,foobar'."\n",
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n".$formattedDt.',source,---,---,---,---,foobar'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n",
];

yield 'Correctly logs with CrawlUri provided and a non-empty log file (should not write headlines)' => [
$dt,
['source' => 'source', 'crawlUri' => new CrawlUri(new Uri('https://contao.org'), 0)],
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n".'source,https://contao.org/,,0,,foobar'."\n",
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n".$formattedDt.',source,https://contao.org/,,0,,foobar'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n",
];

yield 'Correctly logs with new lines in message' => [
$dt,
['source' => 'source', 'crawlUri' => new CrawlUri(new Uri('https://contao.org'), 0)],
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n".'source,https://contao.org/,,0,,"foobar with new lines"'."\n",
'Source,URI,"Found on URI","Found on level",Tags,Message'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n".$formattedDt.',source,https://contao.org/,,0,,"foobar with new lines"'."\n",
'Time,Source,URI,"Found on URI","Found on level",Tags,Message'."\n",
"foobar\rwith\nnew\r\nlines",
];
}
Expand Down

0 comments on commit 448c0ca

Please sign in to comment.