Total Complexity | 6 |
Total Lines | 40 |
Duplicated Lines | 0 % |
Changes | 0 |
1 | <?php |
||
7 | final class FileLogger extends AbstractLogger implements \WebServCo\Framework\Interfaces\LoggerInterface |
||
8 | { |
||
9 | protected $channel; |
||
10 | protected $logPath; |
||
11 | protected $requestInterface; |
||
12 | |||
13 | public function __construct($channel, $logDir, RequestInterface $requestInterface) |
||
14 | { |
||
15 | $this->channel = $channel; |
||
16 | |||
17 | if (!is_readable($logDir)) { |
||
18 | throw new ApplicationException('Log dir not readable'); |
||
19 | } |
||
20 | if (!is_writable($logDir)) { |
||
21 | throw new ApplicationException('Log dir not writeable'); |
||
22 | } |
||
23 | $this->logPath = sprintf('%s%s.log', $logDir, $this->channel); |
||
24 | |||
25 | $this->requestInterface = $requestInterface; |
||
26 | } |
||
27 | |||
28 | public function log($level, $message, $context = []) |
||
29 | { |
||
30 | $data = sprintf( |
||
31 | '[%s] [%s] [%s] %s%s', |
||
32 | date('Y-m-d H:i:s'), |
||
33 | $this->requestInterface->getRemoteAddress(), |
||
34 | $level, |
||
35 | $message, |
||
36 | PHP_EOL |
||
37 | ); |
||
38 | if (!empty($context)) { |
||
39 | $data .= sprintf('%s%s', var_export($context, true), PHP_EOL); |
||
40 | } |
||
41 | return file_put_contents($this->logPath, $data, FILE_APPEND); |
||
42 | } |
||
43 | |||
44 | public function clear() |
||
47 | } |
||
48 | } |
||
49 |