/wp-content/plugins/amazon-web-services/vendor/aws/Monolog/Formatter/LineFormatter.php

https://github.com/mhoofman/wordpress-heroku · PHP · 130 lines · 82 code · 23 blank · 25 comment · 12 complexity · 11bd5e65fdda33f24d4fe39f99b35f9e 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. /**
  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. /**
  40. * {@inheritdoc}
  41. */
  42. public function format(array $record)
  43. {
  44. $vars = parent::format($record);
  45. $output = $this->format;
  46. foreach ($vars['extra'] as $var => $val) {
  47. if (false !== strpos($output, '%extra.'.$var.'%')) {
  48. $output = str_replace('%extra.'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
  49. unset($vars['extra'][$var]);
  50. }
  51. }
  52. if ($this->ignoreEmptyContextAndExtra) {
  53. if (empty($vars['context'])) {
  54. unset($vars['context']);
  55. $output = str_replace('%context%', '', $output);
  56. }
  57. if (empty($vars['extra'])) {
  58. unset($vars['extra']);
  59. $output = str_replace('%extra%', '', $output);
  60. }
  61. }
  62. foreach ($vars as $var => $val) {
  63. if (false !== strpos($output, '%'.$var.'%')) {
  64. $output = str_replace('%'.$var.'%', $this->replaceNewlines($this->convertToString($val)), $output);
  65. }
  66. }
  67. return $output;
  68. }
  69. public function formatBatch(array $records)
  70. {
  71. $message = '';
  72. foreach ($records as $record) {
  73. $message .= $this->format($record);
  74. }
  75. return $message;
  76. }
  77. protected function normalizeException(Exception $e)
  78. {
  79. $previousText = '';
  80. if ($previous = $e->getPrevious()) {
  81. do {
  82. $previousText .= ', '.get_class($previous).': '.$previous->getMessage().' at '.$previous->getFile().':'.$previous->getLine();
  83. } while ($previous = $previous->getPrevious());
  84. }
  85. return '[object] ('.get_class($e).': '.$e->getMessage().' at '.$e->getFile().':'.$e->getLine().$previousText.')';
  86. }
  87. protected function convertToString($data)
  88. {
  89. if (null === $data || is_bool($data)) {
  90. return var_export($data, true);
  91. }
  92. if (is_scalar($data)) {
  93. return (string) $data;
  94. }
  95. if (version_compare(PHP_VERSION, '5.4.0', '>=')) {
  96. return $this->toJson($data, true);
  97. }
  98. return str_replace('\\/', '/', @json_encode($data));
  99. }
  100. protected function replaceNewlines($str)
  101. {
  102. if ($this->allowInlineLineBreaks) {
  103. return $str;
  104. }
  105. return strtr($str, array("\r\n" => ' ', "\r" => ' ', "\n" => ' '));
  106. }
  107. }