ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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 
23 {
27  const VERSION = '4.0';
28 
32  const HEADER_NAME = 'X-ChromeLogger-Data';
33 
34  protected static $initialized = false;
35 
43  protected static $overflowed = false;
44 
45  protected static $json = array(
46  'version' => self::VERSION,
47  'columns' => array('label', 'log', 'backtrace', 'type'),
48  'rows' => array(),
49  );
50 
51  protected static $sendHeaders = true;
52 
57  public function __construct($level = Logger::DEBUG, $bubble = true)
58  {
59  parent::__construct($level, $bubble);
60  if (!function_exists('json_encode')) {
61  throw new \RuntimeException('PHP\'s json extension is required to use Monolog\'s ChromePHPHandler');
62  }
63  }
64 
68  public function handleBatch(array $records)
69  {
70  $messages = array();
71 
72  foreach ($records as $record) {
73  if ($record['level'] < $this->level) {
74  continue;
75  }
76  $messages[] = $this->processRecord($record);
77  }
78 
79  if (!empty($messages)) {
80  $messages = $this->getFormatter()->formatBatch($messages);
81  self::$json['rows'] = array_merge(self::$json['rows'], $messages);
82  $this->send();
83  }
84  }
85 
89  protected function getDefaultFormatter()
90  {
91  return new ChromePHPFormatter();
92  }
93 
101  protected function write(array $record)
102  {
103  self::$json['rows'][] = $record['formatted'];
104 
105  $this->send();
106  }
107 
113  protected function send()
114  {
115  if (self::$overflowed || !self::$sendHeaders) {
116  return;
117  }
118 
119  if (!self::$initialized) {
120  self::$initialized = true;
121 
122  self::$sendHeaders = $this->headersAccepted();
123  if (!self::$sendHeaders) {
124  return;
125  }
126 
127  self::$json['request_uri'] = isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : '';
128  }
129 
130  $json = @json_encode(self::$json);
131  $data = base64_encode(utf8_encode($json));
132  if (strlen($data) > 240*1024) {
133  self::$overflowed = true;
134 
135  $record = array(
136  'message' => 'Incomplete logs, chrome header size limit reached',
137  'context' => array(),
138  'level' => Logger::WARNING,
139  'level_name' => Logger::getLevelName(Logger::WARNING),
140  'channel' => 'monolog',
141  'datetime' => new \DateTime(),
142  'extra' => array(),
143  );
144  self::$json['rows'][count(self::$json['rows']) - 1] = $this->getFormatter()->format($record);
145  $json = @json_encode(self::$json);
146  $data = base64_encode(utf8_encode($json));
147  }
148 
149  if (trim($data) !== '') {
150  $this->sendHeader(self::HEADER_NAME, $data);
151  }
152  }
153 
160  protected function sendHeader($header, $content)
161  {
162  if (!headers_sent() && self::$sendHeaders) {
163  header(sprintf('%s: %s', $header, $content));
164  }
165  }
166 
172  protected function headersAccepted()
173  {
174  if (empty($_SERVER['HTTP_USER_AGENT'])) {
175  return false;
176  }
177 
178  return preg_match('{\bChrome/\d+[\.\d+]*\b}', $_SERVER['HTTP_USER_AGENT']);
179  }
180 
184  public function __get($property)
185  {
186  if ('sendHeaders' !== $property) {
187  throw new \InvalidArgumentException('Undefined property '.$property);
188  }
189 
190  return static::$sendHeaders;
191  }
192 
196  public function __set($property, $value)
197  {
198  if ('sendHeaders' !== $property) {
199  throw new \InvalidArgumentException('Undefined property '.$property);
200  }
201 
202  static::$sendHeaders = $value;
203  }
204 }
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:17
$data
$header
Handler sending logs to the ChromePHP extension (http://www.chromephp.com/)
getFormatter()
{Gets the formatter.FormatterInterface}
const WARNING
Exceptional occurrences that are not errors.
Definition: Logger.php:52
__construct($level=Logger::DEBUG, $bubble=true)
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:388
__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.