dolibarr
25.0.0-alpha
Main Page
Related Pages
Topics
Classes
Files
Examples
File List
File Members
dolibarr_dev
htdocs
debugbar
class
DataCollector
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
25
use DebugBar\DataCollector\MessagesCollector;
26
use Psr\Log\LogLevel;
27
28
//use ReflectionClass;
29
34
class
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
72
public
function
getWidgets
()
73
{
74
global $langs;
75
76
$title = $langs->transnoentities(
'Logs'
);
77
$name = $this->getName();
78
79
return
array(
80
"$title"
=> array(
81
"icon"
=>
"list-alt"
,
82
"widget"
=>
"PhpDebugBar.Widgets.MessagesWidget"
,
83
"map"
=>
"$name.messages"
,
84
"default"
=>
"[]"
85
),
86
"$title:badge"
=> array(
87
"map"
=>
"$name.count"
,
88
"default"
=>
"null"
89
)
90
);
91
}
92
98
public
function
collect
()
99
{
100
global
$conf
;
101
102
$uselogfile =
getDolGlobalInt
(
'DEBUGBAR_USE_LOG_FILE'
);
103
104
if
($uselogfile) {
105
$this->
getStorageLogs
($this->path);
106
}
else
{
107
$log_levels = $this->
getLevels
();
108
109
foreach
($conf->logbuffer as $line) {
110
if
($this->nboflines >= $this->maxnboflines) {
111
break
;
112
}
113
foreach
($log_levels as $level_key => $level) {
114
if
(strpos(strtolower($line), strtolower($level_key)) == 20) {
115
$this->nboflines++;
116
// Use parent method to add the message
117
$this->addMessage($line, $level,
false
);
118
}
119
}
120
}
121
}
122
123
return
parent::collect();
124
}
125
131
public
function
getLogsFile
()
132
{
133
// default dolibarr log file
134
$path = DOL_DATA_ROOT.
'/dolibarr.log'
;
135
return
$path;
136
}
137
144
public
function
getStorageLogs
($path)
145
{
146
if
(!file_exists($path)) {
147
return
;
148
}
149
150
// Load the latest lines
151
$file = implode(
""
, $this->
tailFile
($path, $this->maxnboflines));
152
153
foreach
($this->
getLogs
($file) as $log) {
154
$this->addMessage($log[
'line'
], $log[
'level'
],
false
);
155
}
156
}
157
165
protected
function
tailFile
($file, $lines)
166
{
167
$handle = fopen($file,
"r"
);
168
$linecounter = $lines;
169
$pos = -2;
170
$beginning =
false
;
171
$text = array();
172
while
($linecounter > 0) {
173
$t =
" "
;
174
while
($t !=
"\n"
) {
175
if
(fseek($handle, $pos, SEEK_END) == -1) {
176
$beginning =
true
;
177
break
;
178
}
179
$t = fgetc($handle);
180
$pos--;
181
}
182
$linecounter--;
183
if
($beginning) {
184
rewind($handle);
185
}
186
$text[$lines - $linecounter - 1] = fgets($handle);
187
if
($beginning) {
188
break
;
189
}
190
}
191
fclose($handle);
192
return
array_reverse($text);
193
}
194
201
public
function
getLogs
($file)
202
{
203
$pattern =
"/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/"
;
204
$log_levels = $this->
getLevels
();
205
$matches = array();
206
preg_match_all($pattern, $file, $matches);
207
$log = array();
208
foreach
($matches as $lines) {
209
foreach
($lines as $line) {
210
foreach
($log_levels as $level_key => $level) {
211
if
(strpos(strtolower($line), strtolower($level_key)) == 20) {
212
$log[] = array(
'level'
=> $level,
'line'
=> $line);
213
}
214
}
215
}
216
}
217
$log = array_reverse($log);
218
return
$log;
219
}
220
226
public
function
getLevels
()
227
{
228
// @phan-suppress-next-line PhanRedefinedClassReference // Psr/LogLevel also provided by Sabre setup
229
$class =
new
ReflectionClass(
new
LogLevel());
230
$levels = $class->getConstants();
231
$levels[
'ERR'
] =
'error'
;
232
$levels[
'WARN'
] =
'warning'
;
233
234
return
$levels;
235
}
236
}
$conf
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
Definition
chat_history.php:55
DolLogsCollector
DolLogsCollector class.
Definition
DolLogsCollector.php:35
DolLogsCollector\getWidgets
getWidgets()
Return widget settings.
Definition
DolLogsCollector.php:72
DolLogsCollector\getStorageLogs
getStorageLogs($path)
Get logs.
Definition
DolLogsCollector.php:144
DolLogsCollector\__construct
__construct($path=null, $name='logs')
Constructor.
Definition
DolLogsCollector.php:56
DolLogsCollector\getLogs
getLogs($file)
Search a string for log entries into the log file.
Definition
DolLogsCollector.php:201
DolLogsCollector\getLevels
getLevels()
Get the log levels from psr/log.
Definition
DolLogsCollector.php:226
DolLogsCollector\getLogsFile
getLogsFile()
Get the path to the logs file.
Definition
DolLogsCollector.php:131
DolLogsCollector\tailFile
tailFile($file, $lines)
Get latest file lines.
Definition
DolLogsCollector.php:165
DolLogsCollector\collect
collect()
Return collected data.
Definition
DolLogsCollector.php:98
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return a Dolibarr global constant int value.
Definition
functions.lib.php:359
Generated on Sun Nov 1 2026 01:00:23 for
dolibarr
by Doxygen 1.11.0