Allow use of log file or not for debugbar

This commit is contained in:
Laurent Destailleur 2019-03-23 14:50:28 +01:00
parent df9e121ac6
commit 77843ddeba
2 changed files with 125 additions and 17 deletions

View File

@ -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 '<td colspan="2"><input type="text" class="flat" name="DEBUGBAR_LOGS_LINES
print ' '.$langs->trans("WarningValueHigherSlowsDramaticalyOutput");
print '</td></tr>';
print '<tr class="oddeven"><td>'.$langs->trans("DEBUGBAR_USE_LOG_FILE").'</td>';
print '<td colspan="2">';
print $form->selectyesno('DEBUGBAR_USE_LOG_FILE', $conf->global->DEBUGBAR_USE_LOG_FILE, 1);
print ' '.$langs->trans("UsingLogFileShowAllRecordOfSubrequestButIsSlower");
print '</td></tr>';
print '</table>';
print "</form>\n";

View File

@ -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;
}
/**