PsrHandler.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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\Logger;
  12. use Psr\Log\LoggerInterface;
  13. /**
  14. * Proxies log messages to an existing PSR-3 compliant logger.
  15. *
  16. * @author Michael Moussa <michael.moussa@gmail.com>
  17. */
  18. class PsrHandler extends AbstractHandler
  19. {
  20. /**
  21. * PSR-3 compliant logger
  22. *
  23. * @var LoggerInterface
  24. */
  25. protected $logger;
  26. /**
  27. * @param LoggerInterface $logger The underlying PSR-3 compliant logger to which messages will be proxied
  28. * @param int $level The minimum logging level at which this handler will be triggered
  29. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  30. */
  31. public function __construct(LoggerInterface $logger, $level = Logger::DEBUG, $bubble = true)
  32. {
  33. parent::__construct($level, $bubble);
  34. $this->logger = $logger;
  35. }
  36. /**
  37. * {@inheritDoc}
  38. */
  39. public function handle(array $record): bool
  40. {
  41. if (!$this->isHandling($record)) {
  42. return false;
  43. }
  44. $this->logger->log(strtolower($record['level_name']), $record['message'], $record['context']);
  45. return false === $this->bubble;
  46. }
  47. }