scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
115 office 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 namespace Monolog\Formatter;
12  
13 use Monolog\Logger;
14  
15 /**
16 * Formats incoming records into an HTML table
17 *
18 * This is especially useful for html email logging
19 *
20 * @author Tiago Brito <tlfbrito@gmail.com>
21 */
22 class HtmlFormatter extends NormalizerFormatter
23 {
24 /**
25 * Translates Monolog log levels to html color priorities.
26 */
27 protected $logLevels = array(
28 Logger::DEBUG => '#cccccc',
29 Logger::INFO => '#468847',
30 Logger::NOTICE => '#3a87ad',
31 Logger::WARNING => '#c09853',
32 Logger::ERROR => '#f0ad4e',
33 Logger::CRITICAL => '#FF7708',
34 Logger::ALERT => '#C12A19',
35 Logger::EMERGENCY => '#000000',
36 );
37  
38 /**
39 * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
40 */
41 public function __construct($dateFormat = null)
42 {
43 parent::__construct($dateFormat);
44 }
45  
46 /**
47 * Creates an HTML table row
48 *
49 * @param string $th Row header content
50 * @param string $td Row standard cell content
51 * @param bool $escapeTd false if td content must not be html escaped
52 * @return string
53 */
54 protected function addRow($th, $td = ' ', $escapeTd = true)
55 {
56 $th = htmlspecialchars($th, ENT_NOQUOTES, 'UTF-8');
57 if ($escapeTd) {
58 $td = '<pre>'.htmlspecialchars($td, ENT_NOQUOTES, 'UTF-8').'</pre>';
59 }
60  
61 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>";
62 }
63  
64 /**
65 * Create a HTML h1 tag
66 *
67 * @param string $title Text to be in the h1
68 * @param int $level Error level
69 * @return string
70 */
71 protected function addTitle($title, $level)
72 {
73 $title = htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8');
74  
75 return '<h1 style="background: '.$this->logLevels[$level].';color: #ffffff;padding: 5px;" class="monolog-output">'.$title.'</h1>';
76 }
77  
78 /**
79 * Formats a log record.
80 *
81 * @param array $record A record to format
82 * @return mixed The formatted record
83 */
84 public function format(array $record)
85 {
86 $output = $this->addTitle($record['level_name'], $record['level']);
87 $output .= '<table cellspacing="1" width="100%" class="monolog-output">';
88  
89 $output .= $this->addRow('Message', (string) $record['message']);
90 $output .= $this->addRow('Time', $record['datetime']->format($this->dateFormat));
91 $output .= $this->addRow('Channel', $record['channel']);
92 if ($record['context']) {
93 $embeddedTable = '<table cellspacing="1" width="100%">';
94 foreach ($record['context'] as $key => $value) {
95 $embeddedTable .= $this->addRow($key, $this->convertToString($value));
96 }
97 $embeddedTable .= '</table>';
98 $output .= $this->addRow('Context', $embeddedTable, false);
99 }
100 if ($record['extra']) {
101 $embeddedTable = '<table cellspacing="1" width="100%">';
102 foreach ($record['extra'] as $key => $value) {
103 $embeddedTable .= $this->addRow($key, $this->convertToString($value));
104 }
105 $embeddedTable .= '</table>';
106 $output .= $this->addRow('Extra', $embeddedTable, false);
107 }
108  
109 return $output.'</table>';
110 }
111  
112 /**
113 * Formats a set of log records.
114 *
115 * @param array $records A set of records to format
116 * @return mixed The formatted set of records
117 */
118 public function formatBatch(array $records)
119 {
120 $message = '';
121 foreach ($records as $record) {
122 $message .= $this->format($record);
123 }
124  
125 return $message;
126 }
127  
128 protected function convertToString($data)
129 {
130 if (null === $data || is_scalar($data)) {
131 return (string) $data;
132 }
133  
134 $data = $this->normalize($data);
135 if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
136 return json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
137 }
138  
139 return str_replace('\\/', '/', json_encode($data));
140 }
141 }