[TASK] Re-work/simplify copyright header in PHP files - Part 3
[Packages/TYPO3.CMS.git] / typo3 / sysext / core / Classes / Log / LogRecord.php
1 <?php
2 namespace TYPO3\CMS\Core\Log;
3
4 /**
5 * This file is part of the TYPO3 CMS project.
6 *
7 * It is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU General Public License, either version 2
9 * of the License, or any later version.
10 *
11 * For the full copyright and license information, please read the
12 * LICENSE.txt file that was distributed with this source code.
13 *
14 * The TYPO3 project - inspiring people to share!
15 */
16 /**
17 * Log record
18 *
19 * @author Ingo Renner <ingo@typo3.org>
20 * @author Steffen Müller (typo3@t3node.com)
21 */
22 class LogRecord implements \ArrayAccess {
23
24 /**
25 * Unique ID of the request
26 *
27 * @var string
28 */
29 protected $requestId = '';
30
31 /**
32 * Creation timestamp with microseconds
33 *
34 * @var float
35 */
36 protected $created = 0.0;
37
38 /**
39 * The component where the record was created
40 *
41 * @var string
42 */
43 protected $component = '';
44
45 /**
46 * Severity level
47 *
48 * @var integer
49 */
50 protected $level = \TYPO3\CMS\Core\Log\LogLevel::INFO;
51
52 /**
53 * Log message one-liner
54 *
55 * @var string
56 */
57 protected $message = '';
58
59 /**
60 * Additional log data
61 *
62 * @var array
63 */
64 protected $data = array();
65
66 /**
67 * Gettable properties for ArrayAccess
68 *
69 * @var array
70 */
71 private $gettableProperties = array(
72 'requestId',
73 'created',
74 'component',
75 'level',
76 'message',
77 'data'
78 );
79
80 /**
81 * Settable properties for ArrayAccess
82 *
83 * @var array
84 */
85 private $settableProperties = array(
86 'level',
87 'message',
88 'data'
89 );
90
91 /**
92 * Constructor.
93 *
94 * @param string $component Affected component
95 * @param integer $level Severity level (see \TYPO3\CMS\Core\Log\Level)
96 * @param string $message Log message
97 * @param array $data Additional data
98 */
99 public function __construct($component = '', $level, $message, array $data = array()) {
100 $this->setRequestId(\TYPO3\CMS\Core\Core\Bootstrap::getInstance()->getRequestId())
101 ->setCreated(microtime(TRUE))
102 ->setComponent($component)
103 ->setLevel($level)
104 ->setMessage($message)
105 ->setData($data);
106 }
107
108 /**
109 * Sets the affected component
110 *
111 * @param string $component Component key
112 * @return \TYPO3\CMS\Core\Log\LogRecord
113 */
114 public function setComponent($component) {
115 $this->component = $component;
116 return $this;
117 }
118
119 /**
120 * Returns the component
121 *
122 * @return string Component key
123 */
124 public function getComponent() {
125 return $this->component;
126 }
127
128 /**
129 * Sets the the creation time
130 *
131 * @param float $created Creation time as float
132 * @return \TYPO3\CMS\Core\Log\LogRecord
133 */
134 public function setCreated($created) {
135 $this->created = $created;
136 return $this;
137 }
138
139 /**
140 * Returns the creation time
141 *
142 * @return float Creation time as float
143 */
144 public function getCreated() {
145 return $this->created;
146 }
147
148 /**
149 * Sets the severity level
150 *
151 * @param integer $level Severity level
152 * @return \TYPO3\CMS\Core\Log\LogRecord
153 * @throws \RangeException if the given log level is invalid
154 * @see \TYPO3\CMS\Core\Log\Level
155 */
156 public function setLevel($level) {
157 \TYPO3\CMS\Core\Log\LogLevel::validateLevel($level);
158 $this->level = $level;
159 return $this;
160 }
161
162 /**
163 * Returns the severity level
164 *
165 * @see \TYPO3\CMS\Core\Log\Level
166 * @return integer Severity level
167 */
168 public function getLevel() {
169 return $this->level;
170 }
171
172 /**
173 * Sets log data array
174 *
175 * @param array $data
176 * @return \TYPO3\CMS\Core\Log\LogRecord
177 */
178 public function setData($data) {
179 $this->data = $data;
180 return $this;
181 }
182
183 /**
184 * Returns the log data
185 *
186 * @return array
187 */
188 public function getData() {
189 return $this->data;
190 }
191
192 /**
193 * Adds additional log data to already existing data
194 * and overwrites previously data using the same array keys.
195 *
196 * @param array $data
197 * @return \TYPO3\CMS\Core\Log\LogRecord
198 */
199 public function addData(array $data) {
200 $this->data = array_merge($this->data, $data);
201 return $this;
202 }
203
204 /**
205 * Sets the log message
206 *
207 * @param string|object $message Log message. Usually a string, or an object that can be casted to string (implements __toString())
208 * @return \TYPO3\CMS\Core\Log\LogRecord
209 */
210 public function setMessage($message) {
211 $this->message = (string)$message;
212 return $this;
213 }
214
215 /**
216 * Returns the log message
217 *
218 * @return string Log message
219 */
220 public function getMessage() {
221 return $this->message;
222 }
223
224 /**
225 * Sets the request ID
226 *
227 * @param string $requestId
228 * @return \TYPO3\CMS\Core\Log\LogRecord
229 */
230 public function setRequestId($requestId) {
231 $this->requestId = $requestId;
232 return $this;
233 }
234
235 /**
236 * Returns the request ID
237 *
238 * @return string
239 */
240 public function getRequestId() {
241 return $this->requestId;
242 }
243
244 /**
245 * Convert record to string for simple output, like echo().
246 * Contents of data array is appended as JSON-encoded string
247 *
248 * @return string
249 */
250 public function __toString() {
251 $timestamp = date('r', (int)$this->created);
252 $levelName = \TYPO3\CMS\Core\Log\LogLevel::getName($this->level);
253 $data = !empty($this->data) ? '- ' . json_encode($this->data) : '';
254 $logRecordString = sprintf('%s [%s] request="%s" component="%s": %s %s', $timestamp, $levelName, $this->requestId, $this->component, $this->message, $data);
255 return $logRecordString;
256 }
257
258 /**
259 * Convert record to array
260 *
261 * @return array
262 */
263 public function toArray() {
264 return array(
265 'requestId' => $this->requestId,
266 'created' => $this->created,
267 'component' => $this->component,
268 'level' => $this->level,
269 'message' => $this->message,
270 'data' => $this->data
271 );
272 }
273
274 /**
275 * Checks whether an offset exists, required by ArrayAccess interface
276 *
277 * @param mixed $offset
278 * @return boolean
279 */
280 public function offsetExists($offset) {
281 $offsetExists = FALSE;
282 if (in_array($offset, $this->gettableProperties, TRUE) && isset($this->{$offset})) {
283 $offsetExists = TRUE;
284 }
285 return $offsetExists;
286 }
287
288 /**
289 * Offset to retrieve, required by ArrayAccess interface
290 *
291 * @param mixed $offset
292 * @return mixed
293 */
294 public function offsetGet($offset) {
295 if (!in_array($offset, $this->gettableProperties, TRUE)) {
296 return NULL;
297 }
298 return $this->{$offset};
299 }
300
301 /**
302 * Offset to set, required by ArrayAccess interface
303 *
304 * @param mixed $offset
305 * @param mixed $value
306 * @return void
307 */
308 public function offsetSet($offset, $value) {
309 if (in_array($offset, $this->settableProperties, TRUE)) {
310 $this->{$offset} = $offset;
311 }
312 }
313
314 /**
315 * Offset to unset, required by ArrayAccess interface
316 *
317 * @param mixed $offset
318 * @return void
319 */
320 public function offsetUnset($offset) {
321 if (in_array($offset, $this->settableProperties, TRUE)) {
322 unset($this->{$offset});
323 }
324 }
325
326 }