1 | <?php |
||||
2 | |||||
3 | use Monolog\Logger; |
||||
4 | use Monolog\Handler\StreamHandler; |
||||
5 | use Monolog\Handler\RotatingFileHandler; |
||||
6 | |||||
7 | class Xcloner_Logger extends Logger { |
||||
8 | |||||
9 | private $logger_path; |
||||
10 | private $max_logger_files = 7; |
||||
11 | private $main_logger_url; |
||||
12 | |||||
13 | /** |
||||
14 | * Xcloner_Logger constructor. |
||||
15 | * @param Xcloner $xcloner_container |
||||
16 | * @param string $logger_name |
||||
17 | * @throws Exception |
||||
18 | */ |
||||
19 | public function __construct(Xcloner $xcloner_container, $logger_name = "xcloner_logger") { |
||||
20 | if (!$xcloner_container->get_xcloner_settings()) { |
||||
21 | $xcloner_settings = new Xcloner_Settings($xcloner_container); |
||||
22 | } else { |
||||
23 | $xcloner_settings = $xcloner_container->get_xcloner_settings(); |
||||
24 | } |
||||
25 | |||||
26 | $hash = $xcloner_settings->get_hash(); |
||||
27 | if ($hash == "-".$xcloner_settings->get_server_unique_hash(5)) { |
||||
28 | $hash = ""; |
||||
29 | } |
||||
30 | |||||
31 | $logger_path = $xcloner_settings->get_xcloner_store_path().DS.$xcloner_settings->get_logger_filename(); |
||||
32 | $logger_path_tmp = ""; |
||||
33 | |||||
34 | if ($hash) { |
||||
35 | $logger_path_tmp = $xcloner_settings->get_xcloner_tmp_path().DS.$xcloner_settings->get_logger_filename(1); |
||||
36 | } |
||||
37 | |||||
38 | $this->logger_path = $logger_path; |
||||
39 | |||||
40 | if (!is_dir($xcloner_settings->get_xcloner_store_path()) or !is_writable($xcloner_settings->get_xcloner_store_path())) { |
||||
41 | $logger_path = 'php://stderr'; |
||||
42 | $logger_path_tmp = ""; |
||||
43 | } |
||||
44 | |||||
45 | if (!$xcloner_settings->get_xcloner_option('xcloner_enable_log')) { |
||||
46 | $logger_path = 'php://stderr'; |
||||
47 | $logger_path_tmp = ""; |
||||
48 | } |
||||
49 | |||||
50 | // create a log channel |
||||
51 | parent::__construct($logger_name); |
||||
52 | |||||
53 | $debug_level = Logger::INFO; |
||||
54 | |||||
55 | if (WP_DEBUG) { |
||||
56 | $debug_level = Logger::DEBUG; |
||||
57 | } |
||||
58 | |||||
59 | |||||
60 | if ($logger_path) { |
||||
61 | if (!$xcloner_settings->get_xcloner_option('xcloner_enable_log')) { |
||||
62 | $stream = new StreamHandler($logger_path, $debug_level); |
||||
63 | } else { |
||||
64 | $stream = new RotatingFileHandler($logger_path, $this->max_logger_files, $debug_level); |
||||
65 | } |
||||
66 | |||||
67 | $this->pushHandler($stream); |
||||
68 | |||||
69 | $this->main_logger_url = $stream->getUrl(); |
||||
70 | } |
||||
71 | |||||
72 | if ($hash and $logger_path_tmp) { |
||||
73 | $this->pushHandler(new StreamHandler($logger_path_tmp, $debug_level)); |
||||
74 | } |
||||
75 | |||||
76 | //return $this; |
||||
77 | } |
||||
78 | |||||
79 | /** |
||||
80 | * @return string|null |
||||
81 | */ |
||||
82 | function get_main_logger_url() { |
||||
0 ignored issues
–
show
|
|||||
83 | return $this->main_logger_url; |
||||
84 | } |
||||
85 | |||||
86 | /** |
||||
87 | * @param int $totalLines |
||||
88 | * @return array|bool |
||||
89 | */ |
||||
90 | function getLastDebugLines($totalLines = 200) { |
||||
0 ignored issues
–
show
|
|||||
91 | $lines = array(); |
||||
92 | |||||
93 | if (!file_exists($this->main_logger_url) or !is_readable($this->main_logger_url)) { |
||||
94 | return false; |
||||
95 | } |
||||
96 | |||||
97 | $fp = fopen($this->main_logger_url, 'r'); |
||||
98 | fseek($fp, - 1, SEEK_END); |
||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $handle of fseek() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
99 | $pos = ftell($fp); |
||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $handle of ftell() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
100 | $lastLine = ""; |
||||
101 | |||||
102 | // Loop backword until we have our lines or we reach the start |
||||
103 | while ($pos > 0 && count($lines) < $totalLines) { |
||||
104 | |||||
105 | $C = fgetc($fp); |
||||
0 ignored issues
–
show
It seems like
$fp can also be of type false ; however, parameter $handle of fgetc() does only seem to accept resource , maybe add an additional type check?
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
![]() |
|||||
106 | if ($C == "\n") { |
||||
107 | // skip empty lines |
||||
108 | if (trim($lastLine) != "") { |
||||
109 | $lines[] = $lastLine; |
||||
110 | } |
||||
111 | $lastLine = ''; |
||||
112 | } else { |
||||
113 | $lastLine = $C.$lastLine; |
||||
114 | } |
||||
115 | fseek($fp, $pos--); |
||||
116 | } |
||||
117 | |||||
118 | $lines = array_reverse($lines); |
||||
119 | |||||
120 | return $lines; |
||||
121 | } |
||||
122 | } |
||||
123 |
Adding explicit visibility (
private
,protected
, orpublic
) is generally recommend to communicate to other developers how, and from where this method is intended to be used.