"Fossies" - the Fresh Open Source Software Archive

Member "grav/vendor/monolog/monolog/src/Monolog/Handler/AbstractHandler.php" (8 Jun 2020, 4378 Bytes) of package /linux/www/grav-v1.6.26.zip:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) PHP source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "AbstractHandler.php" see the Fossies "Dox" file reference documentation.

    1 <?php
    2 
    3 /*
    4  * This file is part of the Monolog package.
    5  *
    6  * (c) Jordi Boggiano <j.boggiano@seld.be>
    7  *
    8  * For the full copyright and license information, please view the LICENSE
    9  * file that was distributed with this source code.
   10  */
   11 
   12 namespace Monolog\Handler;
   13 
   14 use Monolog\Formatter\FormatterInterface;
   15 use Monolog\Formatter\LineFormatter;
   16 use Monolog\Logger;
   17 use Monolog\ResettableInterface;
   18 
   19 /**
   20  * Base Handler class providing the Handler structure
   21  *
   22  * @author Jordi Boggiano <j.boggiano@seld.be>
   23  */
   24 abstract class AbstractHandler implements HandlerInterface, ResettableInterface
   25 {
   26     protected $level = Logger::DEBUG;
   27     protected $bubble = true;
   28 
   29     /**
   30      * @var FormatterInterface
   31      */
   32     protected $formatter;
   33     protected $processors = array();
   34 
   35     /**
   36      * @param int  $level  The minimum logging level at which this handler will be triggered
   37      * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
   38      */
   39     public function __construct($level = Logger::DEBUG, $bubble = true)
   40     {
   41         $this->setLevel($level);
   42         $this->bubble = $bubble;
   43     }
   44 
   45     /**
   46      * {@inheritdoc}
   47      */
   48     public function isHandling(array $record)
   49     {
   50         return $record['level'] >= $this->level;
   51     }
   52 
   53     /**
   54      * {@inheritdoc}
   55      */
   56     public function handleBatch(array $records)
   57     {
   58         foreach ($records as $record) {
   59             $this->handle($record);
   60         }
   61     }
   62 
   63     /**
   64      * Closes the handler.
   65      *
   66      * This will be called automatically when the object is destroyed
   67      */
   68     public function close()
   69     {
   70     }
   71 
   72     /**
   73      * {@inheritdoc}
   74      */
   75     public function pushProcessor($callback)
   76     {
   77         if (!is_callable($callback)) {
   78             throw new \InvalidArgumentException('Processors must be valid callables (callback or object with an __invoke method), '.var_export($callback, true).' given');
   79         }
   80         array_unshift($this->processors, $callback);
   81 
   82         return $this;
   83     }
   84 
   85     /**
   86      * {@inheritdoc}
   87      */
   88     public function popProcessor()
   89     {
   90         if (!$this->processors) {
   91             throw new \LogicException('You tried to pop from an empty processor stack.');
   92         }
   93 
   94         return array_shift($this->processors);
   95     }
   96 
   97     /**
   98      * {@inheritdoc}
   99      */
  100     public function setFormatter(FormatterInterface $formatter)
  101     {
  102         $this->formatter = $formatter;
  103 
  104         return $this;
  105     }
  106 
  107     /**
  108      * {@inheritdoc}
  109      */
  110     public function getFormatter()
  111     {
  112         if (!$this->formatter) {
  113             $this->formatter = $this->getDefaultFormatter();
  114         }
  115 
  116         return $this->formatter;
  117     }
  118 
  119     /**
  120      * Sets minimum logging level at which this handler will be triggered.
  121      *
  122      * @param  int|string $level Level or level name
  123      * @return self
  124      */
  125     public function setLevel($level)
  126     {
  127         $this->level = Logger::toMonologLevel($level);
  128 
  129         return $this;
  130     }
  131 
  132     /**
  133      * Gets minimum logging level at which this handler will be triggered.
  134      *
  135      * @return int
  136      */
  137     public function getLevel()
  138     {
  139         return $this->level;
  140     }
  141 
  142     /**
  143      * Sets the bubbling behavior.
  144      *
  145      * @param  bool $bubble true means that this handler allows bubbling.
  146      *                      false means that bubbling is not permitted.
  147      * @return self
  148      */
  149     public function setBubble($bubble)
  150     {
  151         $this->bubble = $bubble;
  152 
  153         return $this;
  154     }
  155 
  156     /**
  157      * Gets the bubbling behavior.
  158      *
  159      * @return bool true means that this handler allows bubbling.
  160      *              false means that bubbling is not permitted.
  161      */
  162     public function getBubble()
  163     {
  164         return $this->bubble;
  165     }
  166 
  167     public function __destruct()
  168     {
  169         try {
  170             $this->close();
  171         } catch (\Exception $e) {
  172             // do nothing
  173         } catch (\Throwable $e) {
  174             // do nothing
  175         }
  176     }
  177 
  178     public function reset()
  179     {
  180         foreach ($this->processors as $processor) {
  181             if ($processor instanceof ResettableInterface) {
  182                 $processor->reset();
  183             }
  184         }
  185     }
  186 
  187     /**
  188      * Gets the default formatter.
  189      *
  190      * @return FormatterInterface
  191      */
  192     protected function getDefaultFormatter()
  193     {
  194         return new LineFormatter();
  195     }
  196 }