1<?php declare(strict_types=1); 2 3/* 4 * This file is part of the Monolog package. 5 * 6 * (c) Jordi Boggiano <j.boggiano@seld.be> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace Monolog\Handler; 13 14use Monolog\Formatter\FormatterInterface; 15use Monolog\Formatter\LineFormatter; 16 17/** 18 * Helper trait for implementing FormattableInterface 19 * 20 * @author Jordi Boggiano <j.boggiano@seld.be> 21 */ 22trait FormattableHandlerTrait 23{ 24 /** 25 * @var ?FormatterInterface 26 */ 27 protected $formatter; 28 29 /** 30 * {@inheritDoc} 31 */ 32 public function setFormatter(FormatterInterface $formatter): HandlerInterface 33 { 34 $this->formatter = $formatter; 35 36 return $this; 37 } 38 39 /** 40 * {@inheritDoc} 41 */ 42 public function getFormatter(): FormatterInterface 43 { 44 if (!$this->formatter) { 45 $this->formatter = $this->getDefaultFormatter(); 46 } 47 48 return $this->formatter; 49 } 50 51 /** 52 * Gets the default formatter. 53 * 54 * Overwrite this if the LineFormatter is not a good default for your handler. 55 */ 56 protected function getDefaultFormatter(): FormatterInterface 57 { 58 return new LineFormatter(); 59 } 60} 61