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