Plop
A simple logging library for PHP
HandlerAbstract.php
1 <?php
2 /*
3  This file is part of Plop, a simple logging library for PHP.
4 
5  Copyright © 2010-2014 François Poirotte
6 
7  Plop is free software: you can redistribute it and/or modify
8  it under the terms of the GNU General Public License as published by
9  the Free Software Foundation, either version 3 of the License, or
10  (at your option) any later version.
11 
12  Plop is distributed in the hope that it will be useful,
13  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16 
17  You should have received a copy of the GNU General Public License
18  along with Plop. If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 namespace Plop;
22 
30 abstract class HandlerAbstract implements \Plop\HandlerInterface
31 {
33  protected $formatter;
34 
36  protected $filters;
37 
50  public function __construct(
51  \Plop\FormatterInterface $formatter = null,
52  \Plop\FiltersCollectionAbstract $filters = null
53  ) {
54  if ($formatter === null) {
55  $formatter = new \Plop\Formatter();
56  }
57  if ($filters === null) {
58  $filters = new \Plop\FiltersCollection();
59  }
60  $this->setFormatter($formatter);
61  $this->filters = $filters;
62  }
63 
65  public function getFormatter()
66  {
67  return $this->formatter;
68  }
69 
71  public function setFormatter(\Plop\FormatterInterface $formatter)
72  {
73  $this->formatter = $formatter;
74  return $this;
75  }
76 
78  public function getFilters()
79  {
80  return $this->filters;
81  }
82 
84  public function setFilters(\Plop\FiltersCollectionAbstract $filters)
85  {
86  $this->filters = $filters;
87  return $this;
88  }
89 
99  protected function format(\Plop\RecordInterface $record)
100  {
101  return $this->formatter->format($record);
102  }
103 
113  abstract protected function emit(\Plop\RecordInterface $record);
114 
116  public function handle(\Plop\RecordInterface $record)
117  {
118  $rv = $this->format($record);
119  if ($rv) {
120  $this->emit($record);
121  }
122  return $this;
123  }
124 
135  protected function getStderr()
136  {
137  return fopen('php://stderr', 'at');
138  }
139 
141  public function handleError(
142  \Plop\RecordInterface $record,
143  \Exception $exception
144  ) {
145  $stderr = $this->getStderr();
146  fprintf($stderr, "%s\n", $exception);
147  fclose($stderr);
148  return $this;
149  }
150 }
handle(\Plop\RecordInterface $record)
An abstract class that can be used as a base to create new log records handlers.
Interface for a log record.
$filters
An object handling a collection of filters.
Interface for a class capable of handling log records.
Interface to a record formatter.
format(\Plop\RecordInterface $record)
handleError(\Plop\RecordInterface $record,\Exception $exception)
setFormatter(\Plop\FormatterInterface $formatter)
Abstract class for a collection of filters.
setFilters(\Plop\FiltersCollectionAbstract $filters)
__construct(\Plop\FormatterInterface $formatter=null,\Plop\FiltersCollectionAbstract $filters=null)
This exception is thrown by Plop whenever a problem is detected.
Definition: Exception.php:30
$formatter
Formatter object to use for this handler.