1 | <?php |
||
2 | /** |
||
3 | * Class FileLog |
||
4 | * |
||
5 | * @filesource FileLog.php |
||
6 | * @created 04.01.2018 |
||
7 | * @package chillerlan\Logger\Output |
||
8 | * @author Smiley <[email protected]> |
||
9 | * @copyright 2018 Smiley |
||
10 | * @license MIT |
||
11 | */ |
||
12 | |||
13 | namespace chillerlan\Logger\Output; |
||
14 | |||
15 | use chillerlan\Logger\LogException; |
||
16 | use chillerlan\Settings\SettingsContainerInterface; |
||
17 | |||
18 | class FileLog extends LogOutputAbstract{ |
||
19 | |||
20 | /** |
||
21 | * @var bool|resource |
||
22 | */ |
||
23 | protected $fh; |
||
24 | |||
25 | /** |
||
26 | * FileLog constructor. |
||
27 | * |
||
28 | * @param \chillerlan\Settings\SettingsContainerInterface|null $options |
||
29 | * |
||
30 | * @throws \chillerlan\Logger\LogException |
||
31 | */ |
||
32 | public function __construct(SettingsContainerInterface $options = null){ |
||
33 | parent::__construct($options); |
||
34 | |||
35 | if(!is_writable($this->options->logfileDir)){ |
||
36 | throw new LogException('log file directory inaccessible'); |
||
37 | } |
||
38 | |||
39 | $logfile = $this->options->logfileDir.'/'.$this->options->logfileName.'-'.date('Y-m-d').'.'.$this->options->logfileExt; |
||
40 | |||
41 | $this->fh = fopen($logfile, 'a+'); |
||
42 | } |
||
43 | |||
44 | /** |
||
45 | * @inheritdoc |
||
46 | */ |
||
47 | public function __destruct(){ |
||
48 | $this->close(); |
||
49 | |||
50 | if(gettype($this->fh) === 'resource'){ |
||
51 | fclose($this->fh); |
||
52 | } |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @inheritdoc |
||
57 | */ |
||
58 | protected function __log(string $level, string $message, array $context = null){ |
||
59 | fwrite($this->fh, $this->message($level, $message, $context)); |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
60 | } |
||
61 | |||
62 | /** |
||
63 | * @inheritdoc |
||
64 | */ |
||
65 | public function close():LogOutputInterface{ |
||
66 | $this->__log('log closed', '~~~'); |
||
67 | return $this; |
||
68 | } |
||
69 | |||
70 | } |
||
71 |