FormattableHandlerTrait.php 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  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. * This trait is present in monolog 1.x to ease forward compatibility.
  17. *
  18. * @author Jordi Boggiano <j.boggiano@seld.be>
  19. */
  20. trait FormattableHandlerTrait
  21. {
  22. /**
  23. * @var FormatterInterface
  24. */
  25. protected $formatter;
  26. /**
  27. * {@inheritdoc}
  28. * @suppress PhanTypeMismatchReturn
  29. */
  30. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  31. {
  32. $this->formatter = $formatter;
  33. return $this;
  34. }
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public function getFormatter(): FormatterInterface
  39. {
  40. if (!$this->formatter) {
  41. $this->formatter = $this->getDefaultFormatter();
  42. }
  43. return $this->formatter;
  44. }
  45. /**
  46. * Gets the default formatter.
  47. *
  48. * Overwrite this if the LineFormatter is not a good default for your handler.
  49. */
  50. protected function getDefaultFormatter(): FormatterInterface
  51. {
  52. return new LineFormatter();
  53. }
  54. }