Allow use of log file or not for debugbar
This commit is contained in:
parent
df9e121ac6
commit
77843ddeba
@ -47,8 +47,9 @@ if ($action == 'set')
|
|||||||
{
|
{
|
||||||
$db->begin();
|
$db->begin();
|
||||||
|
|
||||||
$result = dolibarr_set_const($db, "DEBUGBAR_LOGS_LINES_NUMBER", GETPOST('DEBUGBAR_LOGS_LINES_NUMBER', 'int'), 'chaine', 0, '', 0);
|
$result1 = dolibarr_set_const($db, "DEBUGBAR_LOGS_LINES_NUMBER", GETPOST('DEBUGBAR_LOGS_LINES_NUMBER', 'int'), 'chaine', 0, '', 0);
|
||||||
if ($result < 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++;
|
$error++;
|
||||||
}
|
}
|
||||||
@ -61,7 +62,7 @@ if ($action == 'set')
|
|||||||
else
|
else
|
||||||
{
|
{
|
||||||
$db->rollback();
|
$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 ' '.$langs->trans("WarningValueHigherSlowsDramaticalyOutput");
|
||||||
print '</td></tr>';
|
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 '</table>';
|
||||||
print "</form>\n";
|
print "</form>\n";
|
||||||
|
|
||||||
|
|||||||
@ -31,9 +31,10 @@ class DolLogsCollector extends MessagesCollector
|
|||||||
|
|
||||||
parent::__construct($name);
|
parent::__construct($name);
|
||||||
|
|
||||||
//$this->path = $path ?: $this->getLogsFile();
|
|
||||||
$this->nboflines=0;
|
$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->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()
|
public function collect()
|
||||||
{
|
{
|
||||||
//$this->getStorageLogs($this->path);
|
|
||||||
global $conf;
|
global $conf;
|
||||||
//var_dump($conf->logbuffer);
|
|
||||||
|
|
||||||
$log = array();
|
$uselogfile=$conf->global->DEBUGBAR_USE_LOGFILE;
|
||||||
$log_levels = $this->getLevels();
|
|
||||||
|
|
||||||
foreach ($conf->logbuffer as $line) {
|
if ($uselogfile)
|
||||||
if ($this->nboflines >= $this->maxnboflines)
|
{
|
||||||
{
|
$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;
|
break;
|
||||||
}
|
}
|
||||||
foreach ($log_levels as $level_key => $level) {
|
}
|
||||||
if (strpos(strtolower($line), strtolower($level_key)) == 20) {
|
fclose($handle);
|
||||||
$this->nboflines++;
|
return array_reverse($text);
|
||||||
$this->addMessage($line, $level, false);
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
$log = array_reverse($log);
|
||||||
return parent::collect();
|
return $log;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user