Skip to content

Commit

Permalink
feat(LogsCollector): support multiple log files (barryvdh#1560)
Browse files Browse the repository at this point in the history
* feat(LogsCollector): support multiple log files

* Add log basename to locate the file

* Fix daily driver logging
  • Loading branch information
erikn69 authored Mar 24, 2024
1 parent d861099 commit 4aaf214
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 15 deletions.
40 changes: 25 additions & 15 deletions src/DataCollector/LogsCollector.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Barryvdh\Debugbar\DataCollector;

use DebugBar\DataCollector\MessagesCollector;
use Illuminate\Support\Arr;
use Psr\Log\LogLevel;
use ReflectionClass;

Expand All @@ -14,18 +15,14 @@ public function __construct($path = null, $name = 'logs')
{
parent::__construct($name);

$path = $path ?: $this->getLogsFile();
$this->getStorageLogs($path);
}

/**
* Get the path to the logs file
*
* @return string
*/
public function getLogsFile()
{
return storage_path() . '/logs/laravel.log';
$paths = Arr::wrap($path ?: [
storage_path('logs/laravel.log'),
storage_path('logs/laravel-' . date('Y-m-d') . '.log'), // for daily driver
]);

foreach ($paths as $path) {
$this->getStorageLogs($path);
}
}

/**
Expand All @@ -44,9 +41,16 @@ public function getStorageLogs($path)

//Load the latest lines, guessing about 15x the number of log entries (for stack traces etc)
$file = implode("", $this->tailFile($path, $this->lines));
$basename = basename($path);

foreach ($this->getLogs($file) as $log) {
$this->addMessage($log['header'] . $log['stack'], $log['level'], false);
$this->messages[] = [
'message' => $log['header'] . $log['stack'],
'label' => $log['level'],
'time' => substr($log['header'], 1, 19),
'collector' => $basename,
'is_string' => false,
];
}
}

Expand Down Expand Up @@ -115,11 +119,17 @@ public function getLogs($file)
}
}

$log = array_reverse($log);

return $log;
}

/**
* @return array
*/
public function getMessages()
{
return array_reverse(parent::getMessages());
}

/**
* Get the log levels from psr/log.
* Based on https://github.com/mikemand/logviewer/blob/master/src/Kmd/Logviewer/Logviewer.php by mikemand
Expand Down
5 changes: 5 additions & 0 deletions src/Resources/laravel-debugbar.css
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,11 @@ div.phpdebugbar-widgets-messages li.phpdebugbar-widgets-list-item span.phpdebugb
color: #333;
}

div.phpdebugbar-widgets-messages li.phpdebugbar-widgets-list-item span.phpdebugbar-widgets-collector {
text-transform: none;
color: #888;
}

.phpdebugbar-widgets-toolbar i.phpdebugbar-fa.phpdebugbar-fa-search {
position: relative;
top: -1px;
Expand Down

0 comments on commit 4aaf214

Please sign in to comment.