FingersCrossedHandler.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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;
  11. use Monolog\Logger;
  12. /**
  13. * Buffers all records until a certain level is reached
  14. *
  15. * The advantage of this approach is that you don't get any clutter in your log files.
  16. * Only requests which actually trigger an error (or whatever your actionLevel is) will be
  17. * in the logs, but they will contain all records, not only those above the level threshold.
  18. *
  19. * @author Jordi Boggiano <j.boggiano@seld.be>
  20. */
  21. class FingersCrossedHandler extends AbstractHandler
  22. {
  23. protected $handler;
  24. protected $actionLevel;
  25. protected $buffering = true;
  26. protected $bufferSize;
  27. protected $buffer = array();
  28. protected $stopBuffering;
  29. /**
  30. * @param callback|HandlerInterface $handler Handler or factory callback($record, $fingersCrossedHandler).
  31. * @param int $actionLevel The minimum logging level at which this handler will be triggered
  32. * @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
  33. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  34. * @param Boolean $stopBuffering Whether the handler should stop buffering after being triggered (default true)
  35. */
  36. public function __construct($handler, $actionLevel = Logger::WARNING, $bufferSize = 0, $bubble = true, $stopBuffering = true)
  37. {
  38. $this->handler = $handler;
  39. $this->actionLevel = $actionLevel;
  40. $this->bufferSize = $bufferSize;
  41. $this->bubble = $bubble;
  42. $this->stopBuffering = $stopBuffering;
  43. }
  44. /**
  45. * {@inheritdoc}
  46. */
  47. public function isHandling(array $record)
  48. {
  49. return true;
  50. }
  51. /**
  52. * {@inheritdoc}
  53. */
  54. public function handle(array $record)
  55. {
  56. if ($this->buffering) {
  57. $this->buffer[] = $record;
  58. if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
  59. array_shift($this->buffer);
  60. }
  61. if ($record['level'] >= $this->actionLevel) {
  62. if ($this->stopBuffering) {
  63. $this->buffering = false;
  64. }
  65. if (!$this->handler instanceof HandlerInterface) {
  66. $this->handler = call_user_func($this->handler, $record, $this);
  67. }
  68. if (!$this->handler instanceof HandlerInterface) {
  69. throw new \RuntimeException("The factory callback should return a HandlerInterface");
  70. }
  71. $this->handler->handleBatch($this->buffer);
  72. $this->buffer = array();
  73. }
  74. } else {
  75. $this->handler->handle($record);
  76. }
  77. return false === $this->bubble;
  78. }
  79. /**
  80. * Resets the state of the handler. Stops forwarding records to the wrapped handler.
  81. */
  82. public function reset()
  83. {
  84. $this->buffering = true;
  85. }
  86. }