ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
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:

Public Member Functions

 close ()
 Closes the handler. More...
 
 reset ()
 
- 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
bool 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
bool
} 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 ()
 
 reset ()
 
 isHandling (array $record)
 Checks whether the given record will be handled by this handler. More...
 
 handle (array $record)
 Handles a record. More...
 
 handleBatch (array $records)
 Handles a set of records at once. More...
 
 pushProcessor ($callback)
 Adds a processor in the stack. More...
 
 popProcessor ()
 Removes the processor on top of the stack and returns it. More...
 
 setFormatter (FormatterInterface $formatter)
 Sets the formatter. More...
 
 getFormatter ()
 Gets the formatter. More...
 
 reset ()
 

Static Public Member Functions

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

Protected Member Functions

 getDefaultFormatter ()
 Gets the default formatter.
Returns
FormatterInterface
More...
 
 write (array $record)
 Writes the record down to the log of the implementing handler.
Parameters
array$record
Returns
void
More...
 
 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...
 
 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

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

229 {
230 $args = func_get_args();
231 $method = array_shift($args);
232
233 return static::call_array($method, $args);
234 }

◆ call_array()

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

Definition at line 236 of file BrowserConsoleHandler.php.

237 {
238 return 'c.' . $method . '(' . implode(', ', $args) . ');';
239 }

◆ close()

Monolog\Handler\BrowserConsoleHandler::close ( )

Closes the handler.

This will be called automatically when the object is destroyed

Reimplemented from Monolog\Handler\AbstractHandler.

Definition at line 76 of file BrowserConsoleHandler.php.

77 {
79 }
static resetStatic()
Forget all logged records.

References Monolog\Handler\BrowserConsoleHandler\resetStatic().

+ Here is the call graph for this function:

◆ dump()

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

Definition at line 204 of file BrowserConsoleHandler.php.

205 {
206 $script = array();
207 $dict = array_filter($dict);
208 if (empty($dict)) {
209 return $script;
210 }
211 $script[] = static::call('log', static::quote('%c%s'), static::quote('font-weight: bold'), static::quote($title));
212 foreach ($dict as $key => $value) {
213 $value = json_encode($value);
214 if (empty($value)) {
215 $value = static::quote('');
216 }
217 $script[] = static::call('log', static::quote('%s: %o'), static::quote($key), $value);
218 }
219
220 return $script;
221 }
$key
Definition: croninfo.php:18

References $key, and $title.

◆ generateScript()

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

Definition at line 143 of file BrowserConsoleHandler.php.

144 {
145 $script = array();
146 foreach (static::$records as $record) {
147 $context = static::dump('Context', $record['context']);
148 $extra = static::dump('Extra', $record['extra']);
149
150 if (empty($context) && empty($extra)) {
151 $script[] = static::call_array('log', static::handleStyles($record['formatted']));
152 } else {
153 $script = array_merge($script,
154 array(static::call_array('groupCollapsed', static::handleStyles($record['formatted']))),
155 $context,
156 $extra,
157 array(static::call('groupEnd'))
158 );
159 }
160 }
161
162 return "(function (c) {if (c && c.groupCollapsed) {\n" . implode("\n", $script) . "\n}})(console);";
163 }
$records
Definition: simple_test.php:22
$context
Definition: webdav.php:25

References $context, and $records.

◆ getDefaultFormatter()

Monolog\Handler\BrowserConsoleHandler::getDefaultFormatter ( )
protected

Gets the default formatter.

Returns
FormatterInterface

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}

Reimplemented from Monolog\Handler\AbstractHandler.

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

124 {
125 // Check content type
126 foreach (headers_list() as $header) {
127 if (stripos($header, 'content-type:') === 0) {
128 // This handler only works with HTML and javascript outputs
129 // text/javascript is obsolete in favour of application/javascript, but still used
130 if (stripos($header, 'application/javascript') !== false || stripos($header, 'text/javascript') !== false) {
131 return 'js';
132 }
133 if (stripos($header, 'text/html') === false) {
134 return 'unknown';
135 }
136 break;
137 }
138 }
139
140 return 'html';
141 }

References $header.

◆ handleCustomStyles()

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

Definition at line 184 of file BrowserConsoleHandler.php.

185 {
186 static $colors = array('blue', 'green', 'red', 'magenta', 'orange', 'black', 'grey');
187 static $labels = array();
188
189 return preg_replace_callback('/macro\s*:(.*?)(?:;|$)/', function ($m) use ($string, &$colors, &$labels) {
190 if (trim($m[1]) === 'autolabel') {
191 // Format the string as a label with consistent auto assigned background color
192 if (!isset($labels[$string])) {
193 $labels[$string] = $colors[count($labels) % count($colors)];
194 }
195 $color = $labels[$string];
196
197 return "background-color: $color; color: white; border-radius: 3px; padding: 0 2px 0 2px";
198 }
199
200 return $m[1];
201 }, $style);
202 }
$style
Definition: example_012.php:70

References $m, and $style.

◆ handleStyles()

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

Definition at line 165 of file BrowserConsoleHandler.php.

166 {
167 $args = array(static::quote('font-weight: normal'));
168 $format = '%c' . $formatted;
169 preg_match_all('/\[\[(.*?)\]\]\{([^}]*)\}/s', $format, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
170
171 foreach (array_reverse($matches) as $match) {
172 $args[] = static::quote(static::handleCustomStyles($match[2][0], $match[1][0]));
173 $args[] = '"font-weight: normal"';
174
175 $pos = $match[0][1];
176 $format = substr($format, 0, $pos) . '%c' . $match[1][0] . '%c' . substr($format, $pos + strlen($match[0][0]));
177 }
178
179 array_unshift($args, static::quote($format));
180
181 return $args;
182 }
$format
Definition: metadata.php:141

References $format.

◆ quote()

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

Definition at line 223 of file BrowserConsoleHandler.php.

224 {
225 return '"' . addcslashes($arg, "\"\n\\") . '"';
226 }

◆ registerShutdownFunction()

Monolog\Handler\BrowserConsoleHandler::registerShutdownFunction ( )
protected

Wrapper for register_shutdown_function to allow overriding.

Definition at line 97 of file BrowserConsoleHandler.php.

98 {
99 if (PHP_SAPI !== 'cli') {
100 register_shutdown_function(array('Monolog\Handler\BrowserConsoleHandler', 'send'));
101 }
102 }

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

+ Here is the caller graph for this function:

◆ reset()

Monolog\Handler\BrowserConsoleHandler::reset ( )

Reimplemented from Monolog\Handler\AbstractHandler.

Definition at line 81 of file BrowserConsoleHandler.php.

82 {
84 }

References Monolog\Handler\BrowserConsoleHandler\resetStatic().

+ Here is the call graph for this function:

◆ resetStatic()

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

Forget all logged records.

Definition at line 89 of file BrowserConsoleHandler.php.

90 {
91 static::$records = array();
92 }

References $records.

Referenced by Monolog\Handler\BrowserConsoleHandler\close(), Monolog\Handler\BrowserConsoleHandler\reset(), and Monolog\Handler\BrowserConsoleHandlerTest\setUp().

+ 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.

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

References $format, and $records.

◆ write()

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

Writes the record down to the log of the implementing handler.

Parameters
array$record
Returns
void

Reimplemented from Monolog\Handler\AbstractProcessingHandler.

Definition at line 43 of file BrowserConsoleHandler.php.

44 {
45 // Accumulate records
46 static::$records[] = $record;
47
48 // Register shutdown handler if not already done
49 if (!static::$initialized) {
50 static::$initialized = true;
52 }
53 }
registerShutdownFunction()
Wrapper for register_shutdown_function to allow overriding.

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

+ 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 109 of file BrowserConsoleHandler.php.

110 {
111 echo $str;
112 }

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: