Skip to content

Commit 135c311

Browse files
author
Artyom Miroshnik
committed
Add rules logger channel and get rid of custom logger and debugger
1 parent d94d2a7 commit 135c311

File tree

12 files changed

+252
-196
lines changed

12 files changed

+252
-196
lines changed

config/install/rules.settings.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
log_errors: warning
2+
debug_log: false
3+
debug: false
4+
log_level: info

config/schema/rules.schema.yml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,4 +118,21 @@ rules.context.definition:
118118
label: 'Type'
119119
label:
120120
type: string
121-
label: 'Label'
121+
label: 'Label'
122+
123+
rules.settings:
124+
type: config_object
125+
label: 'Rules settings'
126+
mapping:
127+
log_errors:
128+
type: integer
129+
label: 'Logging of Rules evaluation errors'
130+
debug_log:
131+
type: boolean
132+
label: 'Log debug information to the available loggers'
133+
debug:
134+
type: boolean
135+
label: 'Show debug information'
136+
log_level:
137+
type: integer
138+
label: 'Log level'

rules.services.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,6 @@ services:
55
plugin.manager.rules_data_processor:
66
class: Drupal\rules\Context\DataProcessorManager
77
arguments: ['@container.namespaces', '@module_handler']
8+
logger.channel.rules:
9+
class: Drupal\rules\Logger\RulesLoggerChannel
10+
arguments: ['@config.factory']

src/Engine/RulesLog.php

Lines changed: 0 additions & 153 deletions
This file was deleted.

src/Logger/RulesLoggerChannel.php

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
<?php
2+
3+
/**
4+
* @file
5+
* Contains \Drupal\rules\Logger\RulesLoggerChannel.
6+
*/
7+
8+
namespace Drupal\rules\Logger;
9+
10+
use Drupal\Core\Config\ConfigFactoryInterface;
11+
use Drupal\Core\Config\ImmutableConfig;
12+
use Drupal\Core\Logger\LoggerChannel;
13+
use Psr\Log\LoggerTrait;
14+
15+
/**
16+
* Logs rules log entries in the available loggers.
17+
*/
18+
class RulesLoggerChannel extends LoggerChannel {
19+
use LoggerTrait;
20+
21+
/**
22+
* A configuration object with rules settings.
23+
*
24+
* @var ImmutableConfig
25+
*/
26+
protected $config;
27+
28+
/**
29+
* Static storage of log entries.
30+
*
31+
* @var array
32+
*/
33+
protected $logs = [];
34+
35+
/**
36+
* Creates RulesLoggerChannel object.
37+
*
38+
* @param ConfigFactoryInterface $config_factory
39+
* Config factory instance.
40+
*/
41+
public function __construct(ConfigFactoryInterface $config_factory) {
42+
parent::__construct('rules');
43+
$this->config = $config_factory->get('rules.settings');
44+
}
45+
46+
/**
47+
* {@inheritdoc}
48+
*/
49+
public function log($level, $message, array $context = []) {
50+
$this->logs[] = [
51+
'level' => $level,
52+
'message' => $message,
53+
'context' => $context,
54+
];
55+
56+
// Log message only if rules logging setting is enabled.
57+
if ($this->config->get('debug_log')) {
58+
if ($this->levelTranslation[$this->config->get('log_errors')] >= $this->levelTranslation[$level]) {
59+
parent::log($level, $message, $context);
60+
}
61+
}
62+
}
63+
64+
/**
65+
* Returns the structured array of entries.
66+
*
67+
* @return array
68+
* Array of stored log entries.
69+
*/
70+
public function getLogs() {
71+
return $this->logs;
72+
}
73+
74+
/**
75+
* Clears the static logs entries cache.
76+
*/
77+
public function clearLogs() {
78+
$this->logs = [];
79+
}
80+
81+
}

src/Tests/ConfigEntityTest.php

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
namespace Drupal\rules\Tests;
99

10-
use Drupal\rules\Engine\RulesLog;
11-
1210
/**
1311
* Tests storage and loading of Rules config entities.
1412
*
@@ -30,11 +28,6 @@ public function setUp() {
3028
parent::setUp();
3129

3230
$this->storage = $this->container->get('entity.manager')->getStorage('rules_component');
33-
34-
// Clear the log from any stale entries that are bleeding over from previous
35-
// tests.
36-
$logger = RulesLog::logger();
37-
$logger->clear();
3831
}
3932

4033
/**
@@ -72,8 +65,7 @@ public function testConfigAction() {
7265
$expression->execute();
7366

7467
// Test that the action logged something.
75-
$log = RulesLog::logger()->get();
76-
$this->assertEqual($log[0][0], 'action called');
68+
$this->assertRulesLogEntryExists('action called');
7769
}
7870

7971
/**
@@ -99,8 +91,7 @@ public function testConfigRule() {
9991
$expression->execute();
10092

10193
// Test that the action logged something.
102-
$log = RulesLog::logger()->get();
103-
$this->assertEqual($log[0][0], 'action called');
94+
$this->assertRulesLogEntryExists('action called');
10495
}
10596

10697
/**

src/Tests/NodeIntegrationTest.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,6 @@ class NodeIntegrationTest extends RulesDrupalTestBase {
3030
public function setUp() {
3131
parent::setUp();
3232

33-
// Clear the log from any stale entries that are bleeding over from previous
34-
// tests.
35-
$logger = RulesLog::logger();
36-
$logger->clear();
37-
3833
$this->installSchema('system', ['sequences']);
3934
$this->installEntitySchema('user');
4035
$this->installEntitySchema('node');
@@ -85,10 +80,6 @@ public function testNodeDataSelector() {
8580
$rule->addAction('rules_test_log');
8681
$rule->setContextValue('node', $node);
8782
$rule->execute();
88-
89-
// Test that the action logged something.
90-
$log = RulesLog::logger()->get();
91-
$this->assertEqual($log[0][0], 'action called');
9283
}
9384

9485
/**

src/Tests/RulesDrupalTestBase.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,14 @@ abstract class RulesDrupalTestBase extends KernelTestBase {
3535
*/
3636
protected $typedDataManager;
3737

38+
39+
/**
40+
* Rules logger.
41+
*
42+
* @var \Drupal\rules\Logger\RulesLoggerChannel
43+
*/
44+
protected $logger;
45+
3846
/**
3947
* Modules to enable.
4048
*
@@ -47,6 +55,12 @@ abstract class RulesDrupalTestBase extends KernelTestBase {
4755
*/
4856
public function setUp() {
4957
parent::setUp();
58+
59+
$this->logger = $this->container->get('logger.channel.rules');
60+
// Clear the log from any stale entries that are bleeding over from previous
61+
// tests.
62+
$this->logger->clearLogs();
63+
5064
$this->expressionManager = $this->container->get('plugin.manager.rules_expression');
5165
$this->conditionManager = $this->container->get('plugin.manager.condition');
5266
$this->typedDataManager = $this->container->get('typed_data_manager');
@@ -68,4 +82,18 @@ protected function createCondition($id) {
6882
return $condition;
6983
}
7084

85+
/**
86+
* Checks if particular message is in the log with given delta.
87+
*
88+
* @param string $message
89+
* Log message.
90+
* @param int $log_item_index
91+
* Log item's index in log entries stack.
92+
*/
93+
protected function assertRulesLogEntryExists($message, $log_item_index = 0) {
94+
// Test that the action has logged something.
95+
$logs = $this->logger->getLogs();
96+
$this->assertEqual($logs[$log_item_index]['message'], $message);
97+
}
98+
7199
}

0 commit comments

Comments
 (0)