dolibarr 21.0.0-alpha
DolLogsCollector.php
Go to the documentation of this file.
1<?php
2/* Copyright (C) 2023 Laurent Destailleur <eldy@users.sourceforge.net>
3 * Copyright (C) 2024 MDW <mdeweerd@users.noreply.github.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <https://www.gnu.org/licenses/>.
17 */
18
25use DebugBar\DataCollector\MessagesCollector;
26use Psr\Log\LogLevel;
27
28//use ReflectionClass;
29
34class DolLogsCollector extends MessagesCollector
35{
39 protected $path;
43 protected $maxnboflines;
44
48 protected $nboflines;
49
56 public function __construct($path = null, $name = 'logs')
57 {
58 parent::__construct($name);
59
60 $this->nboflines = 0;
61 $this->maxnboflines = getDolGlobalInt('DEBUGBAR_LOGS_LINES_NUMBER', 250); // High number slows seriously output
62
63 $this->path = $path ?: $this->getLogsFile();
64 }
65
71 public function getWidgets()
72 {
73 global $langs;
74
75 $title = $langs->transnoentities('Logs');
76 $name = $this->getName();
77
78 return array(
79 "$title" => array(
80 "icon" => "list-alt",
81 "widget" => "PhpDebugBar.Widgets.MessagesWidget",
82 "map" => "$name.messages",
83 "default" => "[]"
84 ),
85 "$title:badge" => array(
86 "map" => "$name.count",
87 "default" => "null"
88 )
89 );
90 }
91
97 public function collect()
98 {
99 global $conf;
100
101 $uselogfile = getDolGlobalInt('DEBUGBAR_USE_LOG_FILE');
102
103 if ($uselogfile) {
104 $this->getStorageLogs($this->path);
105 } else {
106 $log_levels = $this->getLevels();
107
108 foreach ($conf->logbuffer as $line) {
109 if ($this->nboflines >= $this->maxnboflines) {
110 break;
111 }
112 foreach ($log_levels as $level_key => $level) {
113 if (strpos(strtolower($line), strtolower($level_key)) == 20) {
114 $this->nboflines++;
115 // Use parent method to add the message
116 $this->addMessage($line, $level, false);
117 }
118 }
119 }
120 }
121
122 return parent::collect();
123 }
124
130 public function getLogsFile()
131 {
132 // default dolibarr log file
133 $path = DOL_DATA_ROOT.'/dolibarr.log';
134 return $path;
135 }
136
143 public function getStorageLogs($path)
144 {
145 if (!file_exists($path)) {
146 return;
147 }
148
149 // Load the latest lines
150 $file = implode("", $this->tailFile($path, $this->maxnboflines));
151
152 foreach ($this->getLogs($file) as $log) {
153 $this->addMessage($log['line'], $log['level'], false);
154 }
155 }
156
164 protected function tailFile($file, $lines)
165 {
166 $handle = fopen($file, "r");
167 $linecounter = $lines;
168 $pos = -2;
169 $beginning = false;
170 $text = array();
171 while ($linecounter > 0) {
172 $t = " ";
173 while ($t != "\n") {
174 if (fseek($handle, $pos, SEEK_END) == -1) {
175 $beginning = true;
176 break;
177 }
178 $t = fgetc($handle);
179 $pos--;
180 }
181 $linecounter--;
182 if ($beginning) {
183 rewind($handle);
184 }
185 $text[$lines - $linecounter - 1] = fgets($handle);
186 if ($beginning) {
187 break;
188 }
189 }
190 fclose($handle);
191 return array_reverse($text);
192 }
193
200 public function getLogs($file)
201 {
202 $pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/";
203 $log_levels = $this->getLevels();
204 $matches = array();
205 preg_match_all($pattern, $file, $matches);
206 $log = array();
207 foreach ($matches as $lines) {
208 foreach ($lines as $line) {
209 foreach ($log_levels as $level_key => $level) {
210 if (strpos(strtolower($line), strtolower($level_key)) == 20) {
211 $log[] = array('level' => $level, 'line' => $line);
212 }
213 }
214 }
215 }
216 $log = array_reverse($log);
217 return $log;
218 }
219
225 public function getLevels()
226 {
227 // @phan-suppress-next-line PhanRedefinedClassReference // Psr/LogLevel also provided by Sabre setup
228 $class = new ReflectionClass(new LogLevel());
229 $levels = $class->getConstants();
230 $levels['ERR'] = 'error';
231 $levels['WARN'] = 'warning';
232
233 return $levels;
234 }
235}
DolLogsCollector class.
getWidgets()
Return widget settings.
getStorageLogs($path)
Get logs.
__construct($path=null, $name='logs')
Constructor.
getLogs($file)
Search a string for log entries into the log file.
getLevels()
Get the log levels from psr/log.
getLogsFile()
Get the path to the logs file.
tailFile($file, $lines)
Get latest file lines.
collect()
Return collected data.
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.