1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Koded package. |
5
|
|
|
* |
6
|
|
|
* (c) Mihail Binev <[email protected]> |
7
|
|
|
* |
8
|
|
|
* Please view the LICENSE distributed with this source code |
9
|
|
|
* for the full copyright and license information. |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Koded\Logging\Processors; |
14
|
|
|
|
15
|
|
|
use Exception; |
16
|
|
|
use Koded\Exceptions\KodedException; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Log processor for storing the log messages into text files on the disk. |
20
|
|
|
* |
21
|
|
|
* CONFIGURATION PARAMETERS |
22
|
|
|
* |
23
|
|
|
* - dir (string), default: sys_get_temp_dir() |
24
|
|
|
* The directory for the log files. Must exist and be writable by the PHP. |
25
|
|
|
* |
26
|
|
|
* NOTE: the log filename is calculated from of the current |
27
|
|
|
* YEAR/MONTH/DATE and appended to this directory path (ex: /path/to/logs/2000/01/01.log) |
28
|
|
|
* |
29
|
|
|
* - extension (string), default: .log |
30
|
|
|
* The log file extension. |
31
|
|
|
*/ |
32
|
|
|
class File extends Processor |
33
|
|
|
{ |
34
|
|
|
private string $dir = ''; |
35
|
|
|
private string $ext = ''; |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* {@inheritdoc} |
39
|
|
|
*/ |
40
|
3 |
|
public function __construct(array $settings) |
41
|
|
|
{ |
42
|
3 |
|
parent::__construct($settings); |
43
|
|
|
|
44
|
3 |
|
\umask(\umask() | 0002); |
45
|
3 |
|
$this->ext = (string)($settings['extension'] ?? '.log'); |
46
|
3 |
|
$this->dir = \rtrim((string)$settings['dir'], '/'); |
47
|
|
|
|
48
|
3 |
|
if (false === \is_dir($this->dir)) { |
49
|
1 |
|
throw FileProcessorException::directoryDoesNotExist($this->dir); |
50
|
|
|
} |
51
|
|
|
|
52
|
2 |
|
if (false === \is_writable($this->dir)) { |
53
|
1 |
|
throw FileProcessorException::directoryIsNotWritable($this->dir); |
54
|
|
|
} |
55
|
|
|
|
56
|
1 |
|
$this->dir .= '/'; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
1 |
|
protected function process(array $message): void |
60
|
|
|
{ |
61
|
|
|
try { |
62
|
|
|
// The filename should be calculated at the moment of writing |
63
|
1 |
|
$dir = $this->dir . \date('Y/m'); |
64
|
1 |
|
\is_dir($dir) || \mkdir($dir, 0775, true); |
65
|
|
|
|
66
|
1 |
|
\file_put_contents($dir . '/' . \date('d') . $this->ext, \strtr($this->format, $message) . PHP_EOL, |
67
|
1 |
|
FILE_APPEND); |
68
|
|
|
|
69
|
|
|
// @codeCoverageIgnoreStart |
70
|
|
|
} catch (Exception $e) { |
71
|
|
|
\error_log($e->getMessage()); |
72
|
|
|
// @codeCoverageIgnoreEnd |
73
|
|
|
} |
74
|
1 |
|
} |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
|
78
|
|
|
class FileProcessorException extends KodedException |
79
|
|
|
{ |
80
|
|
|
private const |
81
|
|
|
E_DIRECTORY_DOES_NOT_EXIST = 1, |
82
|
|
|
E_DIRECTORY_NOT_WRITABLE = 2; |
83
|
|
|
|
84
|
|
|
protected array $messages = [ |
85
|
|
|
self::E_DIRECTORY_DOES_NOT_EXIST => 'Log directory ":dir" must exist', |
86
|
|
|
self::E_DIRECTORY_NOT_WRITABLE => 'Log directory ":dir" must be writable', |
87
|
|
|
]; |
88
|
1 |
|
|
89
|
|
|
public static function directoryDoesNotExist(string $directory): static |
90
|
1 |
|
{ |
91
|
|
|
return new static(static::E_DIRECTORY_DOES_NOT_EXIST, [':dir' => $directory]); |
92
|
|
|
} |
93
|
1 |
|
|
94
|
|
|
public static function directoryIsNotWritable(string $directory): static |
95
|
1 |
|
{ |
96
|
|
|
return new static(static::E_DIRECTORY_NOT_WRITABLE, [':dir' => $directory]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|