Skip to content

Feature: laravel.log summary #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
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
107 changes: 107 additions & 0 deletions src/Console/Commands/SummaryLogCommand.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Opcodes\LogViewer\Console\Commands;

use Illuminate\Console\Command;
use Illuminate\Support\Facades\Storage;
use Illuminate\Support\Str;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\LogFileCollection;

class SummaryLogCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'log-viewer:summary
{--l|logs=1000 : Number of logs to summarize }';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Generates a summary of the logs';

/**
* Execute the console command.
*/
public function handle(): void
{
$number = $this->option('logs');

static::deleteSummaryFile();
$this->comment('Deleted error log summary file');
$this->comment('Generating error log summary file');
static::generateSummaryFile($number);
$this->comment('Done');
}

protected static function deleteSummaryFile(): void
{
Storage::disk('logs')
->delete('summary.log');
}

protected static function generateSummaryFile(int $number): void
{
$summary = [];

/** @var LogFileCollection $files */
$files = LogViewer::getFiles();

foreach ($files as $file) {
if (! Str::endsWith($file->name, 'laravel.log')) {
continue;
}

$logs = collect($file->logs()->get())
->reverse()
->take($number);

foreach ($logs as $log) {
$level = $log->level;
$message = $log->message;
$context = $log->context;
$env = $log->extra['environment'];
$ts = $log->datetime->format('Y-m-d H:i:s');

if (! isset($summary[trim($message)])) {
$summary[$message] = [
'first' => $ts,
'last' => $ts,
'count' => 1,
'level' => $level,
'env' => $env,
'message' => $message,
'context' => $context,
];
} else {
$summary[trim($message)]['count']++;
$summary[trim($message)]['last'] = max(
$ts,
$summary[trim($message)]['last']
);
$summary[trim($message)]['first'] = min(
$ts,
$summary[trim($message)]['first']
);
}
}
}

$lines = array_map(function (array $data) {
return json_encode($data, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
}, array_values($summary));

$uniqueLines = [];
foreach ($lines as $json) {
$uniqueLines[$json] = true;
}

Storage::disk('logs')
->put('summary.log', implode("\n", array_reverse(array_keys($uniqueLines))));
}
}
2 changes: 2 additions & 0 deletions src/LogViewerServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Illuminate\Support\Str;
use Laravel\Octane\Events\RequestTerminated;
use Opcodes\LogViewer\Console\Commands\GenerateDummyLogsCommand;
use Opcodes\LogViewer\Console\Commands\SummaryLogCommand;
use Opcodes\LogViewer\Console\Commands\PublishCommand;
use Opcodes\LogViewer\Events\LogFileDeleted;
use Opcodes\LogViewer\Facades\LogViewer;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function boot()
$this->commands([
PublishCommand::class,
GenerateDummyLogsCommand::class,
SummaryLogCommand::class,
]);
}

Expand Down
55 changes: 55 additions & 0 deletions src/Logs/SummaryLog.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

namespace Opcodes\LogViewer\Logs;

use Carbon\Carbon;
use Opcodes\LogViewer\Facades\LogViewer;
use Opcodes\LogViewer\Logs\Log;
use Str;

class SummaryLog extends Log
{
public static string $name = 'Summary';

public static string $regex = '/^\{.*\}$/';

public static array $seen = [];

public static array $columns = [
['label' => 'Severity', 'data_path' => 'level'],
['label' => 'First', 'data_path' => 'extra.first_datetime'],
['label' => 'Last', 'data_path' => 'datetime'],
['label' => 'Env', 'data_path' => 'extra.environment'],
['label' => 'Count', 'data_path' => 'extra.count'],
['label' => 'Message', 'data_path' => 'message'],
];
protected static string $regexContextKey = 'context';

protected function parseText(array &$matches = []): void
{
$data = json_decode($this->text, true) ?: [];

$matches[static::$regexDatetimeKey] = $data['last'] ?? '';
$matches[static::$regexLevelKey] = $data['level'] ?? '';
$matches[static::$regexMessageKey] = $data['message'] ?? '';
$matches[static::$regexContextKey] = $data['context'] ?? [];

$this->extra = [
'first_datetime' => Carbon::parse($data['first'] ?? null)
->setTimezone(LogViewer::timezone())
->format("Y\u{2011}m\u{2011}d\u{00A0}H:i:s"),
'environment' => $data['env'] ?? null,
'count' => $data['count'] ?? null,
'context' => $data['context'] ?? [],
];

$this->text = $data['message'] ?? '';
}

protected function fillMatches(array $matches = []): void
{
parent::fillMatches($matches);

$this->context = $matches[static::$regexContextKey];
}
}