/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php

https://gitlab.com/judielsm/Handora · PHP · 159 lines · 106 code · 28 blank · 25 comment · 14 complexity · aa5d7736bc7e9c03bd4cb8a6de05c745 MD5 · raw file

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