Aucune description

ChannelLevelActivationStrategy.php 1.9KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. <?php
  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\FingersCrossed;
  11. use Monolog\Logger;
  12. /**
  13. * Channel and Error level based monolog activation strategy. Allows to trigger activation
  14. * based on level per channel. e.g. trigger activation on level 'ERROR' by default, except
  15. * for records of the 'sql' channel; those should trigger activation on level 'WARN'.
  16. *
  17. * Example:
  18. *
  19. * <code>
  20. * $activationStrategy = new ChannelLevelActivationStrategy(
  21. * Logger::CRITICAL,
  22. * array(
  23. * 'request' => Logger::ALERT,
  24. * 'sensitive' => Logger::ERROR,
  25. * )
  26. * );
  27. * $handler = new FingersCrossedHandler(new StreamHandler('php://stderr'), $activationStrategy);
  28. * </code>
  29. *
  30. * @author Mike Meessen <netmikey@gmail.com>
  31. */
  32. class ChannelLevelActivationStrategy implements ActivationStrategyInterface
  33. {
  34. private $defaultActionLevel;
  35. private $channelToActionLevel;
  36. /**
  37. * @param int $defaultActionLevel The default action level to be used if the record's category doesn't match any
  38. * @param array $channelToActionLevel An array that maps channel names to action levels.
  39. */
  40. public function __construct($defaultActionLevel, $channelToActionLevel = array())
  41. {
  42. $this->defaultActionLevel = Logger::toMonologLevel($defaultActionLevel);
  43. $this->channelToActionLevel = array_map('Monolog\Logger::toMonologLevel', $channelToActionLevel);
  44. }
  45. public function isHandlerActivated(array $record)
  46. {
  47. if (isset($this->channelToActionLevel[$record['channel']])) {
  48. return $record['level'] >= $this->channelToActionLevel[$record['channel']];
  49. }
  50. return $record['level'] >= $this->defaultActionLevel;
  51. }
  52. }