Skip to content

Commit

Permalink
bug #34601 [MonologBridge] Fix debug processor datetime type (mRoca)
Browse files Browse the repository at this point in the history
This PR was merged into the 3.4 branch.

Discussion
----------

[MonologBridge] Fix debug processor datetime type

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | yes
| New feature?  | no
| Deprecations? | no
| Tickets       |n/a
| License       | MIT
| Doc PR        | n/a

This PR fixes an eventual `Call to a member function getTimestamp() on string` if any Processor transforms the `datetime` value, and uses the same record conditions as in the [ConsoleFormatter](https://github.com/symfony/monolog-bridge/blob/master/Formatter/ConsoleFormatter.php#L118 )

Commits
-------

ffe3f10 [MonologBridge] Fix debug processor datetime type
  • Loading branch information
nicolas-grekas committed Dec 10, 2019
2 parents 35b1328 + ffe3f10 commit 8aefe97
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Monolog/Processor/DebugProcessor.php
Expand Up @@ -22,7 +22,7 @@ class DebugProcessor implements DebugLoggerInterface
public function __invoke(array $record)
{
$this->records[] = [
'timestamp' => $record['datetime']->getTimestamp(),
'timestamp' => $record['datetime'] instanceof \DateTimeInterface ? $record['datetime']->getTimestamp() : strtotime($record['datetime']),
'message' => $record['message'],
'priority' => $record['level'],
'priorityName' => $record['level_name'],
Expand Down
61 changes: 61 additions & 0 deletions src/Symfony/Bridge/Monolog/Tests/Processor/DebugProcessorTest.php
@@ -0,0 +1,61 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Bridge\Monolog\Tests\Processor;

use Monolog\Logger;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Monolog\Processor\DebugProcessor;

class DebugProcessorTest extends TestCase
{
/**
* @dataProvider providerDatetimeFormatTests
*/
public function testDatetimeFormat(array $record, $expectedTimestamp)
{
$processor = new DebugProcessor();
$processor($record);

$records = $processor->getLogs();
self::assertCount(1, $records);
self::assertSame($expectedTimestamp, $records[0]['timestamp']);
}

/**
* @return array
*/
public function providerDatetimeFormatTests()
{
$record = $this->getRecord();

return [
[array_merge($record, ['datetime' => new \DateTime('2019-01-01T00:01:00+00:00')]), 1546300860],
[array_merge($record, ['datetime' => '2019-01-01T00:01:00+00:00']), 1546300860],
[array_merge($record, ['datetime' => 'foo']), false],
];
}

/**
* @return array
*/
private function getRecord()
{
return [
'message' => 'test',
'context' => [],
'level' => Logger::DEBUG,
'level_name' => Logger::getLevelName(Logger::DEBUG),
'channel' => 'test',
'datetime' => new \DateTime(),
];
}
}

0 comments on commit 8aefe97

Please sign in to comment.