scratch – Blame information for rev 122
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
120 | 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 Monolog\Logger; |
||
15 | use Monolog\Formatter\LogglyFormatter; |
||
16 | |||
17 | /** |
||
18 | * Sends errors to Loggly. |
||
19 | * |
||
20 | * @author Przemek Sobstel <przemek@sobstel.org> |
||
21 | * @author Adam Pancutt <adam@pancutt.com> |
||
22 | * @author Gregory Barchard <gregory@barchard.net> |
||
23 | */ |
||
24 | class LogglyHandler extends AbstractProcessingHandler |
||
25 | { |
||
26 | const HOST = 'logs-01.loggly.com'; |
||
27 | const ENDPOINT_SINGLE = 'inputs'; |
||
28 | const ENDPOINT_BATCH = 'bulk'; |
||
29 | |||
30 | protected $token; |
||
31 | |||
32 | protected $tag = array(); |
||
33 | |||
34 | public function __construct($token, $level = Logger::DEBUG, $bubble = true) |
||
35 | { |
||
36 | if (!extension_loaded('curl')) { |
||
37 | throw new \LogicException('The curl extension is needed to use the LogglyHandler'); |
||
38 | } |
||
39 | |||
40 | $this->token = $token; |
||
41 | |||
42 | parent::__construct($level, $bubble); |
||
43 | } |
||
44 | |||
45 | public function setTag($tag) |
||
46 | { |
||
47 | $tag = !empty($tag) ? $tag : array(); |
||
48 | $this->tag = is_array($tag) ? $tag : array($tag); |
||
49 | } |
||
50 | |||
51 | public function addTag($tag) |
||
52 | { |
||
53 | if (!empty($tag)) { |
||
54 | $tag = is_array($tag) ? $tag : array($tag); |
||
55 | $this->tag = array_unique(array_merge($this->tag, $tag)); |
||
56 | } |
||
57 | } |
||
58 | |||
59 | protected function write(array $record) |
||
60 | { |
||
61 | $this->send($record["formatted"], self::ENDPOINT_SINGLE); |
||
62 | } |
||
63 | |||
64 | public function handleBatch(array $records) |
||
65 | { |
||
66 | $level = $this->level; |
||
67 | |||
68 | $records = array_filter($records, function ($record) use ($level) { |
||
69 | return ($record['level'] >= $level); |
||
70 | }); |
||
71 | |||
72 | if ($records) { |
||
73 | $this->send($this->getFormatter()->formatBatch($records), self::ENDPOINT_BATCH); |
||
74 | } |
||
75 | } |
||
76 | |||
77 | protected function send($data, $endpoint) |
||
78 | { |
||
79 | $url = sprintf("https://%s/%s/%s/", self::HOST, $endpoint, $this->token); |
||
80 | |||
81 | $headers = array('Content-Type: application/json'); |
||
82 | |||
83 | if (!empty($this->tag)) { |
||
84 | $headers[] = 'X-LOGGLY-TAG: '.implode(',', $this->tag); |
||
85 | } |
||
86 | |||
87 | $ch = curl_init(); |
||
88 | |||
89 | curl_setopt($ch, CURLOPT_URL, $url); |
||
90 | curl_setopt($ch, CURLOPT_POST, true); |
||
91 | curl_setopt($ch, CURLOPT_POSTFIELDS, $data); |
||
92 | curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); |
||
93 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); |
||
94 | |||
95 | Curl\Util::execute($ch); |
||
96 | } |
||
97 | |||
98 | protected function getDefaultFormatter() |
||
99 | { |
||
100 | return new LogglyFormatter(); |
||
101 | } |
||
102 | } |