Plop
A simple logging library for PHP
Formatter.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 
29 {
31  const DEFAULT_FORMAT = '%(message)s';
32 
34  const DEFAULT_DATE_FORMAT = "Y-m-d H:i:s,u";
35 
37  protected $format;
38 
40  protected $dateFormat;
41 
43  protected $timezone;
44 
46  protected $pythonLike;
47 
49  protected $interpolator;
50 
93  public function __construct(
94  $format = self::DEFAULT_FORMAT,
95  $dateFormat = self::DEFAULT_DATE_FORMAT,
96  $timezone = null,
97  $pythonLike = false,
98  \Plop\InterpolatorInterface $interpolator = null
99  ) {
100  if ($timezone !== null) {
101  if (is_string($timezone)) {
102  $timezone = new \DateTimeZone($timezone);
103  } elseif (!is_object($timezone) || !($timezone instanceof \DateTimeZone)) {
104  throw new \Plop\Exception('Invalid timezone');
105  }
106  }
107  if ($interpolator === null) {
108  $interpolator = new \Plop\Interpolator\Percent();
109  }
110  $this->setFormat($format);
111  $this->setDateFormat($dateFormat);
112  $this->setTimeZone($timezone);
113  $this->setPythonLike($pythonLike);
114  $this->setInterpolator($interpolator);
115  }
116 
118  public function getFormat()
119  {
120  return $this->format;
121  }
122 
124  public function setFormat($format)
125  {
126  $this->format = $format;
127  return $this;
128  }
129 
131  public function getDateFormat()
132  {
133  return $this->dateFormat;
134  }
135 
137  public function setDateFormat($dateFormat)
138  {
139  $this->dateFormat = $dateFormat;
140  return $this;
141  }
142 
144  public function getTimezone()
145  {
146  return $this->timezone;
147  }
148 
150  public function setTimezone(\DateTimeZone $timezone = null)
151  {
152  $this->timezone = $timezone;
153  return $this;
154  }
155 
165  public function getPythonLike()
166  {
167  return $this->pythonLike;
168  }
169 
182  public function setPythonLike($pythonLike)
183  {
184  if (!is_bool($pythonLike)) {
185  throw new \Plop\Exception('Invalid value');
186  }
187  $this->pythonLike = $pythonLike;
188  return $this;
189  }
190 
191  public function getInterpolator()
192  {
193  return $this->interpolator;
194  }
195 
196  public function setInterpolator(\Plop\InterpolatorInterface $interpolator)
197  {
198  $this->interpolator = $interpolator;
199  return $this;
200  }
201 
203  public function format(\Plop\RecordInterface $record)
204  {
206  $record['message'] = $record->getMessage();
207  $format = (string) $this->format;
208  $dateFormat = (string) $this->dateFormat;
209  if (strpos($format, '%(asctime)') !== false) {
210  $record['asctime'] = $this->formatTime($record, $dateFormat);
211  }
212 
213  $s = $this->interpolator->interpolate($format, $record->asArray());
214  if ($record['exc_info']) {
215  if (!$record['exc_text']) {
216  $record['exc_text'] =
217  $this->formatException($record['exc_info']);
218  }
219  }
220  if ($record['exc_text']) {
221  $s .= "\n" . $record['exc_text'];
222  }
223  return $s;
224  }
225 
243  protected function formatTime(
244  \Plop\RecordInterface $record,
245  $dateFormat = self::DEFAULT_DATE_FORMAT
246  ) {
247  $date = clone $record['createdDate'];
248  if ($this->timezone !== null) {
249  $date->setTimeZone($this->timezone);
250  }
251  return $date->format((string) $dateFormat);
252  }
253 
265  protected function formatException(\Exception $exception)
266  {
267  if (!$this->pythonLike) {
268  $s = (string) $exception;
269  if (substr($s, -1) == "\n") {
270  $s = substr($s, 0, -1);
271  }
272  return $s;
273  }
274 
275  $s = "Traceback (most recent call last):\n";
276  $traces = array();
277  foreach ($exception->getTrace() as $trace) {
278  $origin = '';
279  if (isset($trace['class'])) {
280  $origin = $trace['class'].$trace['type'];
281  }
282  if (isset($trace['function'])) {
283  $origin .= $trace['function'].'()';
284  }
285  if ($origin == '') {
286  $origin = '???';
287  }
288  $file = isset($trace['file']) ? $trace['file'] : '{main}';
289  $line = isset($trace['line']) ? $trace['line'] : 0;
290  $traces[] = "File \"$file\", line $line, in $origin";
291  }
292  $s .= implode("\n", array_reverse($traces))."\n";
293  $s .= "Exception '" . get_class($exception) .
294  "' with message '" . $exception->getMessage() .
295  "' in " . $exception->getFile() .
296  ":" . $exception->getLine();
297  return $s;
298  }
299 }
setTimezone(\DateTimeZone $timezone=null)
Definition: Formatter.php:150
setFormat($format)
Definition: Formatter.php:124
$timezone
Timezone to use to represent dates/times.
Definition: Formatter.php:43
Interface for a log record.
__construct($format=self::DEFAULT_FORMAT, $dateFormat=self::DEFAULT_DATE_FORMAT, $timezone=null, $pythonLike=false,\Plop\InterpolatorInterface $interpolator=null)
Definition: Formatter.php:93
formatTime(\Plop\RecordInterface $record, $dateFormat=self::DEFAULT_DATE_FORMAT)
Definition: Formatter.php:243
$dateFormat
Format to use for dates/times.
Definition: Formatter.php:40
Interface to a record formatter.
format(\Plop\RecordInterface $record)
Definition: Formatter.php:203
$pythonLike
Whether formatException() generates Python-like traces.
Definition: Formatter.php:46
A class that provides basic formatting for log records.
Definition: Formatter.php:28
setDateFormat($dateFormat)
Definition: Formatter.php:137
$interpolator
Object to use for interpolation in the final log message.
Definition: Formatter.php:49
This exception is thrown by Plop whenever a problem is detected.
Definition: Exception.php:30
formatException(\Exception $exception)
Definition: Formatter.php:265
setPythonLike($pythonLike)
Definition: Formatter.php:182
$format
General format for log records.
Definition: Formatter.php:37