dolibarr  17.0.4
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 
26  protected $nboflines;
27 
34  public function __construct($path = null, $name = 'logs')
35  {
36  global $conf;
37 
38  parent::__construct($name);
39 
40  $this->nboflines = 0;
41  $this->maxnboflines = getDolGlobalInt('DEBUGBAR_LOGS_LINES_NUMBER', 250); // High number slows seriously output
42 
43  $this->path = $path ?: $this->getLogsFile();
44  }
45 
51  public function getWidgets()
52  {
53  global $langs;
54 
55  $title = $langs->transnoentities('Logs');
56  $name = $this->getName();
57 
58  return array(
59  "$title" => array(
60  "icon" => "list-alt",
61  "widget" => "PhpDebugBar.Widgets.MessagesWidget",
62  "map" => "$name.messages",
63  "default" => "[]"
64  ),
65  "$title:badge" => array(
66  "map" => "$name.count",
67  "default" => "null"
68  )
69  );
70  }
71 
77  public function collect()
78  {
79  global $conf;
80 
81  $uselogfile = getDolGlobalInt('DEBUGBAR_USE_LOG_FILE');
82 
83  if ($uselogfile) {
84  $this->getStorageLogs($this->path);
85  } else {
86  $log_levels = $this->getLevels();
87 
88  foreach ($conf->logbuffer as $line) {
89  if ($this->nboflines >= $this->maxnboflines) {
90  break;
91  }
92  foreach ($log_levels as $level_key => $level) {
93  if (strpos(strtolower($line), strtolower($level_key)) == 20) {
94  $this->nboflines++;
95  $this->addMessage($line, $level, false);
96  }
97  }
98  }
99  }
100 
101  return parent::collect();
102  }
103 
109  public function getLogsFile()
110  {
111  // default dolibarr log file
112  $path = DOL_DATA_ROOT.'/dolibarr.log';
113  return $path;
114  }
115 
122  public function getStorageLogs($path)
123  {
124  if (!file_exists($path)) {
125  return;
126  }
127 
128  // Load the latest lines
129  $file = implode("", $this->tailFile($path, $this->maxnboflines));
130 
131  foreach ($this->getLogs($file) as $log) {
132  $this->addMessage($log['line'], $log['level'], false);
133  }
134  }
135 
143  protected function tailFile($file, $lines)
144  {
145  $handle = fopen($file, "r");
146  $linecounter = $lines;
147  $pos = -2;
148  $beginning = false;
149  $text = array();
150  while ($linecounter > 0) {
151  $t = " ";
152  while ($t != "\n") {
153  if (fseek($handle, $pos, SEEK_END) == -1) {
154  $beginning = true;
155  break;
156  }
157  $t = fgetc($handle);
158  $pos--;
159  }
160  $linecounter--;
161  if ($beginning) {
162  rewind($handle);
163  }
164  $text[$lines - $linecounter - 1] = fgets($handle);
165  if ($beginning) {
166  break;
167  }
168  }
169  fclose($handle);
170  return array_reverse($text);
171  }
172 
179  public function getLogs($file)
180  {
181  $pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/";
182  $log_levels = $this->getLevels();
183  preg_match_all($pattern, $file, $matches);
184  $log = array();
185  foreach ($matches as $lines) {
186  foreach ($lines as $line) {
187  foreach ($log_levels as $level_key => $level) {
188  if (strpos(strtolower($line), strtolower($level_key)) == 20) {
189  $log[] = array('level' => $level, 'line' => $line);
190  }
191  }
192  }
193  }
194  $log = array_reverse($log);
195  return $log;
196  }
197 
203  public function getLevels()
204  {
205  $class = new ReflectionClass(new LogLevel());
206  $levels = $class->getConstants();
207  $levels['ERR'] = 'error';
208  $levels['WARN'] = 'warning';
209 
210  return $levels;
211  }
212 }
DolLogsCollector class.
getWidgets()
Return widget settings.
getStorageLogs($path)
Get logs.
__construct($path=null, $name='logs')
Constructor.
getLogs($file)
Search a string for log entries.
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 dolibarr global constant int value.