AbstractProcessingHandler.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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\LogRecord;
  12. /**
  13. * Base Handler class providing the Handler structure, including processors and formatters
  14. *
  15. * Classes extending it should (in most cases) only implement write($record)
  16. *
  17. * @author Jordi Boggiano <j.boggiano@seld.be>
  18. * @author Christophe Coevoet <stof@notk.org>
  19. *
  20. * @phpstan-import-type LevelName from \Monolog\Logger
  21. * @phpstan-import-type Level from \Monolog\Logger
  22. */
  23. abstract class AbstractProcessingHandler extends AbstractHandler implements ProcessableHandlerInterface, FormattableHandlerInterface
  24. {
  25. use ProcessableHandlerTrait;
  26. use FormattableHandlerTrait;
  27. /**
  28. * {@inheritDoc}
  29. */
  30. public function handle(LogRecord $record): bool
  31. {
  32. if (!$this->isHandling($record)) {
  33. return false;
  34. }
  35. if ($this->processors) {
  36. $record = $this->processRecord($record);
  37. }
  38. $record->formatted = $this->getFormatter()->format($record);
  39. $this->write($record);
  40. return false === $this->bubble;
  41. }
  42. /**
  43. * Writes the (already formatted) record down to the log of the implementing handler
  44. */
  45. abstract protected function write(LogRecord $record): void;
  46. /**
  47. * @return void
  48. */
  49. public function reset()
  50. {
  51. parent::reset();
  52. $this->resetProcessors();
  53. }
  54. }