Skip to content

Commit a74ec29

Browse files
oshmyheliukshiftedreality
authored andcommitted
MAGECLOUD-3871: Write cloud.log in debug level to file (#589)
1 parent 8d60169 commit a74ec29

File tree

5 files changed

+59
-27
lines changed

5 files changed

+59
-27
lines changed

dist/.magento.env.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,7 @@
474474
# MIN_LOGGING_LEVEL - use to override the minimum logging level for all output streams without making changes #
475475
# to the code. This helps to improve troubleshooting problems with deployment. For example, if #
476476
# your deployment fails, you can use this variable to increase the logging granularity globally. #
477+
# Doesn't affect log level for the file output. #
477478
# See Set up notifications—Log levels. #
478479
# The min_level value in Logging handlers overwrites this setting. #
479480
# Magento Version: Magento 2.1.4 and later #

src/App/Logger/HandlerFactory.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
use Monolog\Handler\StreamHandler;
1515
use Monolog\Handler\SyslogHandler;
1616
use Monolog\Handler\SyslogUdpHandler;
17-
use Monolog\Logger;
1817

1918
/**
2019
* The handler factory.
@@ -81,8 +80,13 @@ public function create(string $handler): HandlerInterface
8180
$minLevel = $this->levelResolver->resolve($configuration->get('min_level', $defaultLevel));
8281

8382
switch ($handler) {
84-
case static::HANDLER_STREAM:
8583
case static::HANDLER_FILE:
84+
$handlerInstance = new StreamHandler(
85+
$configuration->get('file'),
86+
$this->levelResolver->resolve($configuration->get('min_level', LogConfig::LEVEL_DEBUG))
87+
);
88+
break;
89+
case static::HANDLER_STREAM:
8690
$defaultLevelStream = $levelOverride ?: LogConfig::LEVEL_INFO;
8791
$handlerInstance = new StreamHandler(
8892
$configuration->get('stream'),

src/Config/Log.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ private function getConfig(): array
9696
$this->config = array_replace_recursive(
9797
[
9898
HandlerFactory::HANDLER_STREAM => ['stream' => 'php://stdout'],
99-
HandlerFactory::HANDLER_FILE => ['stream' => $this->fileList->getCloudLog()],
99+
HandlerFactory::HANDLER_FILE => ['file' => $this->fileList->getCloudLog()],
100100
],
101101
$this->reader->read()[static::SECTION_CONFIG] ?? []
102102
);

src/Test/Unit/App/Logger/HandlerFactoryTest.php

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Magento\MagentoCloud\App\Logger\LevelResolver;
1313
use Magento\MagentoCloud\Config\GlobalSection;
1414
use Magento\MagentoCloud\Config\Log as LogConfig;
15+
use Monolog\Handler\AbstractHandler;
1516
use Monolog\Handler\HandlerInterface;
1617
use Monolog\Handler\NativeMailerHandler;
1718
use Monolog\Handler\SlackHandler;
@@ -127,22 +128,24 @@ public function testCreateGelfHandler()
127128
}
128129

129130
/**
130-
* @param string $handler
131-
* @param int $repositoryMockGetExpects
131+
* @param string $handlerName
132132
* @param array $repositoryMockReturnMap
133+
* @param $minLevelOverride
133134
* @param string $expectedClass
135+
* @param int $expectedLevel
136+
* @throws \Exception
134137
* @dataProvider createDataProvider
135138
*/
136139
public function testCreate(
137-
string $handler,
140+
string $handlerName,
138141
array $repositoryMockReturnMap,
139142
$minLevelOverride,
140143
string $expectedClass,
141144
int $expectedLevel
142145
) {
143146
$this->logConfigMock->expects($this->once())
144147
->method('get')
145-
->with($handler)
148+
->with($handlerName)
146149
->willReturn($this->repositoryMock);
147150
$this->repositoryMock->method('get')
148151
->willReturnMap($repositoryMockReturnMap);
@@ -156,13 +159,15 @@ public function testCreate(
156159
[LogConfig::LEVEL_NOTICE, Logger::NOTICE],
157160
[LogConfig::LEVEL_INFO, Logger::INFO],
158161
[LogConfig::LEVEL_WARNING, Logger::WARNING],
162+
[LogConfig::LEVEL_DEBUG, Logger::DEBUG],
159163
]);
160164

161-
$handlerInstance = $this->handlerFactory->create($handler);
165+
/** @var AbstractHandler $handler */
166+
$handler = $this->handlerFactory->create($handlerName);
162167

163-
$this->assertInstanceOf(HandlerInterface::class, $handlerInstance);
164-
$this->assertInstanceOf($expectedClass, $handlerInstance);
165-
$this->assertSame($expectedLevel, $handlerInstance->getLevel());
168+
$this->assertInstanceOf(HandlerInterface::class, $handler);
169+
$this->assertInstanceOf($expectedClass, $handler);
170+
$this->assertSame($expectedLevel, $handler->getLevel());
166171
}
167172

168173
/**
@@ -172,7 +177,7 @@ public function testCreate(
172177
public function createDataProvider()
173178
{
174179
return [
175-
[
180+
'stream handler' => [
176181
'handler' => HandlerFactory::HANDLER_STREAM,
177182
'repositoryMockReturnMap' => [
178183
['stream', null, 'php://stdout'],
@@ -183,7 +188,7 @@ public function createDataProvider()
183188
'expectedClass' => StreamHandler::class,
184189
'expectedLevel' => Logger::INFO,
185190
],
186-
[
191+
'stream handler 2' => [
187192
'handler' => HandlerFactory::HANDLER_STREAM,
188193
'repositoryMockReturnMap' => [
189194
['stream', null, 'php://stdout'],
@@ -193,18 +198,40 @@ public function createDataProvider()
193198
'expectedClass' => StreamHandler::class,
194199
'expectedLevel' => Logger::WARNING,
195200
],
196-
[
201+
'file handler default' => [
197202
'handler' => HandlerFactory::HANDLER_FILE,
198203
'repositoryMockReturnMap' => [
199-
['stream', null, 'var/log/cloud.log'],
200-
['min_level', LogConfig::LEVEL_NOTICE, LogConfig::LEVEL_NOTICE],
201-
['min_level', LogConfig::LEVEL_INFO, LogConfig::LEVEL_INFO],
204+
['file', null, 'var/log/cloud.log'],
205+
['min_level', LogConfig::LEVEL_NOTICE, LogConfig::LEVEL_DEBUG],
206+
['min_level', LogConfig::LEVEL_DEBUG, LogConfig::LEVEL_DEBUG],
207+
],
208+
'minLevelOverride' => '',
209+
'expectedClass' => StreamHandler::class,
210+
'expectedLevel' => Logger::DEBUG,
211+
],
212+
'file handler min_level overwritten' => [
213+
'handler' => HandlerFactory::HANDLER_FILE,
214+
'repositoryMockReturnMap' => [
215+
['file', null, 'var/log/cloud.log'],
216+
['min_level', LogConfig::LEVEL_NOTICE, LogConfig::LEVEL_INFO],
217+
['min_level', LogConfig::LEVEL_DEBUG, LogConfig::LEVEL_INFO],
202218
],
203219
'minLevelOverride' => '',
204220
'expectedClass' => StreamHandler::class,
205221
'expectedLevel' => Logger::INFO,
206222
],
207-
[
223+
'file handler MIN_LOGGING_LEVEL overwritten' => [
224+
'handler' => HandlerFactory::HANDLER_FILE,
225+
'repositoryMockReturnMap' => [
226+
['file', null, 'var/log/cloud.log'],
227+
['min_level', LogConfig::LEVEL_INFO, LogConfig::LEVEL_DEBUG],
228+
['min_level', LogConfig::LEVEL_DEBUG, LogConfig::LEVEL_DEBUG],
229+
],
230+
'minLevelOverride' => LogConfig::LEVEL_INFO,
231+
'expectedClass' => StreamHandler::class,
232+
'expectedLevel' => Logger::DEBUG,
233+
],
234+
'slack handler' => [
208235
'handler' => HandlerFactory::HANDLER_SLACK,
209236
'repositoryMockReturnMap' => [
210237
['token', null, 'someToken'],
@@ -217,7 +244,7 @@ public function createDataProvider()
217244
'expectedClass' => SlackHandler::class,
218245
'expectedLevel' => Logger::NOTICE,
219246
],
220-
[
247+
'slack handler 2' =>[
221248
'handler' => HandlerFactory::HANDLER_SLACK,
222249
'repositoryMockReturnMap' => [
223250
['token', null, 'someToken'],
@@ -229,7 +256,7 @@ public function createDataProvider()
229256
'expectedClass' => SlackHandler::class,
230257
'expectedLevel' => Logger::WARNING,
231258
],
232-
[
259+
'email handler' => [
233260
'handler' => HandlerFactory::HANDLER_EMAIL,
234261
'repositoryMockReturnMap' => [
235262
['to', null, 'user@example.com'],
@@ -242,7 +269,7 @@ public function createDataProvider()
242269
'expectedClass' => NativeMailerHandler::class,
243270
'expectedLevel' => Logger::NOTICE,
244271
],
245-
[
272+
'syslog handler' => [
246273
'handler' => HandlerFactory::HANDLER_SYSLOG,
247274
'repositoryMockReturnMap' => [
248275
['ident', null, 'user@example.com'],
@@ -256,7 +283,7 @@ public function createDataProvider()
256283
'expectedClass' => SyslogHandler::class,
257284
'expectedLevel' => Logger::NOTICE,
258285
],
259-
[
286+
'syslog udp handler' => [
260287
'handler' => HandlerFactory::HANDLER_SYSLOG_UDP,
261288
'repositoryMockReturnMap' => [
262289
['host', null, '127.0.0.1'],

src/Test/Unit/Config/LogTest.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -92,36 +92,36 @@ public function getHandlersDataProvider()
9292
'config' => [],
9393
'expectedResult' => [
9494
HandlerFactory::HANDLER_STREAM => ['stream' => 'php://stdout'],
95-
HandlerFactory::HANDLER_FILE => ['stream' => 'somePath']
95+
HandlerFactory::HANDLER_FILE => ['file' => 'somePath']
9696
]
9797
],
9898
[
9999
'config' => ['someConfig' => ['someConfig']],
100100
'expectedResult' => [
101101
HandlerFactory::HANDLER_STREAM => ['stream' => 'php://stdout'],
102-
HandlerFactory::HANDLER_FILE => ['stream' => 'somePath']
102+
HandlerFactory::HANDLER_FILE => ['file' => 'somePath']
103103
],
104104
],
105105
[
106106
'config' => ['log' => []],
107107
'expectedResult' => [
108108
HandlerFactory::HANDLER_STREAM => ['stream' => 'php://stdout'],
109-
HandlerFactory::HANDLER_FILE => ['stream' => 'somePath']
109+
HandlerFactory::HANDLER_FILE => ['file' => 'somePath']
110110
],
111111
],
112112
[
113113
'config' => ['log' => ['SomeHandler' => ['SomeConfig']]],
114114
'expectedResult' => [
115115
HandlerFactory::HANDLER_STREAM => ['stream' => 'php://stdout'],
116-
HandlerFactory::HANDLER_FILE => ['stream' => 'somePath'],
116+
HandlerFactory::HANDLER_FILE => ['file' => 'somePath'],
117117
'SomeHandler' => ['SomeConfig']
118118
],
119119
],
120120
[
121121
'config' => ['log' => ['SomeHandler' => ['SomeConfig']], 'someConfig' => ['someConfig']],
122122
'expectedResult' => [
123123
HandlerFactory::HANDLER_STREAM => ['stream' => 'php://stdout'],
124-
HandlerFactory::HANDLER_FILE => ['stream' => 'somePath'],
124+
HandlerFactory::HANDLER_FILE => ['file' => 'somePath'],
125125
'SomeHandler' => ['SomeConfig']
126126
],
127127
],

0 commit comments

Comments
 (0)