-
Notifications
You must be signed in to change notification settings - Fork 118
Expand file tree
/
Copy pathRulesLog.php
More file actions
187 lines (164 loc) · 4.74 KB
/
RulesLog.php
File metadata and controls
187 lines (164 loc) · 4.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
<?php
/**
* @file
* Contains \Drupal\rules\RulesLog.
*/
namespace Drupal\rules\Engine;
use Drupal\Core\Logger\LoggerChannelInterface;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Routing\LinkGeneratorTrait;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\Url;
use Psr\Log\LogLevel;
/**
* The rules default logging class.
*/
class RulesLog implements RulesLogInterface {
use LinkGeneratorTrait;
use StringTranslationTrait;
protected $log = [];
protected $logLevel;
protected $line = 0;
/**
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
private $loggerChannel;
/**
* @var \Drupal\Core\Config\ConfigFactoryInterface
*/
private $config;
/**
* @param \Drupal\Core\Logger\LoggerChannelInterface $loggerChannel
* @param \Drupal\Core\Config\ConfigFactoryInterface $config
*/
public function __construct(LoggerChannelInterface $loggerChannel, ConfigFactoryInterface $config) {
$this->loggerChannel = $loggerChannel;
$this->config = $config;
$this->logLevel = $config->get('rules.config')->get('rules_log_level');
}
public function __clone() {
throw new \Exception("Cannot clone the logger.");
}
/**
* {@inheritdoc}
*/
public function log($msg, $args = [], $logLevel = LogLevel::INFO, $scope = NULL, $path = NULL) {
$config = $this->config->get('rules.config');
$rules_log_errors = $config->get('rules_log_errors');
$rules_debug_log = $config->get('rules_debug_log');
$rules_debug = $config->get('rules_debug');
if ($logLevel >= $rules_log_errors) {
$this->loggerChannel->log($logLevel, $msg, $args);
}
// Do nothing in case debugging is totally disabled.
if (!$rules_debug_log && !$rules_debug) {
return;
}
if ($logLevel >= $this->logLevel) {
$this->log[] = [$msg, $args, $logLevel, microtime(TRUE), $scope, $path];
}
}
/**
* {@inheritdoc}
*/
public function checkLog($logLevel = LogLevel::WARNING) {
foreach ($this->log as $entry) {
if ($entry[2] >= $logLevel) {
throw new \Exception($this->render());
}
}
}
/**
* {@inheritdoc}
*/
public function hasErrors($logLevel = LogLevel::WARNING) {
foreach ($this->log as $entry) {
if ($entry[2] >= $logLevel) {
return TRUE;
}
}
return FALSE;
}
/**
* {@inheritdoc}
*/
public function get() {
return $this->log;
}
/**
* {@inheritdoc}
*/
public function render() {
$line = 0;
$output = [];
while (isset($this->log[$line])) {
$vars['head'] = $this->t($this->log[$line][0], $this->log[$line][1]);
$vars['log'] = $this->renderHelper($line);
$output[] = [
'#theme' => 'rules_debug_element',
'#head' => $vars['head'],
'#log' => $vars['log'],
];
$line++;
}
return $output;
}
/**
* Renders the log of one event invocation.
*
* @param int $line
*
* @return array
*/
protected function renderHelper(&$line = 0) {
$startTime = isset($this->log[$line][3]) ? $this->log[$line][3] : 0;
$output = [];
while ($line < count($this->log)) {
if ($output && !empty($this->log[$line][4])) {
// The next entry stems from another evaluated set, add in its log
// messages here.
$vars['head'] = $this->t($this->log[$line][0], $this->log[$line][1]);
if (isset($this->log[$line][5])) {
$vars['link'] = '[' . $this->l('edit', Url::fromUri($this->log[$line][5])) . ']';
}
$vars['log'] = $this->renderHelper($line);
$output[] = [
'#theme' => 'rules_debug_element',
'#head' => $vars['head'],
'#link' => $vars['link'],
'#log' => $vars['log'],
];
}
else {
$formatted_diff = round(($this->log[$line][3] - $startTime) * 1000, 3) .' ms';
$msg = $formatted_diff .' '. $this->t($this->log[$line][0], $this->log[$line][1]);
if ($this->log[$line][2] >= LogLevel::WARNING) {
$level = $this->log[$line][2] == LogLevel::WARNING ? 'warn' : 'error';
$msg = '<span class="rules-debug-' . $level . '">'. $msg .'</span>';
}
if (isset($this->log[$line][5]) && !isset($this->log[$line][4])) {
$msg .= ' [' . $this->l('edit', Url::fromUri($this->log[$line][5])) . ']';
}
$output[] = $msg;
if (isset($this->log[$line][5]) && !$this->log[$line][4]) {
// This was the last log entry of this set.
return [
'#theme' => 'item_list',
'#items' => $output,
];
}
}
$line++;
}
return [
'#theme' => 'item_list',
'#items' => $output,
];
}
/**
* {@inheritdoc}
*/
public function clear() {
$this->log = [];
}
}