Skip to content

Commit 5245bc6

Browse files
committed
ACP2E-1287: log exception to exception.log
1 parent a99d718 commit 5245bc6

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

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

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ private function getLogger(): LoggerInterface
9999
*/
100100
public function emergency($message, array $context = [])
101101
{
102+
$context = $this->addExceptionToContext($message, $context);
102103
$this->getLogger()->emergency($message, $context);
103104
}
104105

@@ -107,6 +108,7 @@ public function emergency($message, array $context = [])
107108
*/
108109
public function alert($message, array $context = [])
109110
{
111+
$context = $this->addExceptionToContext($message, $context);
110112
$this->getLogger()->alert($message, $context);
111113
}
112114

@@ -115,6 +117,7 @@ public function alert($message, array $context = [])
115117
*/
116118
public function critical($message, array $context = [])
117119
{
120+
$context = $this->addExceptionToContext($message, $context);
118121
$this->getLogger()->critical($message, $context);
119122
}
120123

@@ -165,4 +168,19 @@ public function log($level, $message, array $context = [])
165168
{
166169
$this->getLogger()->log($level, $message, $context);
167170
}
171+
172+
/**
173+
* Ensure exception logging by adding it to context
174+
*
175+
* @param $message
176+
* @param array $context
177+
* @return array
178+
*/
179+
protected function addExceptionToContext($message, array $context = []): array
180+
{
181+
if ($message instanceof \Throwable) {
182+
$context['exception'] = $message;
183+
}
184+
return $context;
185+
}
168186
}

lib/internal/Magento/Framework/Logger/Test/Unit/LoggerProxyTest.php

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,4 +99,39 @@ public function createWithArguments(): void
9999
$loggerProxy = new LoggerProxy($objectManager);
100100
$loggerProxy->log(LogLevel::ALERT, 'test');
101101
}
102+
103+
/**
104+
* @test
105+
*
106+
* @param $method
107+
*
108+
* @return void
109+
* @dataProvider methodsList
110+
*/
111+
public function logException($method): void
112+
{
113+
$deploymentConfig = $this->getMockBuilder(DeploymentConfig::class)
114+
->disableOriginalConstructor()
115+
->getMock();
116+
$objectManager = $this->getMockBuilder(ObjectManagerInterface::class)
117+
->getMockForAbstractClass();
118+
119+
$logger = $this->getMockBuilder(LoggerInterface::class)
120+
->getMockForAbstractClass();
121+
122+
$objectManager
123+
->method('get')
124+
->withConsecutive([DeploymentConfig::class], [Monolog::class])
125+
->willReturnOnConsecutiveCalls($deploymentConfig, $logger);
126+
127+
if (in_array($method, ['critical', 'emergency', 'alert'])) {
128+
$message = new \Exception('This is an exception.');
129+
} else {
130+
$message = 'test';
131+
}
132+
$logger->expects($this->once())->method($method)->with($message);
133+
134+
$loggerProxy = new LoggerProxy($objectManager);
135+
$loggerProxy->$method($message);
136+
}
102137
}

0 commit comments

Comments
 (0)