ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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  private $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 
44  protected $ravenClient;
45 
49  protected $batchFormatter;
50 
57  {
58  parent::__construct($level, $bubble);
59 
60  $this->ravenClient = $ravenClient;
61  }
62 
66  public function handleBatch(array $records)
67  {
69 
70  // filter records based on their level
71  $records = array_filter($records, function ($record) use ($level) {
72  return $record['level'] >= $level;
73  });
74 
75  if (!$records) {
76  return;
77  }
78 
79  // the record with the highest severity is the "main" one
80  $record = array_reduce($records, function ($highest, $record) {
81  if ($record['level'] >= $highest['level']) {
82  return $record;
83  }
84 
85  return $highest;
86  });
87 
88  // the other ones are added as a context item
89  $logs = array();
90  foreach ($records as $r) {
91  $logs[] = $this->processRecord($r);
92  }
93 
94  if ($logs) {
95  $record['context']['logs'] = (string) $this->getBatchFormatter()->formatBatch($logs);
96  }
97 
98  $this->handle($record);
99  }
100 
107  {
108  $this->batchFormatter = $formatter;
109  }
110 
116  public function getBatchFormatter()
117  {
118  if (!$this->batchFormatter) {
119  $this->batchFormatter = $this->getDefaultBatchFormatter();
120  }
121 
122  return $this->batchFormatter;
123  }
124 
128  protected function write(array $record)
129  {
130  $previousUserContext = false;
131  $options = array();
132  $options['level'] = $this->logLevels[$record['level']];
133  $options['tags'] = array();
134  if (!empty($record['extra']['tags'])) {
135  $options['tags'] = array_merge($options['tags'], $record['extra']['tags']);
136  unset($record['extra']['tags']);
137  }
138  if (!empty($record['context']['tags'])) {
139  $options['tags'] = array_merge($options['tags'], $record['context']['tags']);
140  unset($record['context']['tags']);
141  }
142  if (!empty($record['context']['logger'])) {
143  $options['logger'] = $record['context']['logger'];
144  unset($record['context']['logger']);
145  } else {
146  $options['logger'] = $record['channel'];
147  }
148  if (!empty($record['context'])) {
149  $options['extra']['context'] = $record['context'];
150  if (!empty($record['context']['user'])) {
151  $previousUserContext = $this->ravenClient->context->user;
152  $this->ravenClient->user_context($record['context']['user']);
153  unset($options['extra']['context']['user']);
154  }
155  }
156  if (!empty($record['extra'])) {
157  $options['extra']['extra'] = $record['extra'];
158  }
159 
160  if (isset($record['context']['exception']) && $record['context']['exception'] instanceof \Exception) {
161  $options['extra']['message'] = $record['formatted'];
162  $this->ravenClient->captureException($record['context']['exception'], $options);
163  } else {
164  $this->ravenClient->captureMessage($record['formatted'], array(), $options);
165  }
166 
167  if ($previousUserContext !== false) {
168  $this->ravenClient->user_context($previousUserContext);
169  }
170 
171  }
172 
176  protected function getDefaultFormatter()
177  {
178  return new LineFormatter('[%channel%] %message%');
179  }
180 
186  protected function getDefaultBatchFormatter()
187  {
188  return new LineFormatter();
189  }
190 }
const NOTICE
Uncommon events.
Definition: Logger.php:44
const DEBUG
Detailed debug information.
Definition: Logger.php:32
const ERROR
Runtime errors.
Definition: Logger.php:57
__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:17
$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
if(!is_array($argv)) $options
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:52
const EMERGENCY
Urgent alert.
Definition: Logger.php:77
const CRITICAL
Critical conditions.
Definition: Logger.php:64
processRecord(array $record)
Processes a record.
const ALERT
Action must be taken immediately.
Definition: Logger.php:72
getBatchFormatter()
Gets the formatter for the logs generated by handleBatch().
Handler to send messages to a Sentry (https://github.com/getsentry/sentry) server using raven-php (ht...
Formats incoming records into a one-line string.
const INFO
Interesting events.
Definition: Logger.php:39