2
0

RavenHandler.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Logger;
  12. use Monolog\Handler\AbstractProcessingHandler;
  13. use Monolog\Formatter\RavenFormatter;
  14. use \Raven_Client;
  15. /**
  16. * Handler to send messages to a Sentry (https://github.com/dcramer/sentry) server
  17. * using raven-php (https://github.com/getsentry/raven-php)
  18. *
  19. * @author Marc Abramowitz <marc@marc-abramowitz.com>
  20. */
  21. class RavenHandler extends AbstractProcessingHandler
  22. {
  23. /**
  24. * @var Raven_Client the client object that sends the message to the server
  25. */
  26. protected $ravenClient;
  27. /**
  28. * @param Raven_Client $ravenClient
  29. * @param integer $level The minimum logging level at which this handler will be triggered
  30. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  31. */
  32. public function __construct(Raven_Client $ravenClient, $level = Logger::DEBUG, $bubble = true)
  33. {
  34. parent::__construct($level, $bubble);
  35. $this->ravenClient = $ravenClient;
  36. }
  37. /**
  38. * {@inheritdoc}
  39. */
  40. public function close()
  41. {
  42. $this->ravenClient = null;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. protected function write(array $record)
  48. {
  49. if ($record['level'] == Logger::ERROR)
  50. {
  51. $this->ravenClient->captureException($record['context']['context']);
  52. }
  53. else
  54. {
  55. $this->ravenClient->captureMessage(
  56. $record['formatted']['message'], $params = $record,
  57. $record['formatted']['level'], $stack = true
  58. );
  59. }
  60. }
  61. /**
  62. * {@inheritDoc}
  63. */
  64. protected function getDefaultFormatter()
  65. {
  66. return new RavenFormatter();
  67. }
  68. }