LineFormatter.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. /**
  12. * Formats incoming records into a one-line string
  13. *
  14. * This is especially useful for logging to files
  15. *
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. * @author Christophe Coevoet <stof@notk.org>
  18. */
  19. class LineFormatter extends NormalizerFormatter
  20. {
  21. const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
  22. protected $format;
  23. protected $allowInlineLineBreaks;
  24. protected $ignoreEmptyContextAndExtra;
  25. protected $includeStacktraces;
  26. /**
  27. * @param string $format The format of the message
  28. * @param string $dateFormat The format of the timestamp: one supported by DateTime::format
  29. * @param bool $allowInlineLineBreaks Whether to allow inline line breaks in log entries
  30. * @param bool $ignoreEmptyContextAndExtra
  31. */
  32. public function __construct($format = null, $dateFormat = null, $allowInlineLineBreaks = false, $ignoreEmptyContextAndExtra = false)
  33. {
  34. $this->format = $format ?: static::SIMPLE_FORMAT;
  35. $this->allowInlineLineBreaks = $allowInlineLineBreaks;
  36. $this->ignoreEmptyContextAndExtra = $ignoreEmptyContextAndExtra;
  37. parent::__construct($dateFormat);
  38. }
  39. public function includeStacktraces($include = true)
  40. {
  41. $this->includeStacktraces = $include;
  42. if ($this->includeStacktraces) {
  43. $this->allowInlineLineBreaks = true;
  44. }
  45. }
  46. public function allowInlineLineBreaks($allow = true)
  47. {
  48. $this->allowInlineLineBreaks = $allow;
  49. }
  50. public function ignoreEmptyContextAndExtra($ignore = true)
  51. {
  52. $this->ignoreEmptyContextAndExtra = $ignore;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function format(array $record)
  58. {
  59. $vars = parent::format($record);
  60. $output = $this->format;
  61. foreach ($vars['extra'] as $var => $val) {
  62. if (false !== strpos($output, '%extra.'.$var.'%')) {
  63. $output = str_replace('%extra.'.$var.'%', $this->stringify($val), $output);
  64. unset($vars['extra'][$var]);
  65. }
  66. }
  67. foreach ($vars['context'] as $var => $val) {
  68. if (false !== strpos($output, '%context.'.$var.'%')) {
  69. $output = str_replace('%context.'.$var.'%', $this->stringify($val), $output);
  70. unset($vars['context'][$var]);
  71. }
  72. }
  73. if ($this->ignoreEmptyContextAndExtra) {
  74. if (empty($vars['context'])) {
  75. unset($vars['context']);
  76. $output = str_replace('%context%', '', $output);
  77. }
  78. if (empty($vars['extra'])) {
  79. unset($vars['extra']);
  80. $output = str_replace('%extra%', '', $output);
  81. }
  82. }
  83. foreach ($vars as $var => $val) {
  84. if (false !== strpos($output, '%'.$var.'%')) {
  85. $output = str_replace('%'.$var.'%', $this->stringify($val), $output);
  86. }
  87. }
  88. return $output;
  89. }
  90. public function formatBatch(array $records)
  91. {
  92. $message = '';
  93. foreach ($records as $record) {
  94. $message .= $this->format($record);
  95. }
  96. return $message;
  97. }
  98. public function stringify($value)
  99. {
  100. return $this->replaceNewlines($this->convertToString($value));
  101. }
  102. protected function normalizeException($e)
  103. {
  104. // TODO 2.0 only check for Throwable
  105. if (!$e instanceof \Exception && !$e instanceof \Throwable) {
  106. throw new \InvalidArgumentException('Exception/Throwable expected, got '.gettype($e).' / '.get_class($e));
  107. }
  108. $previousText = '';
  109. if ($previous = $e->getPrevious()) {
  110. do {
  111. $previousText .= ', '.get_class($previous).'(code: '.$previous->getCode().'): '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
  112. } while ($previous = $previous->getPrevious());
  113. }
  114. $str = '[object] ('.get_class($e).'(code: '.$e->getCode().'): '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
  115. if ($this->includeStacktraces) {
  116. $str .= "\n[stacktrace]\n".$e->getTraceAsString();
  117. }
  118. return $str;
  119. }
  120. protected function convertToString($data)
  121. {
  122. if (null === $data || is_bool($data)) {
  123. return var_export($data, true);
  124. }
  125. if (is_scalar($data)) {
  126. return (string) $data;
  127. }
  128. return $this->toJson($data, true);
  129. }
  130. protected function replaceNewlines($str)
  131. {
  132. if ($this->allowInlineLineBreaks) {
  133. return $str;
  134. }
  135. return str_replace(array("\r\n", "\r", "\n"), ' ', $str);
  136. }
  137. }