scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 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  
14 use RollbarNotifier;
15 use Exception;
16 use Monolog\Logger;
17  
18 /**
19 * Sends errors to Rollbar
20 *
21 * If the context data contains a `payload` key, that is used as an array
22 * of payload options to RollbarNotifier's report_message/report_exception methods.
23 *
24 * Rollbar's context info will contain the context + extra keys from the log record
25 * merged, and then on top of that a few keys:
26 *
27 * - level (rollbar level name)
28 * - monolog_level (monolog level name, raw level, as rollbar only has 5 but monolog 8)
29 * - channel
30 * - datetime (unix timestamp)
31 *
32 * @author Paul Statezny <paulstatezny@gmail.com>
33 */
34 class RollbarHandler extends AbstractProcessingHandler
35 {
36 /**
37 * Rollbar notifier
38 *
39 * @var RollbarNotifier
40 */
41 protected $rollbarNotifier;
42  
43 protected $levelMap = array(
44 Logger::DEBUG => 'debug',
45 Logger::INFO => 'info',
46 Logger::NOTICE => 'info',
47 Logger::WARNING => 'warning',
48 Logger::ERROR => 'error',
49 Logger::CRITICAL => 'critical',
50 Logger::ALERT => 'critical',
51 Logger::EMERGENCY => 'critical',
52 );
53  
54 /**
55 * Records whether any log records have been added since the last flush of the rollbar notifier
56 *
57 * @var bool
58 */
59 private $hasRecords = false;
60  
61 protected $initialized = false;
62  
63 /**
64 * @param RollbarNotifier $rollbarNotifier RollbarNotifier object constructed with valid token
65 * @param int $level The minimum logging level at which this handler will be triggered
66 * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
67 */
68 public function __construct(RollbarNotifier $rollbarNotifier, $level = Logger::ERROR, $bubble = true)
69 {
70 $this->rollbarNotifier = $rollbarNotifier;
71  
72 parent::__construct($level, $bubble);
73 }
74  
75 /**
76 * {@inheritdoc}
77 */
78 protected function write(array $record)
79 {
80 if (!$this->initialized) {
81 // __destructor() doesn't get called on Fatal errors
82 register_shutdown_function(array($this, 'close'));
83 $this->initialized = true;
84 }
85  
86 $context = $record['context'];
87 $payload = array();
88 if (isset($context['payload'])) {
89 $payload = $context['payload'];
90 unset($context['payload']);
91 }
92 $context = array_merge($context, $record['extra'], array(
93 'level' => $this->levelMap[$record['level']],
94 'monolog_level' => $record['level_name'],
95 'channel' => $record['channel'],
96 'datetime' => $record['datetime']->format('U'),
97 ));
98  
99 if (isset($context['exception']) && $context['exception'] instanceof Exception) {
100 $payload['level'] = $context['level'];
101 $exception = $context['exception'];
102 unset($context['exception']);
103  
104 $this->rollbarNotifier->report_exception($exception, $context, $payload);
105 } else {
106 $this->rollbarNotifier->report_message(
107 $record['message'],
108 $context['level'],
109 $context,
110 $payload
111 );
112 }
113  
114 $this->hasRecords = true;
115 }
116  
117 public function flush()
118 {
119 if ($this->hasRecords) {
120 $this->rollbarNotifier->flush();
121 $this->hasRecords = false;
122 }
123 }
124  
125 /**
126 * {@inheritdoc}
127 */
128 public function close()
129 {
130 $this->flush();
131 }
132 }