DolLogsCollector.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. use DebugBar\DataCollector\MessagesCollector;
  3. use Psr\Log\LogLevel;
  4. //use ReflectionClass;
  5. /**
  6. * DolLogsCollector class
  7. */
  8. class DolLogsCollector extends MessagesCollector
  9. {
  10. /**
  11. * @var string default logs file path
  12. */
  13. protected $path;
  14. /**
  15. * @var int number of lines to show
  16. */
  17. protected $maxnboflines;
  18. /**
  19. * @var int number of lines
  20. */
  21. protected $nboflines;
  22. /**
  23. * Constructor
  24. *
  25. * @param string $path Path
  26. * @param string $name Name
  27. */
  28. public function __construct($path = null, $name = 'logs')
  29. {
  30. global $conf;
  31. parent::__construct($name);
  32. $this->nboflines = 0;
  33. $this->maxnboflines = getDolGlobalInt('DEBUGBAR_LOGS_LINES_NUMBER', 250); // High number slows seriously output
  34. $this->path = $path ?: $this->getLogsFile();
  35. }
  36. /**
  37. * Return widget settings
  38. *
  39. * @return array Array
  40. */
  41. public function getWidgets()
  42. {
  43. global $langs;
  44. $title = $langs->transnoentities('Logs');
  45. $name = $this->getName();
  46. return array(
  47. "$title" => array(
  48. "icon" => "list-alt",
  49. "widget" => "PhpDebugBar.Widgets.MessagesWidget",
  50. "map" => "$name.messages",
  51. "default" => "[]"
  52. ),
  53. "$title:badge" => array(
  54. "map" => "$name.count",
  55. "default" => "null"
  56. )
  57. );
  58. }
  59. /**
  60. * Return collected data
  61. *
  62. * @return array Array
  63. */
  64. public function collect()
  65. {
  66. global $conf;
  67. $uselogfile = getDolGlobalInt('DEBUGBAR_USE_LOG_FILE');
  68. if ($uselogfile) {
  69. $this->getStorageLogs($this->path);
  70. } else {
  71. $log_levels = $this->getLevels();
  72. foreach ($conf->logbuffer as $line) {
  73. if ($this->nboflines >= $this->maxnboflines) {
  74. break;
  75. }
  76. foreach ($log_levels as $level_key => $level) {
  77. if (strpos(strtolower($line), strtolower($level_key)) == 20) {
  78. $this->nboflines++;
  79. $this->addMessage($line, $level, false);
  80. }
  81. }
  82. }
  83. }
  84. return parent::collect();
  85. }
  86. /**
  87. * Get the path to the logs file
  88. *
  89. * @return string
  90. */
  91. public function getLogsFile()
  92. {
  93. // default dolibarr log file
  94. $path = DOL_DATA_ROOT.'/dolibarr.log';
  95. return $path;
  96. }
  97. /**
  98. * Get logs
  99. *
  100. * @param string $path Path
  101. * @return array
  102. */
  103. public function getStorageLogs($path)
  104. {
  105. if (!file_exists($path)) {
  106. return;
  107. }
  108. // Load the latest lines
  109. $file = implode("", $this->tailFile($path, $this->maxnboflines));
  110. foreach ($this->getLogs($file) as $log) {
  111. $this->addMessage($log['line'], $log['level'], false);
  112. }
  113. }
  114. /**
  115. * Get latest file lines
  116. *
  117. * @param string $file File
  118. * @param int $lines Lines
  119. * @return array Array
  120. */
  121. protected function tailFile($file, $lines)
  122. {
  123. $handle = fopen($file, "r");
  124. $linecounter = $lines;
  125. $pos = -2;
  126. $beginning = false;
  127. $text = array();
  128. while ($linecounter > 0) {
  129. $t = " ";
  130. while ($t != "\n") {
  131. if (fseek($handle, $pos, SEEK_END) == -1) {
  132. $beginning = true;
  133. break;
  134. }
  135. $t = fgetc($handle);
  136. $pos--;
  137. }
  138. $linecounter--;
  139. if ($beginning) {
  140. rewind($handle);
  141. }
  142. $text[$lines - $linecounter - 1] = fgets($handle);
  143. if ($beginning) {
  144. break;
  145. }
  146. }
  147. fclose($handle);
  148. return array_reverse($text);
  149. }
  150. /**
  151. * Search a string for log entries
  152. *
  153. * @param string $file File
  154. * @return array Lines of logs
  155. */
  156. public function getLogs($file)
  157. {
  158. $pattern = "/\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.*/";
  159. $log_levels = $this->getLevels();
  160. preg_match_all($pattern, $file, $matches);
  161. $log = array();
  162. foreach ($matches as $lines) {
  163. foreach ($lines as $line) {
  164. foreach ($log_levels as $level_key => $level) {
  165. if (strpos(strtolower($line), strtolower($level_key)) == 20) {
  166. $log[] = array('level' => $level, 'line' => $line);
  167. }
  168. }
  169. }
  170. }
  171. $log = array_reverse($log);
  172. return $log;
  173. }
  174. /**
  175. * Get the log levels from psr/log.
  176. *
  177. * @return array Array of log level
  178. */
  179. public function getLevels()
  180. {
  181. $class = new ReflectionClass(new LogLevel());
  182. $levels = $class->getConstants();
  183. $levels['ERR'] = 'error';
  184. $levels['WARN'] = 'warning';
  185. return $levels;
  186. }
  187. }