|
0
|
1 |
<?php |
|
|
2 |
|
|
|
3 |
/* |
|
|
4 |
* This file is part of the Monolog package. |
|
|
5 |
* |
|
|
6 |
* (c) Jordi Boggiano <j.boggiano@seld.be> |
|
|
7 |
* |
|
|
8 |
* For the full copyright and license information, please view the LICENSE |
|
|
9 |
* file that was distributed with this source code. |
|
|
10 |
*/ |
|
|
11 |
|
|
|
12 |
namespace Monolog\Formatter; |
|
|
13 |
|
|
|
14 |
use Monolog\Logger; |
|
|
15 |
|
|
|
16 |
/** |
|
|
17 |
* Formats incoming records into a one-line string |
|
|
18 |
* |
|
|
19 |
* This is especially useful for logging to files |
|
|
20 |
* |
|
|
21 |
* @author Jordi Boggiano <j.boggiano@seld.be> |
|
|
22 |
* @author Christophe Coevoet <stof@notk.org> |
|
|
23 |
*/ |
|
|
24 |
class LineFormatter implements FormatterInterface |
|
|
25 |
{ |
|
|
26 |
const SIMPLE_FORMAT = "[%datetime%] %channel%.%level_name%: %message% %context% %extra%\n"; |
|
|
27 |
const SIMPLE_DATE = "Y-m-d H:i:s"; |
|
|
28 |
|
|
|
29 |
protected $format; |
|
|
30 |
protected $dateFormat; |
|
|
31 |
|
|
|
32 |
/** |
|
|
33 |
* @param string $format The format of the message |
|
|
34 |
* @param string $dateFormat The format of the timestamp: one supported by DateTime::format |
|
|
35 |
*/ |
|
|
36 |
public function __construct($format = null, $dateFormat = null) |
|
|
37 |
{ |
|
|
38 |
$this->format = $format ?: static::SIMPLE_FORMAT; |
|
|
39 |
$this->dateFormat = $dateFormat ?: static::SIMPLE_DATE; |
|
|
40 |
} |
|
|
41 |
|
|
|
42 |
/** |
|
|
43 |
* {@inheritdoc} |
|
|
44 |
*/ |
|
|
45 |
public function format(array $record) |
|
|
46 |
{ |
|
|
47 |
$vars = $record; |
|
|
48 |
$vars['datetime'] = $vars['datetime']->format($this->dateFormat); |
|
|
49 |
|
|
|
50 |
$output = $this->format; |
|
|
51 |
foreach ($vars['extra'] as $var => $val) { |
|
|
52 |
if (false !== strpos($output, '%extra.'.$var.'%')) { |
|
|
53 |
$output = str_replace('%extra.'.$var.'%', $this->convertToString($val), $output); |
|
|
54 |
unset($vars['extra'][$var]); |
|
|
55 |
} |
|
|
56 |
} |
|
|
57 |
foreach ($vars as $var => $val) { |
|
|
58 |
$output = str_replace('%'.$var.'%', $this->convertToString($val), $output); |
|
|
59 |
} |
|
|
60 |
|
|
|
61 |
return $output; |
|
|
62 |
} |
|
|
63 |
|
|
|
64 |
public function formatBatch(array $records) |
|
|
65 |
{ |
|
|
66 |
$message = ''; |
|
|
67 |
foreach ($records as $record) { |
|
|
68 |
$message .= $this->format($record); |
|
|
69 |
} |
|
|
70 |
|
|
|
71 |
return $message; |
|
|
72 |
} |
|
|
73 |
|
|
|
74 |
protected function convertToString($data) |
|
|
75 |
{ |
|
|
76 |
if (null === $data || is_scalar($data)) { |
|
|
77 |
return (string) $data; |
|
|
78 |
} |
|
|
79 |
|
|
|
80 |
return stripslashes(json_encode($this->normalize($data))); |
|
|
81 |
} |
|
|
82 |
|
|
|
83 |
protected function normalize($data) |
|
|
84 |
{ |
|
|
85 |
if (null === $data || is_scalar($data)) { |
|
|
86 |
return $data; |
|
|
87 |
} |
|
|
88 |
|
|
|
89 |
if (is_array($data) || $data instanceof \Traversable) { |
|
|
90 |
$normalized = array(); |
|
|
91 |
|
|
|
92 |
foreach ($data as $key => $value) { |
|
|
93 |
$normalized[$key] = $this->normalize($value); |
|
|
94 |
} |
|
|
95 |
|
|
|
96 |
return $normalized; |
|
|
97 |
} |
|
|
98 |
|
|
|
99 |
if (is_resource($data)) { |
|
|
100 |
return '[resource]'; |
|
|
101 |
} |
|
|
102 |
|
|
|
103 |
return sprintf("[object] (%s: %s)", get_class($data), json_encode($data)); |
|
|
104 |
} |
|
|
105 |
} |