ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
RavenHandler.php
Go to the documentation of this file.
1 <?php
2 
3 /*
4  * This file is part of the Monolog package.
5  *
6  * (c) Jordi Boggiano <j.boggiano@seld.be>
7  *
8  * For the full copyright and license information, please view the LICENSE
9  * file that was distributed with this source code.
10  */
11 
12 namespace Monolog\Handler;
13 
16 use Monolog\Logger;
17 use Raven_Client;
18 
26 {
30  protected $logLevels = array(
32  Logger::INFO => Raven_Client::INFO,
33  Logger::NOTICE => Raven_Client::INFO,
34  Logger::WARNING => Raven_Client::WARNING,
35  Logger::ERROR => Raven_Client::ERROR,
36  Logger::CRITICAL => Raven_Client::FATAL,
37  Logger::ALERT => Raven_Client::FATAL,
38  Logger::EMERGENCY => Raven_Client::FATAL,
39  );
40 
45  protected $release;
46 
50  protected $ravenClient;
51 
55  protected $batchFormatter;
56 
63  {
64  parent::__construct($level, $bubble);
65 
66  $this->ravenClient = $ravenClient;
67  }
68 
72  public function handleBatch(array $records)
73  {
75 
76  // filter records based on their level
77  $records = array_filter($records, function ($record) use ($level) {
78  return $record['level'] >= $level;
79  });
80 
81  if (!$records) {
82  return;
83  }
84 
85  // the record with the highest severity is the "main" one
86  $record = array_reduce($records, function ($highest, $record) {
87  if ($record['level'] > $highest['level']) {
88  return $record;
89  }
90 
91  return $highest;
92  });
93 
94  // the other ones are added as a context item
95  $logs = array();
96  foreach ($records as $r) {
97  $logs[] = $this->processRecord($r);
98  }
99 
100  if ($logs) {
101  $record['context']['logs'] = (string) $this->getBatchFormatter()->formatBatch($logs);
102  }
103 
104  $this->handle($record);
105  }
106 
113  {
114  $this->batchFormatter = $formatter;
115  }
116 
122  public function getBatchFormatter()
123  {
124  if (!$this->batchFormatter) {
125  $this->batchFormatter = $this->getDefaultBatchFormatter();
126  }
127 
128  return $this->batchFormatter;
129  }
130 
134  protected function write(array $record)
135  {
136  $previousUserContext = false;
137  $options = array();
138  $options['level'] = $this->logLevels[$record['level']];
139  $options['tags'] = array();
140  if (!empty($record['extra']['tags'])) {
141  $options['tags'] = array_merge($options['tags'], $record['extra']['tags']);
142  unset($record['extra']['tags']);
143  }
144  if (!empty($record['context']['tags'])) {
145  $options['tags'] = array_merge($options['tags'], $record['context']['tags']);
146  unset($record['context']['tags']);
147  }
148  if (!empty($record['context']['fingerprint'])) {
149  $options['fingerprint'] = $record['context']['fingerprint'];
150  unset($record['context']['fingerprint']);
151  }
152  if (!empty($record['context']['logger'])) {
153  $options['logger'] = $record['context']['logger'];
154  unset($record['context']['logger']);
155  } else {
156  $options['logger'] = $record['channel'];
157  }
158  foreach ($this->getExtraParameters() as $key) {
159  foreach (array('extra', 'context') as $source) {
160  if (!empty($record[$source][$key])) {
161  $options[$key] = $record[$source][$key];
162  unset($record[$source][$key]);
163  }
164  }
165  }
166  if (!empty($record['context'])) {
167  $options['extra']['context'] = $record['context'];
168  if (!empty($record['context']['user'])) {
169  $previousUserContext = $this->ravenClient->context->user;
170  $this->ravenClient->user_context($record['context']['user']);
171  unset($options['extra']['context']['user']);
172  }
173  }
174  if (!empty($record['extra'])) {
175  $options['extra']['extra'] = $record['extra'];
176  }
177 
178  if (!empty($this->release) && !isset($options['release'])) {
179  $options['release'] = $this->release;
180  }
181 
182  if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) {
183  $options['message'] = $record['formatted'];
184  $this->ravenClient->captureException($record['context']['exception'], $options);
185  } else {
186  $this->ravenClient->captureMessage($record['formatted'], array(), $options);
187  }
188 
189  if ($previousUserContext !== false) {
190  $this->ravenClient->user_context($previousUserContext);
191  }
192  }
193 
197  protected function getDefaultFormatter()
198  {
199  return new LineFormatter('[%channel%] %message%');
200  }
201 
207  protected function getDefaultBatchFormatter()
208  {
209  return new LineFormatter();
210  }
211 
217  protected function getExtraParameters()
218  {
219  return array('contexts', 'checksum', 'release', 'event_id');
220  }
221 
226  public function setRelease($value)
227  {
228  $this->release = $value;
229 
230  return $this;
231  }
232 }
const NOTICE
Uncommon events.
Definition: Logger.php:45
const DEBUG
Detailed debug information.
Definition: Logger.php:33
const ERROR
Runtime errors.
Definition: Logger.php:58
getExtraParameters()
Gets extra parameters supported by Raven that can be found in "extra" and "context".
__construct(Raven_Client $ravenClient, $level=Logger::DEBUG, $bubble=true)
getDefaultBatchFormatter()
Gets the default formatter for the logs generated by handleBatch().
Base Handler class providing the Handler structure.
$records
Definition: simple_test.php:22
$logLevels
Translates Monolog log levels to Raven log levels.
setBatchFormatter(FormatterInterface $formatter)
Sets the formatter for the logs generated by handleBatch().
handleBatch(array $records)
{Handles a set of records at once.The records to handle (an array of record arrays)} ...
const DEBUG
$r
Definition: example_031.php:79
handle(array $record)
{Handles a record.All records may be passed to this method, and the handler should discard those that...
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:53
const EMERGENCY
Urgent alert.
Definition: Logger.php:78
const CRITICAL
Critical conditions.
Definition: Logger.php:65
processRecord(array $record)
Processes a record.
const ALERT
Action must be taken immediately.
Definition: Logger.php:73
getBatchFormatter()
Gets the formatter for the logs generated by handleBatch().
Handler to send messages to a Sentry (https://github.com/getsentry/sentry) server using sentry-php (h...
$source
Definition: linkback.php:22
Formats incoming records into a one-line string.
$key
Definition: croninfo.php:18
const INFO
Interesting events.
Definition: Logger.php:40