Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffe3f10

Browse files
committed
[MonologBridge] Fix debug processor datetime type
1 parent 3bd5fae commit ffe3f10

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

src/Symfony/Bridge/Monolog/Processor/DebugProcessor.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class DebugProcessor implements DebugLoggerInterface
2222
public function __invoke(array $record)
2323
{
2424
$this->records[] = [
25-
'timestamp' => $record['datetime']->getTimestamp(),
25+
'timestamp' => $record['datetime'] instanceof \DateTimeInterface ? $record['datetime']->getTimestamp() : strtotime($record['datetime']),
2626
'message' => $record['message'],
2727
'priority' => $record['level'],
2828
'priorityName' => $record['level_name'],
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bridge\Monolog\Tests\Processor;
13+
14+
use Monolog\Logger;
15+
use PHPUnit\Framework\TestCase;
16+
use Symfony\Bridge\Monolog\Processor\DebugProcessor;
17+
18+
class DebugProcessorTest extends TestCase
19+
{
20+
/**
21+
* @dataProvider providerDatetimeFormatTests
22+
*/
23+
public function testDatetimeFormat(array $record, $expectedTimestamp)
24+
{
25+
$processor = new DebugProcessor();
26+
$processor($record);
27+
28+
$records = $processor->getLogs();
29+
self::assertCount(1, $records);
30+
self::assertSame($expectedTimestamp, $records[0]['timestamp']);
31+
}
32+
33+
/**
34+
* @return array
35+
*/
36+
public function providerDatetimeFormatTests()
37+
{
38+
$record = $this->getRecord();
39+
40+
return [
41+
[array_merge($record, ['datetime' => new \DateTime('2019-01-01T00:01:00+00:00')]), 1546300860],
42+
[array_merge($record, ['datetime' => '2019-01-01T00:01:00+00:00']), 1546300860],
43+
[array_merge($record, ['datetime' => 'foo']), false],
44+
];
45+
}
46+
47+
/**
48+
* @return array
49+
*/
50+
private function getRecord()
51+
{
52+
return [
53+
'message' => 'test',
54+
'context' => [],
55+
'level' => Logger::DEBUG,
56+
'level_name' => Logger::getLevelName(Logger::DEBUG),
57+
'channel' => 'test',
58+
'datetime' => new \DateTime(),
59+
];
60+
}
61+
}

0 commit comments

Comments
 (0)