Skip to content

Commit 9c59211

Browse files
author
Bohdan Korablov
committed
Merge remote-tracking branch 'falcons/MAGETWO-55429' into MAGETWO-69983
2 parents 1aaa2fc + d65bbce commit 9c59211

File tree

4 files changed

+151
-9
lines changed

4 files changed

+151
-9
lines changed

lib/internal/Magento/Framework/Logger/Handler/System.php

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,20 +41,21 @@ public function __construct(
4141
}
4242

4343
/**
44-
* @{inheritDoc}
44+
* Writes formatted record through the handler.
4545
*
46-
* @param $record array
46+
* @param $record array The record metadata
4747
* @return void
4848
*/
4949
public function write(array $record)
5050
{
51-
if (isset($record['context']['is_exception']) && $record['context']['is_exception']) {
52-
unset($record['context']['is_exception']);
51+
if (isset($record['context']['exception'])) {
5352
$this->exceptionHandler->handle($record);
54-
} else {
55-
unset($record['context']['is_exception']);
56-
$record['formatted'] = $this->getFormatter()->format($record);
57-
parent::write($record);
53+
54+
return;
5855
}
56+
57+
$record['formatted'] = $this->getFormatter()->format($record);
58+
59+
parent::write($record);
5960
}
6061
}

lib/internal/Magento/Framework/Logger/Monolog.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,18 @@ public function __construct($name, array $handlers = [], array $processors = [])
3333
*/
3434
public function addRecord($level, $message, array $context = [])
3535
{
36-
$context['is_exception'] = $message instanceof \Exception;
36+
/**
37+
* To preserve compatibility with Exception messages.
38+
* And support PSR-3 context standard.
39+
*
40+
* @link http://www.php-fig.org/psr/psr-3/#context PSR-3 context standard
41+
*/
42+
if ($message instanceof \Exception && !isset($context['exception'])) {
43+
$context['exception'] = $message;
44+
}
45+
46+
$message = $message instanceof \Exception ? $message->getMessage() : $message;
47+
3748
return parent::addRecord($level, $message, $context);
3849
}
3950
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Logger\Test\Unit\Handler;
7+
8+
use Magento\Framework\Filesystem\DriverInterface;
9+
use Magento\Framework\Logger\Handler\Exception;
10+
use Magento\Framework\Logger\Handler\System;
11+
use Monolog\Logger;
12+
use PHPUnit_Framework_MockObject_MockObject as Mock;
13+
14+
class SystemTest extends \PHPUnit_Framework_TestCase
15+
{
16+
/**
17+
* @var System
18+
*/
19+
private $model;
20+
21+
/**
22+
* @var DriverInterface|Mock
23+
*/
24+
private $filesystemMock;
25+
26+
/**
27+
* @var Exception|Mock
28+
*/
29+
private $exceptionHandlerMock;
30+
31+
/**
32+
* @inheritdoc
33+
*/
34+
protected function setUp()
35+
{
36+
$this->filesystemMock = $this->getMockBuilder(DriverInterface::class)
37+
->getMockForAbstractClass();
38+
$this->exceptionHandlerMock = $this->getMockBuilder(Exception::class)
39+
->disableOriginalConstructor()
40+
->getMock();
41+
42+
$this->model = new System(
43+
$this->filesystemMock,
44+
$this->exceptionHandlerMock
45+
);
46+
}
47+
48+
public function testWrite()
49+
{
50+
$this->filesystemMock->expects($this->once())
51+
->method('getParentDirectory');
52+
$this->filesystemMock->expects($this->once())
53+
->method('isDirectory')
54+
->willReturn('true');
55+
56+
$this->model->write($this->getRecord());
57+
}
58+
59+
public function testWriteException()
60+
{
61+
$record = $this->getRecord();
62+
$record['context']['exception'] = new \Exception('Some exception');
63+
64+
$this->exceptionHandlerMock->expects($this->once())
65+
->method('handle')
66+
->with($record);
67+
$this->filesystemMock->expects($this->never())
68+
->method('getParentDirectory');
69+
70+
$this->model->write($record);
71+
}
72+
73+
/**
74+
* @param int $level
75+
* @param string $message
76+
* @param array $context
77+
* @return array
78+
*/
79+
private function getRecord($level = Logger::WARNING, $message = 'test', $context = [])
80+
{
81+
return [
82+
'message' => $message,
83+
'context' => $context,
84+
'level' => $level,
85+
'level_name' => Logger::getLevelName($level),
86+
'channel' => 'test',
87+
'datetime' => \DateTime::createFromFormat('U.u', sprintf('%.6F', microtime(true))),
88+
'extra' => [],
89+
];
90+
}
91+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
namespace Magento\Framework\Logger\Test\Unit;
7+
8+
use Magento\Framework\Logger\Monolog;
9+
use Monolog\Handler\TestHandler;
10+
11+
class MonologTest extends \PHPUnit_Framework_TestCase
12+
{
13+
public function testAddRecord()
14+
{
15+
$logger = new Monolog(__METHOD__);
16+
$handler = new TestHandler();
17+
18+
$logger->pushHandler($handler);
19+
20+
$logger->addError('test');
21+
list($record) = $handler->getRecords();
22+
23+
$this->assertSame('test', $record['message']);
24+
}
25+
26+
public function testAddRecordAsException()
27+
{
28+
$logger = new Monolog(__METHOD__);
29+
$handler = new TestHandler();
30+
31+
$logger->pushHandler($handler);
32+
33+
$logger->addError(new \Exception('Some exception'));
34+
list($record) = $handler->getRecords();
35+
36+
$this->assertInstanceOf(\Exception::class, $record['context']['exception']);
37+
$this->assertSame('Some exception', $record['message']);
38+
}
39+
}

0 commit comments

Comments
 (0)