4 * This file is part of the Monolog package.
6 * (c) Jordi Boggiano <j.boggiano@seld.be>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Monolog\Handler;
14 use Monolog\Formatter\LineFormatter;
15 use Monolog\Formatter\FormatterInterface;
20 * Handler to send messages to a Sentry (https://github.com/getsentry/sentry) server
21 * using raven-php (https://github.com/getsentry/raven-php)
23 * @author Marc Abramowitz <marc@marc-abramowitz.com>
25 class RavenHandler extends AbstractProcessingHandler
28 * Translates Monolog log levels to Raven log levels.
30 private $logLevels = array(
31 Logger::DEBUG => Raven_Client::DEBUG,
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,
42 * @var string should represent the current version of the calling
43 * software. Can be any string (git commit, version number)
48 * @var Raven_Client the client object that sends the message to the server
50 protected $ravenClient;
53 * @var LineFormatter The formatter to use for the logs generated via handleBatch()
55 protected $batchFormatter;
58 * @param Raven_Client $ravenClient
59 * @param int $level The minimum logging level at which this handler will be triggered
60 * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
62 public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true)
64 parent::__construct($level, $bubble);
66 $this->ravenClient = $ravenClient;
72 public function handleBatch(array $records)
74 $level = $this->level;
76 // filter records based on their level
77 $records = array_filter($records, function ($record) use ($level) {
78 return $record['level'] >= $level;
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']) {
94 // the other ones are added as a context item
96 foreach ($records as $r) {
97 $logs[] = $this->processRecord($r);
101 $record['context']['logs'] = (string) $this->getBatchFormatter()->formatBatch($logs);
104 $this->handle($record);
108 * Sets the formatter for the logs generated by handleBatch().
110 * @param FormatterInterface $formatter
112 public function setBatchFormatter(FormatterInterface $formatter)
114 $this->batchFormatter = $formatter;
118 * Gets the formatter for the logs generated by handleBatch().
120 * @return FormatterInterface
122 public function getBatchFormatter()
124 if (!$this->batchFormatter) {
125 $this->batchFormatter = $this->getDefaultBatchFormatter();
128 return $this->batchFormatter;
134 protected function write(array $record)
136 $previousUserContext = false;
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']);
144 if (!empty($record['context']['tags'])) {
145 $options['tags'] = array_merge($options['tags'], $record['context']['tags']);
146 unset($record['context']['tags']);
148 if (!empty($record['context']['fingerprint'])) {
149 $options['fingerprint'] = $record['context']['fingerprint'];
150 unset($record['context']['fingerprint']);
152 if (!empty($record['context']['logger'])) {
153 $options['logger'] = $record['context']['logger'];
154 unset($record['context']['logger']);
156 $options['logger'] = $record['channel'];
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]);
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']);
174 if (!empty($record['extra'])) {
175 $options['extra']['extra'] = $record['extra'];
178 if (!empty($this->release) && !isset($options['release'])) {
179 $options['release'] = $this->release;
182 if (isset($record['context']['exception']) && ($record['context']['exception'] instanceof \Exception || (PHP_VERSION_ID >= 70000 && $record['context']['exception'] instanceof \Throwable))) {
183 $options['extra']['message'] = $record['formatted'];
184 $this->ravenClient->captureException($record['context']['exception'], $options);
186 $this->ravenClient->captureMessage($record['formatted'], array(), $options);
189 if ($previousUserContext !== false) {
190 $this->ravenClient->user_context($previousUserContext);
197 protected function getDefaultFormatter()
199 return new LineFormatter('[%channel%] %message%');
203 * Gets the default formatter for the logs generated by handleBatch().
205 * @return FormatterInterface
207 protected function getDefaultBatchFormatter()
209 return new LineFormatter();
213 * Gets extra parameters supported by Raven that can be found in "extra" and "context"
217 protected function getExtraParameters()
219 return array('checksum', 'release', 'event_id');
223 * @param string $value
226 public function setRelease($value)
228 $this->release = $value;