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\MessageInterface |
38
|
|
|
*/ |
39
|
|
|
protected $message; |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @param array $params |
43
|
|
|
* @throws \ReflectionException |
44
|
|
|
*/ |
45
|
11 |
|
public function __construct(array $params = []) |
46
|
|
|
{ |
47
|
11 |
|
$this->defaultParams = array_merge($this->defaultParams, $params); |
48
|
|
|
|
49
|
11 |
|
$levels = new \ReflectionClass(new LogLevel); |
50
|
11 |
|
$this->levels = $levels->getConstants(); |
51
|
|
|
|
52
|
11 |
|
$this->reloadStorage(); |
53
|
11 |
|
$this->reloadMessage(); |
54
|
11 |
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* log event information into file |
58
|
|
|
* |
59
|
|
|
* @param array|string|object $message |
60
|
|
|
* @param array $context |
61
|
|
|
* @return $this |
62
|
|
|
*/ |
63
|
10 |
|
public function makeLog($message, array $context = []) |
64
|
|
|
{ |
65
|
10 |
|
$this->log($this->defaultParams['level'], $message, $context); |
66
|
|
|
|
67
|
9 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @param string $level |
72
|
|
|
* @param string|array|object $message |
73
|
|
|
* @param array $context |
74
|
|
|
* @throws \Psr\Log\InvalidArgumentException |
75
|
|
|
*/ |
76
|
12 |
|
public function log($level, $message, array $context = []) |
77
|
|
|
{ |
78
|
12 |
|
if (!in_array($level, $this->levels, true)) { |
79
|
1 |
|
throw new InvalidArgumentException('Level not defined: ' . $level); |
80
|
|
|
} |
81
|
|
|
|
82
|
11 |
|
$newMessage = $this->message |
83
|
11 |
|
->createMessage($message, $context) |
84
|
10 |
|
->getMessage(); |
85
|
|
|
|
86
|
10 |
|
$this->storage->store($newMessage, $level); |
87
|
10 |
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return $this |
91
|
|
|
*/ |
92
|
12 |
|
protected function reloadStorage() |
93
|
|
|
{ |
94
|
12 |
|
if ($this->defaultParams['storage'] instanceof StorageInterface) { |
95
|
1 |
|
$this->storage = $this->defaultParams['storage']; |
96
|
1 |
|
return $this; |
97
|
|
|
} |
98
|
|
|
|
99
|
11 |
|
$this->storage = new $this->defaultParams['storage']($this->defaultParams); |
100
|
11 |
|
return $this; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @return $this |
105
|
|
|
*/ |
106
|
11 |
|
protected function reloadMessage() |
107
|
|
|
{ |
108
|
11 |
|
if ($this->defaultParams['message'] instanceof MessageInterface) { |
109
|
1 |
|
$this->message = $this->defaultParams['message']; |
110
|
1 |
|
return $this; |
111
|
|
|
} |
112
|
|
|
|
113
|
10 |
|
$this->message = new $this->defaultParams['message']($this->defaultParams); |
114
|
10 |
|
return $this; |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* set log option for all future executions of makeLog |
119
|
|
|
* |
120
|
|
|
* @param string $key |
121
|
|
|
* @param mixed $val |
122
|
|
|
* @return $this |
123
|
|
|
*/ |
124
|
3 |
|
public function setOption($key, $val) |
125
|
|
|
{ |
126
|
3 |
|
$this->defaultParams[$key] = $val; |
127
|
3 |
|
return $this->reloadStorage(); |
128
|
|
|
} |
129
|
|
|
|
130
|
|
|
/** |
131
|
|
|
* return all configuration or only given key value |
132
|
|
|
* |
133
|
|
|
* @param null|string $key |
134
|
|
|
* @return array|mixed |
135
|
|
|
*/ |
136
|
3 |
|
public function getOption($key = null) |
137
|
|
|
{ |
138
|
3 |
|
if (is_null($key)) { |
139
|
1 |
|
return $this->defaultParams; |
140
|
|
|
} |
141
|
|
|
|
142
|
3 |
|
return $this->defaultParams[$key]; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
1 |
|
public function getLastMessage() |
149
|
|
|
{ |
150
|
1 |
|
return $this->message->getMessage(); |
151
|
|
|
} |
152
|
|
|
} |
153
|
|
|
|