FingersCrossedHandler.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /**
  29. * @param callback|HandlerInterface $handler Handler or factory callback($record, $fingersCrossedHandler).
  30. * @param int $actionLevel The minimum logging level at which this handler will be triggered
  31. * @param int $bufferSize How many entries should be buffered at most, beyond that the oldest items are removed from the buffer.
  32. * @param Boolean $bubble Whether the messages that are handled can bubble up the stack or not
  33. */
  34. public function __construct($handler, $actionLevel = Logger::WARNING, $bufferSize = 0, $bubble = false)
  35. {
  36. $this->handler = $handler;
  37. $this->actionLevel = $actionLevel;
  38. $this->bufferSize = $bufferSize;
  39. $this->bubble = $bubble;
  40. }
  41. /**
  42. * {@inheritdoc}
  43. */
  44. public function handle(array $record)
  45. {
  46. if ($this->buffering) {
  47. $this->buffer[] = $record;
  48. if ($this->bufferSize > 0 && count($this->buffer) > $this->bufferSize) {
  49. array_shift($this->buffer);
  50. }
  51. if ($record['level'] >= $this->actionLevel) {
  52. $this->buffering = false;
  53. if (!$this->handler instanceof HandlerInterface) {
  54. $this->handler = call_user_func($this->handler, $record, $this);
  55. }
  56. if (!$this->handler instanceof HandlerInterface) {
  57. throw new \RuntimeException("The factory callback should return a HandlerInterface");
  58. }
  59. foreach ($this->buffer as $record) {
  60. $this->handler->handle($record);
  61. }
  62. $this->buffer = array();
  63. }
  64. } else {
  65. $this->handler->handle($record);
  66. }
  67. return false === $this->bubble;
  68. }
  69. /**
  70. * Resets the state of the handler. Stops forwarding records to the wrapped handler.
  71. */
  72. public function reset()
  73. {
  74. $this->buffering = true;
  75. }
  76. /**
  77. * Implemented to comply with the AbstractHandler requirements. Can not be called.
  78. */
  79. public function write(array $record)
  80. {
  81. throw new \BadMethodCallException('This method should not be called directly on the FingersCrossedHandler.');
  82. }
  83. }