| 1 |
<?php |
| 2 |
/* |
| 3 |
* This file is part of the Monolog package. |
| 4 |
* |
| 5 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
| 6 |
* |
| 7 |
* For the full copyright and license information, please view the LICENSE |
| 8 |
* file that was distributed with this source code. |
| 9 |
*/ |
| 10 |
|
| 11 |
/** |
| 12 |
* Formats incoming records into an HTML table |
| 13 |
* |
| 14 |
* This is especially useful for html email logging |
| 15 |
* |
| 16 |
* @author Tiago Brito <tlfbrito@gmail.com> |
| 17 |
*/ |
| 18 |
class Monolog_Formatter_HtmlFormatter extends Monolog_Formatter_NormalizerFormatter |
| 19 |
{ |
| 20 |
/** |
| 21 |
* Translates Monolog log levels to html color priorities. |
| 22 |
*/ |
| 23 |
private $logLevels = array( |
| 24 |
Monolog_Logger::DEBUG => '#cccccc', |
| 25 |
Monolog_Logger::INFO => '#468847', |
| 26 |
Monolog_Logger::NOTICE => '#3a87ad', |
| 27 |
Monolog_Logger::WARNING => '#c09853', |
| 28 |
Monolog_Logger::ERROR => '#f0ad4e', |
| 29 |
Monolog_Logger::CRITICAL => '#FF7708', |
| 30 |
Monolog_Logger::ALERT => '#C12A19', |
| 31 |
Monolog_Logger::EMERGENCY => '#000000', |
| 32 |
); |
| 33 |
|
| 34 |
/** |
| 35 |
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format |
| 36 |
*/ |
| 37 |
public function __construct($dateFormat = null) |
| 38 |
{ |
| 39 |
parent::__construct($dateFormat); |
| 40 |
} |
| 41 |
|
| 42 |
/** |
| 43 |
* Creates an HTML table row |
| 44 |
* |
| 45 |
* @param string $th Row header content |
| 46 |
* @param string $td Row standard cell content |
| 47 |
* |
| 48 |
* @return string |
| 49 |
*/ |
| 50 |
private function addRow($th, $td = ' ') |
| 51 |
{ |
| 52 |
$th = htmlspecialchars($th, ENT_NOQUOTES, 'UTF-8'); |
| 53 |
$td = '<pre>'.htmlspecialchars($td, ENT_NOQUOTES, 'UTF-8').'</pre>'; |
| 54 |
|
| 55 |
return "<tr style=\"padding: 4px;spacing: 0;text-align: left;\">\n<th style=\"background: #cccccc\" width=\"100px\">$th:</th>\n<td style=\"padding: 4px;spacing: 0;text-align: left;background: #eeeeee\">".$td."</td>\n</tr>"; |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Create a HTML h1 tag |
| 60 |
* |
| 61 |
* @param string $title Text to be in the h1 |
| 62 |
* @param integer $level Error level |
| 63 |
* |
| 64 |
* @return string |
| 65 |
*/ |
| 66 |
private function addTitle($title, $level) |
| 67 |
{ |
| 68 |
$title = htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8'); |
| 69 |
|
| 70 |
return '<h1 style="background: '.$this->logLevels[$level].';color: #ffffff;padding: 5px;">'.$title.'</h1>'; |
| 71 |
} |
| 72 |
|
| 73 |
/** |
| 74 |
* Formats a log record. |
| 75 |
* |
| 76 |
* @param array $record A record to format |
| 77 |
* |
| 78 |
* @return mixed The formatted record |
| 79 |
*/ |
| 80 |
public function format(array $record) |
| 81 |
{ |
| 82 |
$output = $this->addTitle($record['level_name'], $record['level']); |
| 83 |
$output .= '<table cellspacing="1" width="100%">'; |
| 84 |
|
| 85 |
$output .= $this->addRow('Message', (string) $record['message']); |
| 86 |
$output .= $this->addRow('Time', $record['datetime']->format('Y-m-d\TH:i:s.uO')); |
| 87 |
$output .= $this->addRow('Channel', $record['channel']); |
| 88 |
if ($record['context']) { |
| 89 |
$output .= $this->addRow('Context', $this->convertToString($record['context'])); |
| 90 |
} |
| 91 |
if ($record['extra']) { |
| 92 |
$output .= $this->addRow('Extra', $this->convertToString($record['extra'])); |
| 93 |
} |
| 94 |
|
| 95 |
return $output.'</table>'; |
| 96 |
} |
| 97 |
|
| 98 |
/** |
| 99 |
* Formats a set of log records. |
| 100 |
* |
| 101 |
* @param array $records A set of records to format |
| 102 |
* |
| 103 |
* @return mixed The formatted set of records |
| 104 |
*/ |
| 105 |
public function formatBatch(array $records) |
| 106 |
{ |
| 107 |
$message = ''; |
| 108 |
foreach ($records as $record) { |
| 109 |
$message .= $this->format($record); |
| 110 |
} |
| 111 |
|
| 112 |
return $message; |
| 113 |
} |
| 114 |
|
| 115 |
protected function convertToString($data) |
| 116 |
{ |
| 117 |
if (null === $data || is_scalar($data)) { |
| 118 |
return (string) $data; |
| 119 |
} |
| 120 |
|
| 121 |
$data = $this->normalize($data); |
| 122 |
if (version_compare(PHP_VERSION, '5.4.0', '>=')) { |
| 123 |
return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE); |
| 124 |
} |
| 125 |
|
| 126 |
return str_replace('\\/', '/', json_encode($data)); |
| 127 |
} |
| 128 |
} |
| 129 |
|