ILIAS  release_5-3 Revision v5.3.23-19-g915713cf615
Monolog\Handler\BrowserConsoleHandler Class Reference

Handler sending logs to browser's javascript console with no browser extension required. More...

+ Inheritance diagram for Monolog\Handler\BrowserConsoleHandler:
+ Collaboration diagram for Monolog\Handler\BrowserConsoleHandler:

Static Public Member Functions

static send ()
 Convert records to javascript console commands and send it to the browser. More...
 
static reset ()
 Forget all logged records. More...
 

Protected Member Functions

 getDefaultFormatter ()
 
 write (array $record)
 
 registerShutdownFunction ()
 Wrapper for register_shutdown_function to allow overriding. More...
 
- Protected Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 write (array $record)
 Writes the record down to the log of the implementing handler. More...
 
 processRecord (array $record)
 Processes a record. More...
 
- Protected Member Functions inherited from Monolog\Handler\AbstractHandler
 getDefaultFormatter ()
 Gets the default formatter. More...
 

Static Protected Member Functions

static writeOutput ($str)
 Wrapper for echo to allow overriding. More...
 
static getResponseFormat ()
 Checks the format of the response. More...
 

Static Protected Attributes

static $initialized = false
 
static $records = array()
 

Static Private Member Functions

static generateScript ()
 
static handleStyles ($formatted)
 
static handleCustomStyles ($style, $string)
 
static dump ($title, array $dict)
 
static quote ($arg)
 
static call ()
 
static call_array ($method, array $args)
 

Additional Inherited Members

- Public Member Functions inherited from Monolog\Handler\AbstractProcessingHandler
 handle (array $record)
 {Handles a record.All records may be passed to this method, and the handler should discard those that it does not want to handle.The return value of this function controls the bubbling process of the handler stack. Unless the bubbling is interrupted (by returning true), the Logger class will keep on calling further handlers in the stack with a given log record.
Parameters
array$recordThe record to handle
Returns
Boolean true means that this handler handled the record, and that bubbling is not permitted. false means the record was either not processed or that this handler allows bubbling.
} More...
 
- Public Member Functions inherited from Monolog\Handler\AbstractHandler
 __construct ($level=Logger::DEBUG, $bubble=true)
 
 isHandling (array $record)
 {Checks whether the given record will be handled by this handler.This is mostly done for performance reasons, to avoid calling processors for nothing.Handlers should still check the record levels within handle(), returning false in isHandling() is no guarantee that handle() will not be called, and isHandling() might not be called for a given record.
Parameters
array$recordPartial log record containing only a level key
Returns
Boolean
} More...
 
 handleBatch (array $records)
 {Handles a set of records at once.
Parameters
array$recordsThe records to handle (an array of record arrays)
} More...
 
 close ()
 Closes the handler. More...
 
 pushProcessor ($callback)
 {Adds a processor in the stack.
Parameters
callable$callback
Returns
self
} More...
 
 popProcessor ()
 {Removes the processor on top of the stack and returns it.
Returns
callable
} More...
 
 setFormatter (FormatterInterface $formatter)
 {Sets the formatter.
Parameters
FormatterInterface$formatter
Returns
self
} More...
 
 getFormatter ()
 {Gets the formatter.
Returns
FormatterInterface
} More...
 
 setLevel ($level)
 Sets minimum logging level at which this handler will be triggered. More...
 
 getLevel ()
 Gets minimum logging level at which this handler will be triggered. More...
 
 setBubble ($bubble)
 Sets the bubbling behavior. More...
 
 getBubble ()
 Gets the bubbling behavior. More...
 
 __destruct ()
 
- Protected Attributes inherited from Monolog\Handler\AbstractHandler
 $level = Logger::DEBUG
 
 $bubble = true
 
 $formatter
 
 $processors = array()
 

Detailed Description

Handler sending logs to browser's javascript console with no browser extension required.

Author
Olivier Poitrey rs@da.nosp@m.ilym.nosp@m.otion.nosp@m..com

Definition at line 21 of file BrowserConsoleHandler.php.

Member Function Documentation

◆ call()

static Monolog\Handler\BrowserConsoleHandler::call ( )
staticprivate

Definition at line 218 of file BrowserConsoleHandler.php.

219  {
220  $args = func_get_args();
221  $method = array_shift($args);
222 
223  return self::call_array($method, $args);
224  }

◆ call_array()

static Monolog\Handler\BrowserConsoleHandler::call_array (   $method,
array  $args 
)
staticprivate

Definition at line 226 of file BrowserConsoleHandler.php.

227  {
228  return 'c.' . $method . '(' . implode(', ', $args) . ');';
229  }

◆ dump()

static Monolog\Handler\BrowserConsoleHandler::dump (   $title,
array  $dict 
)
staticprivate

Definition at line 194 of file BrowserConsoleHandler.php.

References $key, $title, and array.

195  {
196  $script = array();
197  $dict = array_filter($dict);
198  if (empty($dict)) {
199  return $script;
200  }
201  $script[] = self::call('log', self::quote('%c%s'), self::quote('font-weight: bold'), self::quote($title));
202  foreach ($dict as $key => $value) {
203  $value = json_encode($value);
204  if (empty($value)) {
205  $value = self::quote('');
206  }
207  $script[] = self::call('log', self::quote('%s: %o'), self::quote($key), $value);
208  }
209 
210  return $script;
211  }
Create styles array
The data for the language used.
$key
Definition: croninfo.php:18

◆ generateScript()

static Monolog\Handler\BrowserConsoleHandler::generateScript ( )
staticprivate

Definition at line 133 of file BrowserConsoleHandler.php.

References $records, and array.

134  {
135  $script = array();
136  foreach (self::$records as $record) {
137  $context = self::dump('Context', $record['context']);
138  $extra = self::dump('Extra', $record['extra']);
139 
140  if (empty($context) && empty($extra)) {
141  $script[] = self::call_array('log', self::handleStyles($record['formatted']));
142  } else {
143  $script = array_merge($script,
144  array(self::call_array('groupCollapsed', self::handleStyles($record['formatted']))),
145  $context,
146  $extra,
147  array(self::call('groupEnd'))
148  );
149  }
150  }
151 
152  return "(function (c) {if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
153  }
$records
Definition: simple_test.php:22
Create styles array
The data for the language used.

◆ getDefaultFormatter()

Monolog\Handler\BrowserConsoleHandler::getDefaultFormatter ( )
protected

Formatted output may contain some formatting markers to be transferred to console.log using the c format.

Example of formatted string:

You can do [[blue text]]{color: blue} or [[green background]]{background-color: green; color: white}

Definition at line 35 of file BrowserConsoleHandler.php.

36  {
37  return new LineFormatter('[[%channel%]]{macro: autolabel} [[%level_name%]]{font-weight: bold} %message%');
38  }

◆ getResponseFormat()

static Monolog\Handler\BrowserConsoleHandler::getResponseFormat ( )
staticprotected

Checks the format of the response.

If Content-Type is set to application/javascript or text/javascript -> js If Content-Type is set to text/html, or is unset -> html If Content-Type is anything else -> unknown

Returns
string One of 'js', 'html' or 'unknown'

Definition at line 113 of file BrowserConsoleHandler.php.

References $header.

114  {
115  // Check content type
116  foreach (headers_list() as $header) {
117  if (stripos($header, 'content-type:') === 0) {
118  // This handler only works with HTML and javascript outputs
119  // text/javascript is obsolete in favour of application/javascript, but still used
120  if (stripos($header, 'application/javascript') !== false || stripos($header, 'text/javascript') !== false) {
121  return 'js';
122  }
123  if (stripos($header, 'text/html') === false) {
124  return 'unknown';
125  }
126  break;
127  }
128  }
129 
130  return 'html';
131  }

◆ handleCustomStyles()

static Monolog\Handler\BrowserConsoleHandler::handleCustomStyles (   $style,
  $string 
)
staticprivate

Definition at line 174 of file BrowserConsoleHandler.php.

References $m, $style, and array.

175  {
176  static $colors = array('blue', 'green', 'red', 'magenta', 'orange', 'black', 'grey');
177  static $labels = array();
178 
179  return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function ($m) use ($string, &$colors, &$labels) {
180  if (trim($m[1]) === 'autolabel') {
181  // Format the string as a label with consistent auto assigned background color
182  if (!isset($labels[$string])) {
183  $labels[$string] = $colors[count($labels) % count($colors)];
184  }
185  $color = $labels[$string];
186 
187  return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px";
188  }
189 
190  return $m[1];
191  }, $style);
192  }
$style
Definition: example_012.php:70
Create styles array
The data for the language used.

◆ handleStyles()

static Monolog\Handler\BrowserConsoleHandler::handleStyles (   $formatted)
staticprivate

Definition at line 155 of file BrowserConsoleHandler.php.

References $format, and array.

156  {
157  $args = array(self::quote('font-weight: normal'));
158  $format = '%c' . $formatted;
159  preg_match_all('/\[\[(.*?)\]\]\{([^}]*)\}/s', $format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
160 
161  foreach (array_reverse($matches) as $match) {
162  $args[] = self::quote(self::handleCustomStyles($match[2][0], $match[1][0]));
163  $args[] = '"font-weight: normal"';
164 
165  $pos = $match[0][1];
166  $format = substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . substr($format, $pos + strlen($match[0][0]));
167  }
168 
169  array_unshift($args, self::quote($format));
170 
171  return $args;
172  }
$format
Definition: metadata.php:141
Create styles array
The data for the language used.

◆ quote()

static Monolog\Handler\BrowserConsoleHandler::quote (   $arg)
staticprivate

Definition at line 213 of file BrowserConsoleHandler.php.

214  {
215  return '"' . addcslashes($arg, "\"\n\\") . '"';
216  }

◆ registerShutdownFunction()

Monolog\Handler\BrowserConsoleHandler::registerShutdownFunction ( )
protected

Wrapper for register_shutdown_function to allow overriding.

Definition at line 87 of file BrowserConsoleHandler.php.

References array.

Referenced by Monolog\Handler\BrowserConsoleHandler\write().

88  {
89  if (PHP_SAPI !== 'cli') {
90  register_shutdown_function(array('Monolog\Handler\BrowserConsoleHandler', 'send'));
91  }
92  }
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ reset()

static Monolog\Handler\BrowserConsoleHandler::reset ( )
static

Forget all logged records.

Definition at line 79 of file BrowserConsoleHandler.php.

References $records, and array.

Referenced by Monolog\Handler\BrowserConsoleHandlerTest\setUp().

80  {
82  }
$records
Definition: simple_test.php:22
Create styles array
The data for the language used.
+ Here is the caller graph for this function:

◆ send()

static Monolog\Handler\BrowserConsoleHandler::send ( )
static

Convert records to javascript console commands and send it to the browser.

This method is automatically called on PHP shutdown if output is HTML or Javascript.

Definition at line 59 of file BrowserConsoleHandler.php.

References $format, and $records.

60  {
61  $format = self::getResponseFormat();
62  if ($format === 'unknown') {
63  return;
64  }
65 
66  if (count(self::$records)) {
67  if ($format === 'html') {
68  self::writeOutput('<script>' . self::generateScript() . '</script>');
69  } elseif ($format === 'js') {
70  self::writeOutput(self::generateScript());
71  }
72  self::reset();
73  }
74  }
$format
Definition: metadata.php:141
$records
Definition: simple_test.php:22

◆ write()

Monolog\Handler\BrowserConsoleHandler::write ( array  $record)
protected

Definition at line 43 of file BrowserConsoleHandler.php.

References $records, and Monolog\Handler\BrowserConsoleHandler\registerShutdownFunction().

44  {
45  // Accumulate records
46  self::$records[] = $record;
47 
48  // Register shutdown handler if not already done
49  if (!self::$initialized) {
50  self::$initialized = true;
51  $this->registerShutdownFunction();
52  }
53  }
$records
Definition: simple_test.php:22
registerShutdownFunction()
Wrapper for register_shutdown_function to allow overriding.
+ Here is the call graph for this function:

◆ writeOutput()

static Monolog\Handler\BrowserConsoleHandler::writeOutput (   $str)
staticprotected

Wrapper for echo to allow overriding.

Parameters
string$str

Definition at line 99 of file BrowserConsoleHandler.php.

100  {
101  echo $str;
102  }

Field Documentation

◆ $initialized

Monolog\Handler\BrowserConsoleHandler::$initialized = false
staticprotected

Definition at line 23 of file BrowserConsoleHandler.php.

◆ $records

Monolog\Handler\BrowserConsoleHandler::$records = array()
staticprotected

Definition at line 24 of file BrowserConsoleHandler.php.


The documentation for this class was generated from the following file: