1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace SimpleLog\Storage; |
||
6 | |||
7 | use SimpleLog\LogException; |
||
8 | |||
9 | class File implements StorageInterface |
||
10 | { |
||
11 | /** |
||
12 | * @var array |
||
13 | */ |
||
14 | protected $params = []; |
||
15 | |||
16 | /** |
||
17 | * @param array $params |
||
18 | */ |
||
19 | 17 | public function __construct(array $params) |
|
20 | { |
||
21 | 17 | $this->params = $params; |
|
22 | 17 | } |
|
23 | |||
24 | /** |
||
25 | * @param string $message |
||
26 | * @param string $level |
||
27 | * @throws LogException |
||
28 | * @return $this |
||
29 | */ |
||
30 | 15 | public function store(string $message, string $level): StorageInterface |
|
31 | { |
||
32 | 15 | $flag = 0; |
|
33 | 15 | $logFile = $this->params['log_path'] . DIRECTORY_SEPARATOR . $level . '.log'; |
|
34 | |||
35 | 15 | if (!\file_exists($this->params['log_path'])) { |
|
36 | 13 | $bool = @\mkdir($this->params['log_path']); |
|
37 | |||
38 | 13 | if (!$bool) { |
|
39 | 1 | throw new LogException('Unable to create log directory: ' . $this->params['log_path']); |
|
40 | } |
||
41 | } |
||
42 | |||
43 | 14 | if (\file_exists($logFile)) { |
|
44 | 3 | $flag = FILE_APPEND; |
|
45 | } |
||
46 | |||
47 | 14 | $bool = @\file_put_contents($logFile, $message, $flag | LOCK_EX); |
|
48 | |||
49 | 14 | if (!$bool) { |
|
0 ignored issues
–
show
|
|||
50 | 1 | throw new LogException('Unable to save log file: ' . $logFile); |
|
51 | } |
||
52 | |||
53 | 13 | return $this; |
|
54 | } |
||
55 | } |
||
56 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: