ILIAS  release_5-2 Revision v5.2.25-18-g3f80b828510
LogglyHandler.php
Go to the documentation of this file.
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;
16 
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  {
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 }
const DEBUG
Detailed debug information.
Definition: Logger.php:32
Base Handler class providing the Handler structure.
Sends errors to Loggly.
$url
Definition: shib_logout.php:72
Encodes message information into JSON in a format compatible with Loggly.
__construct($token, $level=Logger::DEBUG, $bubble=true)
$records
Definition: simple_test.php:22
static execute($ch, $retries=5, $closeAfterDone=true)
Executes a CURL request with optional retries and exception on failure.
Definition: Util.php:32
handleBatch(array $records)
Handles a set of records at once.
getFormatter()
{Gets the formatter.FormatterInterface}
Create styles array
The data for the language used.