Bläddra i källkod

Tweak html output a bit for readability, update readme, force unicode, refs #275

Jordi Boggiano 12 år sedan
förälder
incheckning
11cb918bbc
2 ändrade filer med 17 tillägg och 13 borttagningar
  1. 1 0
      README.mdown
  2. 16 13
      src/Monolog/Formatter/HtmlFormatter.php

+ 1 - 0
README.mdown

@@ -178,6 +178,7 @@ Formatters
 ----------
 
 - _LineFormatter_: Formats a log record into a one-line string.
+- _HtmlFormatter_: Used to format log records into a human readable html table, mainly suitable for emails.
 - _NormalizerFormatter_: Normalizes objects/resources down to strings so a record can easily be serialized/encoded.
 - _ScalarFormatter_: Used to format log records into an associative array of scalar values.
 - _JsonFormatter_: Encodes a log record into json.

+ 16 - 13
src/Monolog/Formatter/HtmlEmailFormatter.php → src/Monolog/Formatter/HtmlFormatter.php

@@ -11,13 +11,13 @@
 namespace Monolog\Formatter;
 
 /**
- * Formats incoming records into a HTML table
+ * Formats incoming records into an HTML table
  *
  * This is especially useful for html email logging
  *
  * @author Tiago Brito <tlfbrito@gmail.com>
  */
-class HtmlEmailFormatter extends NormalizerFormatter
+class HtmlFormatter extends NormalizerFormatter
 {
     /**
      * Translates Monolog log levels to html color priorities.
@@ -50,8 +50,8 @@ class HtmlEmailFormatter extends NormalizerFormatter
      */
     private function addRow($th, $td = ' ')
     {
-        $th = htmlspecialchars($th);
-        $td = '<pre>'.htmlspecialchars($td).'</pre>';
+        $th = htmlspecialchars($th, ENT_NOQUOTES, 'UTF-8');
+        $td = '<pre>'.htmlspecialchars($td, ENT_NOQUOTES, 'UTF-8').'</pre>';
 
         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>";
     }
@@ -65,8 +65,8 @@ class HtmlEmailFormatter extends NormalizerFormatter
      */
     private function addTitle($title, $level)
     {
-        $title = htmlspecialchars($title);
-     
+        $title = htmlspecialchars($title, ENT_NOQUOTES, 'UTF-8');
+
         return '<h1 style="background: '.$this->logLevels[$level].';color: #ffffff;padding: 5px;">'.$title.'</h1>';
     }
     /**
@@ -77,15 +77,18 @@ class HtmlEmailFormatter extends NormalizerFormatter
      */
     public function format(array $record)
     {
-        $output = $this->addTitle($this->convertToString($record['level_name']), $record['level']);
+        $output = $this->addTitle($record['level_name'], $record['level']);
         $output .= '<table cellspacing="1" width="100%">';
 
-        $output .= $this->addRow('Message', $this->convertToString($record['message']));
-        $output .= $this->addRow('Generated at', $this->convertToString($record['datetime']));
-        $output .= $this->addRow('Level', $this->convertToString($record['level']));
-        $output .= $this->addRow('Channel', $this->convertToString($record['channel']));
-        $output .= $this->addRow('Context', $this->convertToString($record['context']));
-        $output .= $this->addRow('Extra', $this->convertToString($record['extra']));
+        $output .= $this->addRow('Message', (string) $record['message']);
+        $output .= $this->addRow('Time', $record['datetime']->format('Y-m-d\TH:i:s.uO'));
+        $output .= $this->addRow('Channel', $record['channel']);
+        if ($record['context']) {
+            $output .= $this->addRow('Context', $this->convertToString($record['context']));
+        }
+        if ($record['extra']) {
+            $output .= $this->addRow('Extra', $this->convertToString($record['extra']));
+        }
 
         return $output.'</table>';
     }