LineFormatter.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 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 implements FormatterInterface
  21. {
  22. const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n";
  23. const SIMPLE_DATE = "Y-m-d H:i:s";
  24. protected $format;
  25. protected $dateFormat;
  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. */
  30. public function __construct($format = null, $dateFormat = null)
  31. {
  32. $this->format = $format ?: static::SIMPLE_FORMAT;
  33. $this->dateFormat = $dateFormat ?: static::SIMPLE_DATE;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function format(array $record)
  39. {
  40. $vars = $record;
  41. $vars['datetime'] = $vars['datetime']->format($this->dateFormat);
  42. $output = $this->format;
  43. foreach ($vars['extra'] as $var => $val) {
  44. if (false !== strpos($output, '%extra.'.$var.'%')) {
  45. $output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output);
  46. unset($vars['extra'][$var]);
  47. }
  48. }
  49. foreach ($vars as $var => $val) {
  50. $output = str_replace('%'.$var.'%', $this->convertToString($val), $output);
  51. }
  52. return $output;
  53. }
  54. public function formatBatch(array $records)
  55. {
  56. $message = '';
  57. foreach ($records as $record) {
  58. $message .= $this->format($record);
  59. }
  60. return $message;
  61. }
  62. protected function convertToString($data)
  63. {
  64. if (null === $data || is_scalar($data)) {
  65. return (string) $data;
  66. }
  67. return stripslashes(json_encode($this->normalize($data)));
  68. }
  69. protected function normalize($data)
  70. {
  71. if (null === $data || is_scalar($data)) {
  72. return $data;
  73. }
  74. if (is_array($data) || $data instanceof \Traversable) {
  75. $normalized = array();
  76. foreach ($data as $key => $value) {
  77. $normalized[$key] = $this->normalize($value);
  78. }
  79. return $normalized;
  80. }
  81. if (is_resource($data)) {
  82. return '[resource]';
  83. }
  84. return sprintf("[object] (%s: %s)", get_class($data), json_encode($data));
  85. }
  86. }