1
0
mirror of https://github.com/Seldaek/monolog.git synced 2025-10-24 01:56:18 +02:00

Add RavenHandler and RavenFormatter for working with Raven which is the

protocol used by Sentry (http://github.com/dcramer/sentry). Uses
raven-php (https://github.com/getsentry/raven-php).
This commit is contained in:
Marc Abramowitz
2012-04-24 17:20:13 -07:00
parent abc80e0db8
commit acbf574b98
4 changed files with 237 additions and 0 deletions

View File

@@ -0,0 +1,51 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Formatter;
use Monolog\Logger;
use \Raven_Client;
/**
* Serializes a log message for Raven (https://github.com/getsentry/raven-php)
*
* @author Marc Abramowitz <marc@marc-abramowitz.com>
*/
class RavenFormatter extends NormalizerFormatter
{
/**
* Translates Monolog log levels to Raven log levels.
*/
private $logLevels = array(
Logger::DEBUG => Raven_Client::DEBUG,
Logger::INFO => Raven_Client::INFO,
Logger::WARNING => Raven_Client::WARNING,
Logger::ERROR => Raven_Client::ERROR,
);
/**
* {@inheritdoc}
*/
public function format(array $record)
{
$record = parent::format($record);
$record['level'] = $this->logLevels[$record['level']];
$record['message'] = $record['channel'] . ': ' . $record['message'];
if (isset($record['context']['context']))
{
$record['context'] = $record['context']['context'];
}
return $record;
}
}

View File

@@ -0,0 +1,77 @@
<?php
/*
* This file is part of the Monolog package.
*
* (c) Jordi Boggiano <j.boggiano@seld.be>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Monolog\Handler;
use Monolog\Logger;
use Monolog\Handler\AbstractProcessingHandler;
use Monolog\Formatter\RavenFormatter;
use \Raven_Client;
/**
* Handler to send messages to a Sentry (https://github.com/dcramer/sentry) server
* using raven-php (https://github.com/getsentry/raven-php)
*
* @author Marc Abramowitz <marc@marc-abramowitz.com>
*/
class RavenHandler extends AbstractProcessingHandler
{
/**
* @var Raven_Client the client object that sends the message to the server
*/
protected $ravenClient;
/**
* @param Raven_Client $ravenClient
* @param integer $level The minimum logging level at which this handler will be triggered
* @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
*/
public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true)
{
parent::__construct($level, $bubble);
$this->ravenClient = $ravenClient;
}
/**
* {@inheritdoc}
*/
public function close()
{
$this->ravenClient = null;
}
/**
* {@inheritdoc}
*/
protected function write(array $record)
{
if ($record['level'] == Logger::ERROR)
{
$this->ravenClient->captureException($record['context']['context']);
}
else
{
$this->ravenClient->captureMessage(
$record['formatted']['message'], $params = $record,
$record['formatted']['level'], $stack = true
);
}
}
/**
* {@inheritDoc}
*/
protected function getDefaultFormatter()
{
return new RavenFormatter();
}
}