diff --git a/htdocs/admin/debugbar.php b/htdocs/admin/debugbar.php
index 16dd39b7085..b6002d1a4df 100644
--- a/htdocs/admin/debugbar.php
+++ b/htdocs/admin/debugbar.php
@@ -47,8 +47,9 @@ if ($action == 'set')
{
$db->begin();
- $result = dolibarr_set_const($db, "DEBUGBAR_LOGS_LINES_NUMBER", GETPOST('DEBUGBAR_LOGS_LINES_NUMBER', 'int'), 'chaine', 0, '', 0);
- if ($result < 0)
+ $result1 = dolibarr_set_const($db, "DEBUGBAR_LOGS_LINES_NUMBER", GETPOST('DEBUGBAR_LOGS_LINES_NUMBER', 'int'), 'chaine', 0, '', 0);
+ $result2 = dolibarr_set_const($db, "DEBUGBAR_USE_LOG_FILE", GETPOST('DEBUGBAR_USE_LOG_FILE', 'int'), 'chaine', 0, '', 0);
+ if ($result1 < 0 || $result2 < 0)
{
$error++;
}
@@ -61,7 +62,7 @@ if ($action == 'set')
else
{
$db->rollback();
- setEventMessages($error, $errors, 'errors');
+ setEventMessages($error, null, 'errors');
}
}
@@ -96,6 +97,12 @@ print '
';
+print ' | | '.$langs->trans("DEBUGBAR_USE_LOG_FILE").' | ';
+print '';
+print $form->selectyesno('DEBUGBAR_USE_LOG_FILE', $conf->global->DEBUGBAR_USE_LOG_FILE, 1);
+print ' '.$langs->trans("UsingLogFileShowAllRecordOfSubrequestButIsSlower");
+print ' |
';
+
print '';
print "\n";
diff --git a/htdocs/debugbar/class/DataCollector/DolLogsCollector.php b/htdocs/debugbar/class/DataCollector/DolLogsCollector.php
index eba2ca503cc..889e6b3d368 100644
--- a/htdocs/debugbar/class/DataCollector/DolLogsCollector.php
+++ b/htdocs/debugbar/class/DataCollector/DolLogsCollector.php
@@ -31,9 +31,10 @@ class DolLogsCollector extends MessagesCollector
parent::__construct($name);
- //$this->path = $path ?: $this->getLogsFile();
$this->nboflines=0;
$this->maxnboflines = empty($conf->global->DEBUGBAR_LOGS_LINES_NUMBER) ? 250 : $conf->global->DEBUGBAR_LOGS_LINES_NUMBER; // High number slows seriously output
+
+ $this->path = $path ?: $this->getLogsFile();
}
/**
@@ -69,27 +70,127 @@ class DolLogsCollector extends MessagesCollector
*/
public function collect()
{
- //$this->getStorageLogs($this->path);
global $conf;
- //var_dump($conf->logbuffer);
- $log = array();
- $log_levels = $this->getLevels();
+ $uselogfile=$conf->global->DEBUGBAR_USE_LOGFILE;
- foreach ($conf->logbuffer as $line) {
- if ($this->nboflines >= $this->maxnboflines)
- {
+ if ($uselogfile)
+ {
+ $this->getStorageLogs($this->path);
+ }
+ else
+ {
+ $log_levels = $this->getLevels();
+
+ foreach ($conf->logbuffer as $line) {
+ if ($this->nboflines >= $this->maxnboflines)
+ {
+ break;
+ }
+ foreach ($log_levels as $level_key => $level) {
+ if (strpos(strtolower($line), strtolower($level_key)) == 20) {
+ $this->nboflines++;
+ $this->addMessage($line, $level, false);
+ }
+ }
+ }
+ }
+
+ return parent::collect();
+ }
+
+ /**
+ * Get the path to the logs file
+ *
+ * @return string
+ */
+ public function getLogsFile()
+ {
+ // default dolibarr log file
+ $path = DOL_DATA_ROOT . '/dolibarr.log';
+ return $path;
+ }
+
+ /**
+ * Get logs
+ *
+ * @param string $path Path
+ * @return array
+ */
+ public function getStorageLogs($path)
+ {
+ if (! file_exists($path)) {
+ return;
+ }
+
+ // Load the latest lines
+ $file = implode("", $this->tailFile($path, $this->maxnboflines));
+
+ foreach ($this->getLogs($file) as $log) {
+ $this->addMessage($log['line'], $log['level'], false);
+ }
+ }
+
+ /**
+ * Get latest file lines
+ *
+ * @param string $file File
+ * @param int $lines Lines
+ * @return array Array
+ */
+ protected function tailFile($file, $lines)
+ {
+ $handle = fopen($file, "r");
+ $linecounter = $lines;
+ $pos = -2;
+ $beginning = false;
+ $text = [];
+ while ($linecounter > 0) {
+ $t = " ";
+ while ($t != "\n") {
+ if (fseek($handle, $pos, SEEK_END) == -1) {
+ $beginning = true;
+ break;
+ }
+ $t = fgetc($handle);
+ $pos--;
+ }
+ $linecounter--;
+ if ($beginning) {
+ rewind($handle);
+ }
+ $text[$lines - $linecounter - 1] = fgets($handle);
+ if ($beginning) {
break;
}
- foreach ($log_levels as $level_key => $level) {
- if (strpos(strtolower($line), strtolower($level_key)) == 20) {
- $this->nboflines++;
- $this->addMessage($line, $level, false);
+ }
+ fclose($handle);
+ return array_reverse($text);
+ }
+
+ /**
+ * Search a string for log entries
+ *
+ * @param string $file File
+ * @return array Lines of logs
+ */
+ public function getLogs($file)
+ {
+ $pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/";
+ $log_levels = $this->getLevels();
+ preg_match_all($pattern, $file, $matches);
+ $log = [];
+ foreach ($matches as $lines) {
+ foreach ($lines as $line) {
+ foreach ($log_levels as $level_key => $level) {
+ if (strpos(strtolower($line), strtolower($level_key)) == 20) {
+ $log[] = ['level' => $level, 'line' => $line];
+ }
}
}
}
-
- return parent::collect();
+ $log = array_reverse($log);
+ return $log;
}
/**