HtmlFormatter.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php declare(strict_types=1);
  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. use Monolog\Utils;
  13. /**
  14. * Formats incoming records into an HTML table
  15. *
  16. * This is especially useful for html email logging
  17. *
  18. * @author Tiago Brito <tlfbrito@gmail.com>
  19. */
  20. class HtmlFormatter extends NormalizerFormatter
  21. {
  22. /**
  23. * Translates Monolog log levels to html color priorities.
  24. */
  25. protected $logLevels = [
  26. Logger::DEBUG => '#cccccc',
  27. Logger::INFO => '#468847',
  28. Logger::NOTICE => '#3a87ad',
  29. Logger::WARNING => '#c09853',
  30. Logger::ERROR => '#f0ad4e',
  31. Logger::CRITICAL => '#FF7708',
  32. Logger::ALERT => '#C12A19',
  33. Logger::EMERGENCY => '#000000',
  34. ];
  35. /**
  36. * @param string|null $dateFormat The format of the timestamp: one supported by DateTime::format
  37. */
  38. public function __construct(?string $dateFormat = null)
  39. {
  40. parent::__construct($dateFormat);
  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. * @param bool $escapeTd false if td content must not be html escaped
  48. */
  49. protected function addRow(string $th, string $td = ' ', bool $escapeTd = true): string
  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;text-align: left;\">\n<th style=\"vertical-align: top;background: #ccc;color: #000\" width=\"100\">$th:</th>\n<td style=\"padding: 4px;text-align: left;vertical-align: top;background: #eee;color: #000\">".$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(string $title, int $level): string
  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 string The formatted record
  74. */
  75. public function format(array $record): string
  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', $this->formatDate($record['datetime']));
  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((string)$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((string)$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 string The formatted set of records
  105. */
  106. public function formatBatch(array $records): string
  107. {
  108. $message = '';
  109. foreach ($records as $record) {
  110. $message .= $this->format($record);
  111. }
  112. return $message;
  113. }
  114. protected function convertToString($data): string
  115. {
  116. if (null === $data || is_scalar($data)) {
  117. return (string) $data;
  118. }
  119. $data = $this->normalize($data);
  120. return Utils::jsonEncode($data, JSON_PRETTY_PRINT | Utils::DEFAULT_JSON_FLAGS, true);
  121. }
  122. }