ILIAS  release_5-2 Revision v5.2.25-18-g3f80b82851
ChromePHPHandler.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 
15 use Monolog\Logger;
16 
25 {
29  const VERSION = '4.0';
30 
34  const HEADER_NAME = 'X-ChromeLogger-Data';
35 
39  const USER_AGENT_REGEX = '{\b(?:Chrome/\d+(?:\.\d+)*|Firefox/(?:4[3-9]|[5-9]\d|\d{3,})(?:\.\d)*)\b}';
40 
41  protected static $initialized = false;
42 
50  protected static $overflowed = false;
51 
52  protected static $json = array(
53  'version' => self::VERSION,
54  'columns' => array('label', 'log', 'backtrace', 'type'),
55  'rows' => array(),
56  );
57 
58  protected static $sendHeaders = true;
59 
64  public function __construct($level = Logger::DEBUG, $bubble = true)
65  {
66  parent::__construct($level, $bubble);
67  if (!function_exists('json_encode')) {
68  throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s ChromePHPHandler');
69  }
70  }
71 
75  public function handleBatch(array $records)
76  {
77  $messages = array();
78 
79  foreach ($records as $record) {
80  if ($record['level'] < $this->level) {
81  continue;
82  }
83  $messages[] = $this->processRecord($record);
84  }
85 
86  if (!empty($messages)) {
87  $messages = $this->getFormatter()->formatBatch($messages);
88  self::$json['rows'] = array_merge(self::$json['rows'], $messages);
89  $this->send();
90  }
91  }
92 
96  protected function getDefaultFormatter()
97  {
98  return new ChromePHPFormatter();
99  }
100 
108  protected function write(array $record)
109  {
110  self::$json['rows'][] = $record['formatted'];
111 
112  $this->send();
113  }
114 
120  protected function send()
121  {
122  if (self::$overflowed || !self::$sendHeaders) {
123  return;
124  }
125 
126  if (!self::$initialized) {
127  self::$initialized = true;
128 
129  self::$sendHeaders = $this->headersAccepted();
130  if (!self::$sendHeaders) {
131  return;
132  }
133 
134  self::$json['request_uri'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
135  }
136 
137  $json = @json_encode(self::$json);
138  $data = base64_encode(utf8_encode($json));
139  if (strlen($data) > 240 * 1024) {
140  self::$overflowed = true;
141 
142  $record = array(
143  'message' => 'Incomplete logs, chrome header size limit reached',
144  'context' => array(),
145  'level' => Logger::WARNING,
146  'level_name' => Logger::getLevelName(Logger::WARNING),
147  'channel' => 'monolog',
148  'datetime' => new \DateTime(),
149  'extra' => array(),
150  );
151  self::$json['rows'][count(self::$json['rows']) - 1] = $this->getFormatter()->format($record);
152  $json = @json_encode(self::$json);
153  $data = base64_encode(utf8_encode($json));
154  }
155 
156  if (trim($data) !== '') {
157  $this->sendHeader(self::HEADER_NAME, $data);
158  }
159  }
160 
167  protected function sendHeader($header, $content)
168  {
169  if (!headers_sent() && self::$sendHeaders) {
170  header(sprintf('%s: %s', $header, $content));
171  }
172  }
173 
179  protected function headersAccepted()
180  {
181  if (empty($_SERVER['HTTP_USER_AGENT'])) {
182  return false;
183  }
184 
185  return preg_match(self::USER_AGENT_REGEX, $_SERVER['HTTP_USER_AGENT']);
186  }
187 
191  public function __get($property)
192  {
193  if ('sendHeaders' !== $property) {
194  throw new \InvalidArgumentException('Undefined property '.$property);
195  }
196 
197  return static::$sendHeaders;
198  }
199 
203  public function __set($property, $value)
204  {
205  if ('sendHeaders' !== $property) {
206  throw new \InvalidArgumentException('Undefined property '.$property);
207  }
208 
209  static::$sendHeaders = $value;
210  }
211 }
const USER_AGENT_REGEX
Regular expression to detect supported browsers (matches any Chrome, or Firefox 43+) ...
const DEBUG
Detailed debug information.
Definition: Logger.php:32
Formats a log message according to the ChromePHP array format.
if((!isset($_SERVER['DOCUMENT_ROOT'])) OR(empty($_SERVER['DOCUMENT_ROOT']))) $_SERVER['DOCUMENT_ROOT']
handleBatch(array $records)
{Handles a set of records at once.The records to handle (an array of record arrays)} ...
sendHeader($header, $content)
Send header string to the client.
__get($property)
BC getter for the sendHeaders property that has been made static.
Base Handler class providing the Handler structure.
$records
Definition: simple_test.php:22
$header
Handler sending logs to the ChromePHP extension (http://www.chromephp.com/)
getFormatter()
{Gets the formatter.FormatterInterface}
Add a drawing to the header
Definition: 04printing.php:69
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
__construct($level=Logger::DEBUG, $bubble=true)
Create styles array
The data for the language used.
processRecord(array $record)
Processes a record.
$messages
Definition: en-x-test.php:7
headersAccepted()
Verifies if the headers are accepted by the current user agent.
static getLevelName($level)
Gets the name of the logging level.
Definition: Logger.php:458
__set($property, $value)
BC setter for the sendHeaders property that has been made static.
write(array $record)
Creates & sends header for a record.
const VERSION
Version of the extension.