2019-03-16 16:27:14 +01:00
< ? php
use DebugBar\DataCollector\MessagesCollector ;
use Psr\Log\LogLevel ;
2021-02-25 22:09:10 +01:00
2019-03-16 16:27:14 +01:00
//use ReflectionClass;
/**
* DolLogsCollector class
*/
class DolLogsCollector extends MessagesCollector
{
/**
* @ var string default logs file path
*/
protected $path ;
/**
* @ var int number of lines to show
*/
2019-03-23 14:37:54 +01:00
protected $maxnboflines ;
2019-03-16 16:27:14 +01:00
/**
* Constructor
*
2019-03-16 23:17:23 +01:00
* @ param string $path Path
* @ param string $name Name
2019-03-16 16:27:14 +01:00
*/
public function __construct ( $path = null , $name = 'logs' )
{
global $conf ;
parent :: __construct ( $name );
2020-04-10 10:59:32 +02:00
$this -> nboflines = 0 ;
$this -> maxnboflines = empty ( $conf -> global -> DEBUGBAR_LOGS_LINES_NUMBER ) ? 250 : $conf -> global -> DEBUGBAR_LOGS_LINES_NUMBER ; // High number slows seriously output
2019-03-23 14:50:28 +01:00
$this -> path = $path ? : $this -> getLogsFile ();
2019-03-16 16:27:14 +01:00
}
/**
* Return widget settings
*
2019-03-16 19:37:54 +01:00
* @ return array Array
2019-03-16 16:27:14 +01:00
*/
public function getWidgets ()
{
global $langs ;
$title = $langs -> transnoentities ( 'Logs' );
$name = $this -> getName ();
return array (
" $title " => array (
" icon " => " list-alt " ,
" widget " => " PhpDebugBar.Widgets.MessagesWidget " ,
" map " => " $name .messages " ,
" default " => " [] "
),
" $title :badge " => array (
" map " => " $name .count " ,
" default " => " null "
)
);
}
/**
* Return collected data
*
2019-03-16 19:37:54 +01:00
* @ return array Array
2019-03-16 16:27:14 +01:00
*/
public function collect ()
{
2019-03-23 14:37:54 +01:00
global $conf ;
2022-05-03 12:17:54 +02:00
$uselogfile = getDolGlobalInt ( 'DEBUGBAR_USE_LOG_FILE' );
2019-03-23 14:50:28 +01:00
2021-02-25 22:09:10 +01:00
if ( $uselogfile ) {
2020-10-31 14:32:18 +01:00
$this -> getStorageLogs ( $this -> path );
2020-05-21 15:05:19 +02:00
} else {
2020-10-31 14:32:18 +01:00
$log_levels = $this -> getLevels ();
foreach ( $conf -> logbuffer as $line ) {
2021-02-25 22:09:10 +01:00
if ( $this -> nboflines >= $this -> maxnboflines ) {
2020-10-31 14:32:18 +01:00
break ;
}
foreach ( $log_levels as $level_key => $level ) {
if ( strpos ( strtolower ( $line ), strtolower ( $level_key )) == 20 ) {
$this -> nboflines ++ ;
$this -> addMessage ( $line , $level , false );
}
}
}
2019-03-23 14:50:28 +01:00
}
return parent :: collect ();
}
/**
* Get the path to the logs file
*
* @ return string
*/
public function getLogsFile ()
{
2020-10-31 14:32:18 +01:00
// default dolibarr log file
$path = DOL_DATA_ROOT . '/dolibarr.log' ;
return $path ;
2019-03-23 14:50:28 +01:00
}
/**
* Get logs
*
* @ param string $path Path
* @ return array
*/
public function getStorageLogs ( $path )
{
2020-10-31 14:32:18 +01:00
if ( ! file_exists ( $path )) {
return ;
}
2019-03-23 14:50:28 +01:00
2020-10-31 14:32:18 +01:00
// Load the latest lines
$file = implode ( " " , $this -> tailFile ( $path , $this -> maxnboflines ));
2019-03-23 14:37:54 +01:00
2020-10-31 14:32:18 +01:00
foreach ( $this -> getLogs ( $file ) as $log ) {
$this -> addMessage ( $log [ 'line' ], $log [ 'level' ], false );
}
2019-03-23 14:50:28 +01:00
}
/**
* Get latest file lines
*
* @ param string $file File
* @ param int $lines Lines
* @ return array Array
*/
protected function tailFile ( $file , $lines )
{
2020-10-31 14:32:18 +01:00
$handle = fopen ( $file , " r " );
$linecounter = $lines ;
$pos = - 2 ;
$beginning = false ;
$text = array ();
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 ;
}
}
fclose ( $handle );
return array_reverse ( $text );
2019-03-23 14:50:28 +01:00
}
/**
* Search a string for log entries
*
* @ param string $file File
* @ return array Lines of logs
*/
public function getLogs ( $file )
{
2020-10-31 14:32:18 +01:00
$pattern = " / \ d { 4}- \ d { 2}- \ d { 2} \ d { 2}: \ d { 2}: \ d { 2}.*/ " ;
$log_levels = $this -> getLevels ();
preg_match_all ( $pattern , $file , $matches );
$log = array ();
foreach ( $matches as $lines ) {
foreach ( $lines as $line ) {
foreach ( $log_levels as $level_key => $level ) {
if ( strpos ( strtolower ( $line ), strtolower ( $level_key )) == 20 ) {
$log [] = array ( 'level' => $level , 'line' => $line );
}
}
}
}
$log = array_reverse ( $log );
return $log ;
2019-03-16 16:27:14 +01:00
}
/**
* Get the log levels from psr / log .
*
2019-03-16 20:10:22 +01:00
* @ return array Array of log level
2019-03-16 16:27:14 +01:00
*/
public function getLevels ()
{
$class = new ReflectionClass ( new LogLevel ());
$levels = $class -> getConstants ();
$levels [ 'ERR' ] = 'error' ;
2019-03-24 13:27:26 +01:00
$levels [ 'WARN' ] = 'warning' ;
2019-03-16 16:27:14 +01:00
return $levels ;
}
2019-03-16 20:10:22 +01:00
}