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