FormattableHandlerTrait.php 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\Handler;
  11. use Monolog\Formatter\FormatterInterface;
  12. use Monolog\Formatter\LineFormatter;
  13. /**
  14. * Helper trait for implementing FormattableInterface
  15. *
  16. * @author Jordi Boggiano <j.boggiano@seld.be>
  17. */
  18. trait FormattableHandlerTrait
  19. {
  20. /**
  21. * @var ?FormatterInterface
  22. */
  23. protected $formatter;
  24. /**
  25. * {@inheritdoc}
  26. */
  27. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  28. {
  29. $this->formatter = $formatter;
  30. return $this;
  31. }
  32. /**
  33. * {@inheritdoc}
  34. */
  35. public function getFormatter(): FormatterInterface
  36. {
  37. if (!$this->formatter) {
  38. $this->formatter = $this->getDefaultFormatter();
  39. }
  40. return $this->formatter;
  41. }
  42. /**
  43. * Gets the default formatter.
  44. *
  45. * Overwrite this if the LineFormatter is not a good default for your handler.
  46. */
  47. protected function getDefaultFormatter(): FormatterInterface
  48. {
  49. return new LineFormatter();
  50. }
  51. }