ILIAS  release_5-1 Revision 5.0.0-5477-g43f3e3fab5f
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)
 
- 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 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 180 of file BrowserConsoleHandler.php.

181  {
182  $args = func_get_args();
183  $method = array_shift($args);
184 
185  return self::call_array($method, $args);
186  }

◆ call_array()

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

Definition at line 188 of file BrowserConsoleHandler.php.

189  {
190  return 'c.' . $method . '(' . implode(', ', $args) . ');';
191  }

◆ dump()

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

Definition at line 156 of file BrowserConsoleHandler.php.

157  {
158  $script = array();
159  $dict = array_filter($dict);
160  if (empty($dict)) {
161  return $script;
162  }
163  $script[] = self::call('log', self::quote('%c%s'), self::quote('font-weight: bold'), self::quote($title));
164  foreach ($dict as $key => $value) {
165  $value = json_encode($value);
166  if (empty($value)) {
167  $value = self::quote('');
168  }
169  $script[] = self::call('log', self::quote('%s: %o'), self::quote($key), $value);
170  }
171 
172  return $script;
173  }

◆ generateScript()

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

Definition at line 95 of file BrowserConsoleHandler.php.

References $records.

96  {
97  $script = array();
98  foreach (self::$records as $record) {
99  $context = self::dump('Context', $record['context']);
100  $extra = self::dump('Extra', $record['extra']);
101 
102  if (empty($context) && empty($extra)) {
103  $script[] = self::call_array('log', self::handleStyles($record['formatted']));
104  } else {
105  $script = array_merge($script,
106  array(self::call_array('groupCollapsed', self::handleStyles($record['formatted']))),
107  $context,
108  $extra,
109  array(self::call('groupEnd'))
110  );
111  }
112  }
113 
114  return "(function (c) {if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
115  }
$records
Definition: simple_test.php:17

◆ 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 36 of file BrowserConsoleHandler.php.

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

◆ handleCustomStyles()

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

Definition at line 136 of file BrowserConsoleHandler.php.

References $style.

137  {
138  static $colors = array('blue', 'green', 'red', 'magenta', 'orange', 'black', 'grey');
139  static $labels = array();
140 
141  return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function ($m) use ($string, &$colors, &$labels) {
142  if (trim($m[1]) === 'autolabel') {
143  // Format the string as a label with consistent auto assigned background color
144  if (!isset($labels[$string])) {
145  $labels[$string] = $colors[count($labels) % count($colors)];
146  }
147  $color = $labels[$string];
148 
149  return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px";
150  }
151 
152  return $m[1];
153  }, $style);
154  }
$style
Definition: example_012.php:70

◆ handleStyles()

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

Definition at line 117 of file BrowserConsoleHandler.php.

118  {
119  $args = array(self::quote('font-weight: normal'));
120  $format = '%c' . $formatted;
121  preg_match_all('/\[\[(.*?)\]\]\{([^}]*)\}/s', $format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
122 
123  foreach (array_reverse($matches) as $match) {
124  $args[] = self::quote(self::handleCustomStyles($match[2][0], $match[1][0]));
125  $args[] = '"font-weight: normal"';
126 
127  $pos = $match[0][1];
128  $format = substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . substr($format, $pos + strlen($match[0][0]));
129  }
130 
131  array_unshift($args, self::quote($format));
132 
133  return $args;
134  }

◆ quote()

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

Definition at line 175 of file BrowserConsoleHandler.php.

176  {
177  return '"' . addcslashes($arg, "\"\n\\") . '"';
178  }

◆ reset()

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

Forget all logged records.

Definition at line 90 of file BrowserConsoleHandler.php.

References $records.

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

91  {
92  self::$records = array();
93  }
$records
Definition: simple_test.php:17
+ 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 60 of file BrowserConsoleHandler.php.

References $header, and $records.

61  {
62  $htmlTags = true;
63  // Check content type
64  foreach (headers_list() as $header) {
65  if (stripos($header, 'content-type:') === 0) {
66  // This handler only works with HTML and javascript outputs
67  // text/javascript is obsolete in favour of application/javascript, but still used
68  if (stripos($header, 'application/javascript') !== false || stripos($header, 'text/javascript') !== false) {
69  $htmlTags = false;
70  } elseif (stripos($header, 'text/html') === false) {
71  return;
72  }
73  break;
74  }
75  }
76 
77  if (count(self::$records)) {
78  if ($htmlTags) {
79  echo '<script>' , self::generateScript() , '</script>';
80  } else {
81  echo self::generateScript();
82  }
83  self::reset();
84  }
85  }
$records
Definition: simple_test.php:17
$header

◆ write()

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

Definition at line 44 of file BrowserConsoleHandler.php.

References $records.

45  {
46  // Accumulate records
47  self::$records[] = $record;
48 
49  // Register shutdown handler if not already done
50  if (PHP_SAPI !== 'cli' && !self::$initialized) {
51  self::$initialized = true;
52  register_shutdown_function(array('Monolog\Handler\BrowserConsoleHandler', 'send'));
53  }
54  }
$records
Definition: simple_test.php:17

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: