HtmlFormatter.php 4.4 KB

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