1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SimpleLog; |
4
|
|
|
|
5
|
|
|
use Psr\Log\LoggerInterface; |
6
|
|
|
use Psr\Log\LoggerTrait; |
7
|
|
|
use Psr\Log\LogLevel; |
8
|
|
|
use Psr\Log\InvalidArgumentException; |
9
|
|
|
use SimpleLog\Storage\StorageInterface; |
10
|
|
|
use SimpleLog\Message\MessageInterface; |
11
|
|
|
|
12
|
|
|
class Log implements LogInterface, LoggerInterface |
13
|
|
|
{ |
14
|
|
|
use LoggerTrait; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @var array |
18
|
|
|
*/ |
19
|
|
|
protected $defaultParams = [ |
20
|
|
|
'log_path' => './log', |
21
|
|
|
'level' => 'notice', |
22
|
|
|
'storage' => \SimpleLog\Storage\File::class, |
23
|
|
|
'message' => \SimpleLog\Message\DefaultMessage::class, |
24
|
|
|
]; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var \SimpleLog\Storage\StorageInterface |
28
|
|
|
*/ |
29
|
|
|
protected $storage; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
protected $levels = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @var \SimpleLog\Message\DefaultMessage |
38
|
|
|
*/ |
39
|
|
|
protected $message; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $params |
43
|
|
|
* @throws \ReflectionException |
44
|
|
|
*/ |
45
|
9 |
|
public function __construct(array $params = []) |
46
|
|
|
{ |
47
|
9 |
|
$this->defaultParams = array_merge($this->defaultParams, $params); |
48
|
|
|
|
49
|
9 |
|
$levels = new \ReflectionClass(new LogLevel); |
50
|
9 |
|
$this->levels = $levels->getConstants(); |
51
|
|
|
|
52
|
9 |
|
$this->reloadStorage(); |
53
|
9 |
|
$this->reloadMessage(); |
54
|
9 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* log event information into file |
58
|
|
|
* |
59
|
|
|
* @param array|string|object $message |
60
|
|
|
* @param array $context |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
8 |
|
public function makeLog($message, array $context = []) |
64
|
|
|
{ |
65
|
8 |
|
return $this->log($this->defaultParams['level'], $message, $context); |
|
|
|
|
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @param string $level |
70
|
|
|
* @param string|array|object $message |
71
|
|
|
* @param array $context |
72
|
|
|
* @throws \Psr\Log\InvalidArgumentException |
73
|
|
|
*/ |
74
|
10 |
|
public function log($level, $message, array $context = []) |
75
|
|
|
{ |
76
|
10 |
|
if (!in_array($level, $this->levels, true)) { |
77
|
1 |
|
throw new InvalidArgumentException('Level not defined: ' . $level); |
78
|
|
|
} |
79
|
|
|
|
80
|
9 |
|
$newMessage = $this->message |
81
|
9 |
|
->createMessage($message, $context) |
82
|
8 |
|
->getMessage(); |
83
|
|
|
|
84
|
8 |
|
$this->storage->store($newMessage, $level); |
85
|
8 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @return $this |
89
|
|
|
*/ |
90
|
10 |
|
protected function reloadStorage() |
91
|
|
|
{ |
92
|
10 |
|
if ($this->defaultParams['storage'] instanceof StorageInterface) { |
93
|
|
|
$this->storage = $this->defaultParams['storage']; |
94
|
|
|
return $this; |
95
|
|
|
} |
96
|
|
|
|
97
|
10 |
|
$this->storage = new $this->defaultParams['storage']($this->defaultParams); |
98
|
10 |
|
return $this; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* @return $this |
103
|
|
|
*/ |
104
|
9 |
|
protected function reloadMessage() |
105
|
|
|
{ |
106
|
9 |
|
if ($this->defaultParams['message'] instanceof MessageInterface) { |
107
|
|
|
$this->message = $this->defaultParams['message']; |
|
|
|
|
108
|
|
|
return $this; |
109
|
|
|
} |
110
|
|
|
|
111
|
9 |
|
$this->message = new $this->defaultParams['message']($this->defaultParams); |
112
|
9 |
|
return $this; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
/** |
116
|
|
|
* set log option for all future executions of makeLog |
117
|
|
|
* |
118
|
|
|
* @param string $key |
119
|
|
|
* @param mixed $val |
120
|
|
|
* @return $this |
121
|
|
|
*/ |
122
|
3 |
|
public function setOption($key, $val) |
123
|
|
|
{ |
124
|
3 |
|
$this->defaultParams[$key] = $val; |
125
|
3 |
|
return $this->reloadStorage(); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* return all configuration or only given key value |
130
|
|
|
* |
131
|
|
|
* @param null|string $key |
132
|
|
|
* @return array|mixed |
133
|
|
|
*/ |
134
|
3 |
|
public function getOption($key = null) |
135
|
|
|
{ |
136
|
3 |
|
if (is_null($key)) { |
137
|
1 |
|
return $this->defaultParams; |
138
|
|
|
} |
139
|
|
|
|
140
|
3 |
|
return $this->defaultParams[$key]; |
141
|
|
|
} |
142
|
|
|
|
143
|
|
|
/** |
144
|
|
|
* @return string |
145
|
|
|
*/ |
146
|
1 |
|
public function getLastMessage() |
147
|
|
|
{ |
148
|
1 |
|
return $this->message->getMessage(); |
149
|
|
|
} |
150
|
|
|
} |
151
|
|
|
|
This check looks for function or method calls that always return null and whose return value is used.
The method
getObject()
can return nothing but null, so it makes no sense to use the return value.The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.