dolibarr  16.0.5
DolLogsCollector.php
1 <?php
2 
3 use DebugBar\DataCollector\MessagesCollector;
4 use Psr\Log\LogLevel;
5 
6 //use ReflectionClass;
7 
12 class DolLogsCollector extends MessagesCollector
13 {
17  protected $path;
21  protected $maxnboflines;
22 
29  public function __construct($path = null, $name = 'logs')
30  {
31  global $conf;
32 
33  parent::__construct($name);
34 
35  $this->nboflines = 0;
36  $this->maxnboflines = empty($conf->global->DEBUGBAR_LOGS_LINES_NUMBER) ? 250 : $conf->global->DEBUGBAR_LOGS_LINES_NUMBER; // High number slows seriously output
37 
38  $this->path = $path ?: $this->getLogsFile();
39  }
40 
46  public function getWidgets()
47  {
48  global $langs;
49 
50  $title = $langs->transnoentities('Logs');
51  $name = $this->getName();
52 
53  return array(
54  "$title" => array(
55  "icon" => "list-alt",
56  "widget" => "PhpDebugBar.Widgets.MessagesWidget",
57  "map" => "$name.messages",
58  "default" => "[]"
59  ),
60  "$title:badge" => array(
61  "map" => "$name.count",
62  "default" => "null"
63  )
64  );
65  }
66 
72  public function collect()
73  {
74  global $conf;
75 
76  $uselogfile = getDolGlobalInt('DEBUGBAR_USE_LOG_FILE');
77 
78  if ($uselogfile) {
79  $this->getStorageLogs($this->path);
80  } else {
81  $log_levels = $this->getLevels();
82 
83  foreach ($conf->logbuffer as $line) {
84  if ($this->nboflines >= $this->maxnboflines) {
85  break;
86  }
87  foreach ($log_levels as $level_key => $level) {
88  if (strpos(strtolower($line), strtolower($level_key)) == 20) {
89  $this->nboflines++;
90  $this->addMessage($line, $level, false);
91  }
92  }
93  }
94  }
95 
96  return parent::collect();
97  }
98 
104  public function getLogsFile()
105  {
106  // default dolibarr log file
107  $path = DOL_DATA_ROOT.'/dolibarr.log';
108  return $path;
109  }
110 
117  public function getStorageLogs($path)
118  {
119  if (!file_exists($path)) {
120  return;
121  }
122 
123  // Load the latest lines
124  $file = implode("", $this->tailFile($path, $this->maxnboflines));
125 
126  foreach ($this->getLogs($file) as $log) {
127  $this->addMessage($log['line'], $log['level'], false);
128  }
129  }
130 
138  protected function tailFile($file, $lines)
139  {
140  $handle = fopen($file, "r");
141  $linecounter = $lines;
142  $pos = -2;
143  $beginning = false;
144  $text = array();
145  while ($linecounter > 0) {
146  $t = " ";
147  while ($t != "\n") {
148  if (fseek($handle, $pos, SEEK_END) == -1) {
149  $beginning = true;
150  break;
151  }
152  $t = fgetc($handle);
153  $pos--;
154  }
155  $linecounter--;
156  if ($beginning) {
157  rewind($handle);
158  }
159  $text[$lines - $linecounter - 1] = fgets($handle);
160  if ($beginning) {
161  break;
162  }
163  }
164  fclose($handle);
165  return array_reverse($text);
166  }
167 
174  public function getLogs($file)
175  {
176  $pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/";
177  $log_levels = $this->getLevels();
178  preg_match_all($pattern, $file, $matches);
179  $log = array();
180  foreach ($matches as $lines) {
181  foreach ($lines as $line) {
182  foreach ($log_levels as $level_key => $level) {
183  if (strpos(strtolower($line), strtolower($level_key)) == 20) {
184  $log[] = array('level' => $level, 'line' => $line);
185  }
186  }
187  }
188  }
189  $log = array_reverse($log);
190  return $log;
191  }
192 
198  public function getLevels()
199  {
200  $class = new ReflectionClass(new LogLevel());
201  $levels = $class->getConstants();
202  $levels['ERR'] = 'error';
203  $levels['WARN'] = 'warning';
204 
205  return $levels;
206  }
207 }
DolLogsCollector\collect
collect()
Return collected data.
Definition: DolLogsCollector.php:72
DolLogsCollector\getLevels
getLevels()
Get the log levels from psr/log.
Definition: DolLogsCollector.php:198
DolLogsCollector\getWidgets
getWidgets()
Return widget settings.
Definition: DolLogsCollector.php:46
DolLogsCollector\getLogsFile
getLogsFile()
Get the path to the logs file.
Definition: DolLogsCollector.php:104
DolLogsCollector\getLogs
getLogs($file)
Search a string for log entries.
Definition: DolLogsCollector.php:174
DolLogsCollector\tailFile
tailFile($file, $lines)
Get latest file lines.
Definition: DolLogsCollector.php:138
DolLogsCollector\__construct
__construct($path=null, $name='logs')
Constructor.
Definition: DolLogsCollector.php:29
DolLogsCollector\getStorageLogs
getStorageLogs($path)
Get logs.
Definition: DolLogsCollector.php:117
DolLogsCollector
DolLogsCollector class.
Definition: DolLogsCollector.php:12
getDolGlobalInt
getDolGlobalInt($key, $default=0)
Return dolibarr global constant int value.
Definition: functions.lib.php:93