Plop
A simple logging library for PHP
Record.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 
31 class Record implements \Plop\RecordInterface
32 {
34  protected $dict;
35 
80  public function __construct(
81  $loggerNamespace,
82  $loggerClass,
83  $loggerMethod,
84  $namespace,
85  $class,
86  $method,
87  $level,
88  $pathname,
89  $lineno,
90  $msg,
91  array $args,
92  \Plop\InterpolatorInterface $interpolator,
93  \Exception $exception = null
94  ) {
95  static $pid = null;
96  if ($pid === null) {
97  $pid = getmypid();
98  }
99 
100  $this->interpolator = $interpolator;
101  $logging = \Plop\Plop::getInstance();
102  $ct = explode(' ', microtime(false));
103  $msecs = (int) substr($ct[0] . '000000', 2);
104  $date = new \DateTime('@' . $ct[1], new \DateTimeZone('UTC'));
105  $date = new \DateTime(
106  sprintf(
107  '%s.%s',
108  $date->format('Y-m-d\\TH:i:s'),
109  substr($ct[0], 2)
110  ),
111  new \DateTimeZone('UTC')
112  );
113  $created = ((float) $date->format('U.u'));
114  $diff = ($created - $logging->getCreationDate()) * 1000;
115  if (isset($_SERVER['argv'][0])) {
116  $processName = basename($_SERVER['argv'][0]);
117  } else {
118  $processName = '-';
119  }
120  // Represent the date using the local timezone (if configured).
121  $date->setTimeZone(new \DateTimeZone(@date_default_timezone_get()));
122 
123  $this->dict['args'] = $args;
124  $this->dict['class'] = $class;
125  $this->dict['created'] = $created;
126  $this->dict['createdDate'] = $date;
127  $this->dict['exc_info'] = $exception;
128  $this->dict['exc_text'] = null;
129  $this->dict['filename'] = $pathname;
130  $this->dict['funcName'] = $method;
131  $this->dict['hostname'] = php_uname('n');
132  $this->dict['levelname'] = $logging->getLevelName($level);
133  $this->dict['levelno'] = $level;
134  $this->dict['lineno'] = $lineno;
135  $this->dict['loggerClass'] = $loggerClass;
136  $this->dict['loggerMethod'] = $loggerMethod;
137  $this->dict['loggerNamespace'] = $loggerNamespace;
138  $this->dict['method'] = $method;
139  $this->dict['module'] = $namespace;
140  $this->dict['msecs'] = $msecs;
141  $this->dict['msg'] = $msg;
142  $this->dict['namespace'] = $namespace;
143  $this->dict['pathname'] = $pathname;
144  $this->dict['process'] = $pid;
145  $this->dict['processName'] = $processName;
146  $this->dict['relativeCreated'] = $diff;
147  $this->dict['threadId'] = null;
148  $this->dict['threadCreatorId'] = null;
149  }
150 
152  public function getInterpolator()
153  {
154  return $this->interpolator;
155  }
156 
158  public function setInterpolator(\Plop\InterpolatorInterface $interpolator)
159  {
160  $this->interpolator = $interpolator;
161  return $this;
162  }
163 
165  public function getMessage()
166  {
167  return $this->interpolator->interpolate(
168  $this->dict['msg'],
169  $this->dict['args']
170  );
171  }
172 
212  public function offsetGet($offset)
213  {
214  return $this->dict[$offset];
215  }
216 
231  public function offsetSet($offset, $value)
232  {
233  return $this->dict[$offset] = $value;
234  }
235 
248  public function offsetExists($offset)
249  {
250  return isset($this->dict[$offset]);
251  }
252 
259  public function offsetUnset($offset)
260  {
261  unset($this->dict[$offset]);
262  }
263 
265  public function asArray()
266  {
267  return $this->dict;
268  }
269 
276  public function serialize()
277  {
278  return serialize(array($this->dict, $this->interpolator));
279  }
280 
291  public function unserialize($data)
292  {
293  $data = unserialize($data);
294  $this->dict = $data[0];
295  $this->setInterpolator($data[1]);
296  }
297 }
$dict
Array of properties for the log record.
Definition: Record.php:34
offsetExists($offset)
Definition: Record.php:248
getInterpolator()
Definition: Record.php:152
offsetGet($offset)
Definition: Record.php:212
Interface for a log record.
offsetSet($offset, $value)
Definition: Record.php:231
setInterpolator(\Plop\InterpolatorInterface $interpolator)
Definition: Record.php:158
static & getInstance()
Definition: Plop.php:195
__construct($loggerNamespace, $loggerClass, $loggerMethod, $namespace, $class, $method, $level, $pathname, $lineno, $msg, array $args,\Plop\InterpolatorInterface $interpolator,\Exception $exception=null)
Definition: Record.php:80
getMessage()
Definition: Record.php:165
unserialize($data)
Definition: Record.php:291
This exception is thrown by Plop whenever a problem is detected.
Definition: Exception.php:30
A class that stores a log record.
Definition: Record.php:31
offsetUnset($offset)
Definition: Record.php:259