Skip to content

Commit

Permalink
Add logic to show log files
Browse files Browse the repository at this point in the history
  • Loading branch information
denniseilander committed Dec 6, 2023
1 parent 86efae8 commit e507e48
Show file tree
Hide file tree
Showing 6 changed files with 202 additions and 23 deletions.
45 changes: 45 additions & 0 deletions resources/views/livewire/log-files.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<x-pulse::card :cols="$cols" :rows="$rows" :class="$class">
<x-pulse::card-header
name="Log Files"
title="Log Files"
details="Track Application Log Files"
>
<x-slot:icon>
<x-pulse::icons.circle-stack />
</x-slot:icon>
</x-pulse::card-header>

<x-pulse::scroll :expand="$expand" wire:poll.5s="">
<div class="min-h-full flex flex-col">
@if ($servers->isNotEmpty())
@foreach ($servers as $server)
<x-pulse::table>
<x-pulse::thead>
<tr>
<x-pulse::th>Logfile</x-pulse::th>
<x-pulse::th class="text-right">Size</x-pulse::th>
</tr>
</x-pulse::thead>
<tbody>
@foreach ($server->logFiles as $logFile)
<tr class="h-2 first:h-0"></tr>
<tr wire:key="log-file-{{ $logFile->name }}">
<x-pulse::td>
<div class="flex items-center" title="{{ $logFile->name }}">
<div>{{ $logFile->name }}</div>
</div>
</x-pulse::td>
<x-pulse::td numeric class="text-gray-700 dark:text-gray-300 font-bold">
{{ $logFile->readableSize }}
</x-pulse::td>
</tr>
@endforeach
</tbody>
</x-pulse::table>
@endforeach
@else
<x-pulse::no-results />
@endif
</div>
</x-pulse::scroll>
</x-pulse::card>
59 changes: 59 additions & 0 deletions src/DataTransferObjects/LogFile.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

namespace Denniseilander\LogFiles\DataTransferObjects;

use Illuminate\Support\Collection;
use Illuminate\Support\Number;

class LogFile
{
public function __construct(
public string $name,
public string $path,
public int $size,
public string $readableSize,
) {
}

/**
* @return array<string, mixed>
*/
public function toArray(): array
{
return [
'name' => $this->name,
'path' => $this->path,
'size' => $this->size,
'readable_size' => $this->readableSize,
];
}

public static function fromPath(string $path): self
{
return new self(
name: basename($path),
path: $path,
size: filesize($path),
readableSize: Number::fileSize(filesize($path)),
);
}

/**
* @throws \JsonException
*/
public static function multipleFromJson(string $json): Collection
{
$data = json_decode(
json: $json,
associative: true,
depth: 512,
flags: JSON_THROW_ON_ERROR
);

return Collection::make($data)->map(function ($item) {
return new self(...array_values($item));
});
}
}
35 changes: 35 additions & 0 deletions src/Livewire/LogFiles.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

declare(strict_types=1);

namespace Denniseilander\LogFiles\Livewire;

use Carbon\CarbonImmutable;
use Denniseilander\LogFiles\DataTransferObjects\LogFile;
use Illuminate\Contracts\View\View;
use Illuminate\Support\Facades\View as ViewFacade;
use Laravel\Pulse\Facades\Pulse;
use Laravel\Pulse\Livewire\Card;
use Livewire\Attributes\Lazy;

#[Lazy]
class LogFiles extends Card
{
public function render(): View
{
$servers = Pulse::values('log_files')
->map(function (object $logFile) {
$logFiles = LogFile::multipleFromJson($logFile->value);

return (object) [
'serverName' => $logFile->key,
'logFiles' => $logFiles,
'updated_at' => CarbonImmutable::createFromTimestamp($logFile->timestamp),
];
});

return ViewFacade::make('pulse-log-files::livewire.log-files', [
'servers' => $servers,
]);
}
}
28 changes: 28 additions & 0 deletions src/LogFilesServiceProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Denniseilander\LogFiles;

use Denniseilander\LogFiles\Livewire\LogFiles;
use Illuminate\Foundation\Application;
use Livewire\LivewireManager;
use Spatie\LaravelPackageTools\Package;
use Spatie\LaravelPackageTools\PackageServiceProvider;

class LogFilesServiceProvider extends PackageServiceProvider
{
public function configurePackage(Package $package): void
{
$package
->name('pulse-log-files')
->hasViews();
}

public function packageBooted(): void
{
$this->callAfterResolving('livewire', function (LivewireManager $livewire, Application $app) {
$livewire->component('pulse.log-files', LogFiles::class);
});
}
}
41 changes: 18 additions & 23 deletions src/Recorders/LogFiles.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@

namespace Denniseilander\LogFiles\Recorders;

use App\Services\Pulse\Connections\AbstractConnection;
use Denniseilander\LogFiles\DataTransferObjects\LogFile;
use Denniseilander\LogFiles\Services\LogFilesService;
use Illuminate\Config\Repository;
use Illuminate\Support\Collection;
use Illuminate\Support\Number;
use Illuminate\Support\Arr;
use Illuminate\Support\Str;
use Laravel\Pulse\Events\SharedBeat;
use Laravel\Pulse\Pulse;
Expand All @@ -21,44 +21,39 @@ class LogFiles
*/
public string $listen = SharedBeat::class;

/**
* Create a new recorder instance.
*/
public function __construct(
protected Pulse $pulse,
protected Repository $config
protected Repository $config,
protected LogFilesService $logFilesService,
) {
//
}

/**
* Record the database sizes.
* Record the log files.
*/
public function record(SharedBeat $event): void
{
// Only run every 5 minutes
//if ($event->time->second % 300 !== 0) {
// return;
//}
$config = $this->config->get('pulse.recorders.'.self::class);
$runEverySeconds = Arr::get($config, 'run_every_seconds', 300);

if ($event->time->second % $runEverySeconds !== 0) {
return;
}

$server = $this->config->get('pulse.recorders.'.self::class.'.server_name');
$slug = Str::slug($server);

$logFiles = [];
foreach (glob(storage_path('logs/*.log')) as $filePath) {
$logFiles[] = [
'name' => basename($filePath),
'size' => $size = filesize($filePath),
'readable_size' => Number::fileSize($size),
];
info('Log file size', ['name' => basename($filePath), 'size' => $size]);
}
$logFiles = $this->logFilesService
->getFiles()
->map(fn (LogFile $logFile) => $logFile->toArray())
->toJson();

$this->pulse->set(
type: 'log_files',
key: $slug,
value: json_encode($logFiles, flags: JSON_THROW_ON_ERROR),
value: $logFiles,
timestamp: $event->time
);
}
}
}
17 changes: 17 additions & 0 deletions src/Services/LogFilesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace Denniseilander\LogFiles\Services;

use Denniseilander\LogFiles\DataTransferObjects\LogFile;
use Illuminate\Support\Collection;

class LogFilesService
{
public function getFiles(): Collection
{
return Collection::make(glob(storage_path('logs/*.log')) ?: [])
->map(fn (string $logFile) => LogFile::fromPath($logFile));
}
}

0 comments on commit e507e48

Please sign in to comment.