Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 61 additions & 4 deletions src/Concerns/ReadsLogs.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Laravel\Boost\Concerns;

use Illuminate\Support\Arr;
use Illuminate\Support\Facades\Config;

trait ReadsLogs
Expand Down Expand Up @@ -45,11 +46,67 @@ protected function resolveLogFilePath(): string
$channel = Config::get('logging.default');
$channelConfig = Config::get("logging.channels.{$channel}");

if (($channelConfig['driver'] ?? null) === 'daily') {
return storage_path('logs/laravel-'.date('Y-m-d').'.log');
$channelConfig = $this->resolveChannelWithPath($channelConfig);

$baseLogPath = Arr::get($channelConfig, 'path', storage_path('logs/laravel.log'));

if (Arr::get($channelConfig, 'driver') === 'daily') {
return $this->resolveDailyLogFilePath($baseLogPath);
}

return $baseLogPath;
}

/**
* @param array<string, mixed>|null $channelConfig
* @return array<string, mixed>|null
*/
protected function resolveChannelWithPath(?array $channelConfig, int $depth = 0): ?array
{
if ($channelConfig === null || $depth > 2) {
return $channelConfig;
}

return storage_path('logs/laravel.log');
if (isset($channelConfig['path'])) {
return $channelConfig;
}

if (($channelConfig['driver'] ?? null) !== 'stack') {
return $channelConfig;
}

$firstValidLoggerConfig = collect($channelConfig['channels'] ?? [])
->map(fn (string $name) => Config::get("logging.channels.{$name}"))
->filter(fn ($config): bool => is_array($config))
->map(fn (array $config) => $this->resolveChannelWithPath($config, $depth + 1))
->first(fn (?array $config): bool => isset($config['path']));

return $firstValidLoggerConfig ?? $channelConfig;
}

protected function resolveDailyLogFilePath(string $basePath): string
{
$pathInfo = pathinfo($basePath);
$directory = $pathInfo['dirname'];
$filename = $pathInfo['filename'];
$extension = isset($pathInfo['extension']) ? '.'.$pathInfo['extension'] : '';

$todayLogFile = $directory.DIRECTORY_SEPARATOR.$filename.'-'.date('Y-m-d').$extension;

if (file_exists($todayLogFile)) {
return $todayLogFile;
}

$pattern = $directory.DIRECTORY_SEPARATOR.$filename.'-*'.$extension;
$files = glob($pattern) ?: [];

$datePattern = '/^'.preg_quote($filename, '/').'-\d{4}-\d{2}-\d{2}'.preg_quote($extension, '/').'$/';
$latestFile = collect($files)
->filter(fn ($file): int|false => preg_match($datePattern, basename($file)))
->sortDesc()
->first();

return $latestFile ?? $todayLogFile;
}

/**
Expand Down Expand Up @@ -130,7 +187,7 @@ protected function scanLogChunkForEntries(string $logFile, int $chunkSize): arra
$offset = max($fileSize - $chunkSize, 0);
fseek($handle, $offset);

// If we started mid-line, discard the partial line to align to next newline.
// If we started mid-line, discard the partial line to align to the next newline.
if ($offset > 0) {
fgets($handle);
}
Expand Down
Loading