31 const DEFAULT_FORMAT =
'%(message)s';
34 const DEFAULT_DATE_FORMAT =
"Y-m-d H:i:s,u";
94 $format = self::DEFAULT_FORMAT,
95 $dateFormat = self::DEFAULT_DATE_FORMAT,
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');
107 if ($interpolator === null) {
108 $interpolator = new \Plop\Interpolator\Percent();
110 $this->setFormat($format);
111 $this->setDateFormat($dateFormat);
112 $this->setTimeZone($timezone);
113 $this->setPythonLike($pythonLike);
114 $this->setInterpolator($interpolator);
120 return $this->format;
126 $this->format = $format;
133 return $this->dateFormat;
139 $this->dateFormat = $dateFormat;
146 return $this->timezone;
152 $this->timezone = $timezone;
167 return $this->pythonLike;
184 if (!is_bool($pythonLike)) {
185 throw new \Plop\Exception(
'Invalid value');
187 $this->pythonLike = $pythonLike;
191 public function getInterpolator()
193 return $this->interpolator;
198 $this->interpolator = $interpolator;
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);
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']);
220 if ($record[
'exc_text']) {
221 $s .=
"\n" . $record[
'exc_text'];
245 $dateFormat = self::DEFAULT_DATE_FORMAT
247 $date = clone $record[
'createdDate'];
248 if ($this->timezone !== null) {
249 $date->setTimeZone($this->timezone);
251 return $date->format((
string) $dateFormat);
267 if (!$this->pythonLike) {
268 $s = (string) $exception;
269 if (substr($s, -1) ==
"\n") {
270 $s = substr($s, 0, -1);
275 $s =
"Traceback (most recent call last):\n";
277 foreach ($exception->getTrace() as $trace) {
279 if (isset($trace[
'class'])) {
280 $origin = $trace[
'class'].$trace[
'type'];
282 if (isset($trace[
'function'])) {
283 $origin .= $trace[
'function'].
'()';
288 $file = isset($trace[
'file']) ? $trace[
'file'] :
'{main}';
289 $line = isset($trace[
'line']) ? $trace[
'line'] : 0;
290 $traces[] =
"File \"$file\", line $line, in $origin";
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();
Interface for a log record.
This exception is thrown by Plop whenever a problem is detected.