1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Oqq\Minc\Log\Formatter; |
4
|
|
|
|
5
|
|
|
use Oqq\Minc\Log\Record; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* @author Eric Braun <[email protected]> |
9
|
|
|
*/ |
10
|
|
|
class LineFormatter implements FormatterInterface |
11
|
|
|
{ |
12
|
|
|
const DEFAULT_MESSAGE_FORMAT = '[%datetime%] %channel%.%level_name%: %message% %context% %extras%'; |
13
|
|
|
const DEFAULT_DATE_FORMAT = 'Y-m-d H:i:s'; |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
protected $messageFormat; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
protected $dateFormat; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @param string $messageFormat |
23
|
|
|
* @param string $dateFormat |
24
|
|
|
*/ |
25
|
|
|
public function __construct($messageFormat = null, $dateFormat = null) |
26
|
|
|
{ |
27
|
|
|
$this->messageFormat = $messageFormat ?: static::DEFAULT_MESSAGE_FORMAT; |
28
|
|
|
$this->dateFormat = $dateFormat ?: static::DEFAULT_DATE_FORMAT; |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
public function getMessageFormat() |
35
|
|
|
{ |
36
|
|
|
return $this->messageFormat; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return string |
41
|
|
|
*/ |
42
|
|
|
public function getDateFormat() |
43
|
|
|
{ |
44
|
|
|
return $this->dateFormat; |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @inheritdoc |
49
|
|
|
*/ |
50
|
|
|
public function format(Record $record) |
51
|
|
|
{ |
52
|
|
|
$replacements = []; |
53
|
|
|
|
54
|
|
View Code Duplication |
if (false !== strpos($this->messageFormat, '%datetime%')) { |
|
|
|
|
55
|
|
|
$replacements['%datetime%'] = $this->convertToString($record->getCreatedAt()); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
if (false !== strpos($this->messageFormat, '%channel%')) { |
59
|
|
|
$replacements['%channel%'] = $record->getChannel(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
if (false !== strpos($this->messageFormat, '%level_name%')) { |
63
|
|
|
$replacements['%level_name%'] = $record->getLevel()->getName(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (false !== strpos($this->messageFormat, '%message%')) { |
67
|
|
|
$replacements['%message%'] = $record->getMessage(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
View Code Duplication |
if (false !== strpos($this->messageFormat, '%context%')) { |
|
|
|
|
71
|
|
|
$replacements['%context%'] = $this->convertToString($record->getContext(), true); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
View Code Duplication |
if (false !== strpos($this->messageFormat, '%extras%')) { |
|
|
|
|
75
|
|
|
$replacements['%extras%'] = $this->convertToString($record->getExtras(), true); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
return strtr($this->messageFormat, $replacements); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* @todo use a minc helper class for string converting |
83
|
|
|
* @param mixed $data |
84
|
|
|
* @param bool $ignoreEmptyData |
85
|
|
|
* |
86
|
|
|
* @return string |
87
|
|
|
*/ |
88
|
|
|
protected function convertToString($data, $ignoreEmptyData = false) |
89
|
|
|
{ |
90
|
|
|
if ($ignoreEmptyData && empty($data)) { |
91
|
|
|
return ''; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
if (null === $data || is_bool($data)) { |
95
|
|
|
return var_export($data, true); |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
if (is_scalar($data) || method_exists($data, '__toString')) { |
99
|
|
|
return (string) $data; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
if ($data instanceof \DateTime) { |
103
|
|
|
return $data->format($this->dateFormat); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
return json_encode($data); |
107
|
|
|
} |
108
|
|
|
} |
109
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.