first commit
Some checks failed
Build / run (push) Has been cancelled

This commit is contained in:
maher
2025-10-29 11:42:25 +01:00
commit 703f50a09d
4595 changed files with 385164 additions and 0 deletions

View File

@@ -0,0 +1,27 @@
<?php
namespace Common\Logging;
use Common\Logging\Mail\OutgoingEmailLogItem;
use Common\Logging\Schedule\ScheduleLogItem;
use Illuminate\Console\Command;
class CleanLogTables extends Command
{
protected $signature = 'app-logs:clean';
protected $description = 'Delete old log entries from the database.';
public function handle()
{
ScheduleLogItem::where('ran_at', '<', now()->subDays(30))->delete();
OutgoingEmailLogItem::where(
'created_at',
'<',
now()->subDays(7),
)->delete();
$this->info('Old log entries have been deleted.');
return self::SUCCESS;
}
}

View File

@@ -0,0 +1,109 @@
<?php
namespace Common\Logging\Error;
use Common\Core\BaseController;
use Opcodes\LogViewer\Facades\LogViewer;
class ErrorLogController extends BaseController
{
public function __construct()
{
$this->middleware('isAdmin');
}
public function index()
{
$files = LogViewer::getFiles()
->sortByLatestFirst()
->values();
$perPage = request('perPage', 20);
if ($files->isEmpty()) {
$pagination = $this->emptyPagination();
} else {
$file = request('file')
? $files->firstWhere('identifier', request('file'))
: $files->first();
if (!$file) {
$pagination = $this->emptyPagination();
} else {
$logQuery = $file->logs();
if (request('query')) {
$logQuery->search(request('query'));
}
$pagination = $logQuery
->reverse()
->scan()
->paginate($perPage);
$pagination->through(
fn($log) => [
'id' => $log->index,
'index' => $log->index,
'level' => strtolower($log->level),
'datetime' => $log->datetime,
'message' => $log->message,
'exception' => $log->context['exception'] ?? null,
],
);
}
}
return $this->success([
'selectedFile' => $files->first()?->identifier,
'files' => $files->map(
fn($file) => [
'name' => $file->name,
'identifier' => $file->identifier,
'size' => $file->size(),
],
),
'pagination' => $pagination,
]);
}
public function download(string $identifier)
{
$file = LogViewer::getFile($identifier);
return $file->download();
}
public function downloadLatest()
{
$file = LogViewer::getFiles()
->sortByLatestFirst()
->first();
return $file->download();
}
public function destroy(string $fileIdentifier)
{
$file = LogViewer::getFile($fileIdentifier);
if (!is_null($file)) {
$file->delete();
}
return $this->success([]);
}
protected function emptyPagination(): array
{
return [
'current_page' => 1,
'data' => [],
'from' => 1,
'last_page' => 1,
'path' => null,
'per_page' => (int) request('perPage', 20),
'to' => 0,
'total' => 0,
];
}
}

View File

@@ -0,0 +1,75 @@
<?php
namespace Common\Logging\Mail;
use Common\Core\BaseController;
use Common\Database\Datasource\Datasource;
use ZBateson\MailMimeParser\Message;
class OutgoingEmailLogController extends BaseController
{
public function __construct()
{
$this->middleware('isAdmin');
}
public function show(int $id): mixed
{
$logItem = OutgoingEmailLogItem::findOrFail($id);
$message = Message::from($logItem->mime, true);
$logItem->parsed_message = [
'headers' => collect($message->getAllHeaders())->mapWithKeys(
fn($header) => [$header->getName() => $header->getValue()],
),
'body' => [
'text' => $message->getTextContent(),
'html' => $message->getHtmlContent(),
],
];
return $this->success([
'logItem' => $logItem,
]);
}
public function index(): mixed
{
$pagination = (new Datasource(
OutgoingEmailLogItem::query(),
request()->all(),
))->paginate();
return $this->success([
'pagination' => $pagination,
]);
}
public function downloadLog()
{
$log = json_encode(
OutgoingEmailLogItem::limit(1000)->get(),
JSON_PRETTY_PRINT,
);
return response($log)
->header('Content-Type', 'application/json')
->header(
'Content-Disposition',
'attachment; filename="outgoing-email-log.json"',
);
}
public function downloadLogItem(int $id)
{
$logItem = OutgoingEmailLogItem::findOrFail($id);
return response($logItem->mime)
->header('Content-Type', 'message/rfc822')
->header(
'Content-Disposition',
"attachment; filename=\"{$logItem->subject}.eml\"",
);
}
}

View File

@@ -0,0 +1,55 @@
<?php
namespace Common\Logging\Mail;
use Common\Core\BaseModel;
use Illuminate\Database\Eloquent\Casts\Attribute;
class OutgoingEmailLogItem extends BaseModel
{
const MODEL_TYPE = 'outgoing_email_log_item';
protected $table = 'outgoing_email_log';
protected $guarded = ['id'];
protected $casts = [
'id' => 'integer',
];
protected $hidden = ['mime'];
protected function mime(): Attribute
{
return Attribute::make(get: fn(string $value) => utf8_decode($value));
}
public static function filterableFields(): array
{
return ['id', 'status', 'from', 'to', 'created_at'];
}
public function toNormalizedArray(): array
{
return [
'id' => $this->id,
'name' => $this->subject,
'description' => $this->message_id,
'model_type' => self::MODEL_TYPE,
];
}
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'subject' => $this->subject,
'to' => $this->to,
];
}
public static function getModelTypeAttribute(): string
{
return self::MODEL_TYPE;
}
}

View File

@@ -0,0 +1,51 @@
<?php
namespace Common\Logging\Mail;
use Illuminate\Events\Dispatcher;
use Illuminate\Mail\Events\MessageSending;
use Illuminate\Mail\Events\MessageSent;
class OutgoingEmailLogSubscriber
{
public function handleSending(MessageSending $event): void
{
$headers = $event->message->getPreparedHeaders();
$logItem = OutgoingEmailLogItem::create([
'message_id' => $headers->get('Message-ID')->getBodyAsString(),
'from' => $headers->get('From')->getBodyAsString(),
'to' => $headers->get('To')->getBodyAsString(),
'subject' => $headers->get('Subject')->getBodyAsString(),
'mime' => utf8_encode($event->message->toString()),
'status' => 'not-sent',
]);
$event->message
->getHeaders()
->addTextHeader('X-BE-LOG-ID', $logItem->id);
}
public function handleSent(MessageSent $event): void
{
$logId = $event->message
->getHeaders()
->get('X-BE-LOG-ID')
->getBodyAsString();
OutgoingEmailLogItem::where('id', $logId)->update([
'status' => 'sent',
'message_id' => $event->sent
->getSymfonySentMessage()
->getMessageId(),
]);
}
public function subscribe(Dispatcher $events): array
{
return [
MessageSending::class => 'handleSending',
MessageSent::class => 'handleSent',
];
}
}

View File

@@ -0,0 +1,76 @@
<?php
namespace Common\Logging\Schedule;
use Illuminate\Console\Scheduling\Event;
use Illuminate\Console\Scheduling\Schedule;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Stringable;
use Symfony\Component\Stopwatch\Stopwatch;
trait MonitorsSchedule
{
protected function monitorSchedule(Schedule $schedule): void
{
collect($schedule->events())->each(function (Event $event) {
$logItem = new ScheduleLogItem();
$stopwatch = new Stopwatch(true);
$event->before(function () use ($event, $stopwatch, $logItem) {
$logItem->ran_at = now();
$stopwatch->start($event->command);
});
$event->after(function (Stringable $output) use (
$event,
$stopwatch,
$logItem,
) {
$stopwatch->stop($event->command);
$commandParts = collect(explode(' ', $event->command));
$artisanIndex = $commandParts->search(
fn($str) => trim($str, '\'"') === 'artisan',
);
$signature = $commandParts->get($artisanIndex + 1);
$namespace = get_class(Artisan::all()[$signature]);
// check if command already ran with the same signature and exit code in the last hour
$lastLogItem = ScheduleLogItem::query()
->where('command', $namespace)
->when(
// only keep one log item of ScheduleHealthCommand
$namespace !== ScheduleHealthCommand::class,
function ($query) use ($event) {
$query
->where('exit_code', $event->exitCode)
->where('ran_at', '>=', now()->subHour());
},
)
->first();
$data = [
'command' => $namespace,
'output' => trim($output->limit(1000)->toString(), "\n"),
'exit_code' => $event->exitCode,
'duration' => $stopwatch
->getEvent($event->command)
->getDuration(),
];
if ($lastLogItem) {
$lastLogItem
->fill([
...$data,
'ran_at' => $logItem->ran_at,
'count_in_last_hour' =>
$lastLogItem->count_in_last_hour + 1,
])
->save();
} else {
$logItem->fill($data)->save();
}
});
});
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Common\Logging\Schedule;
use Illuminate\Console\Command;
class ScheduleHealthCommand extends Command
{
protected $signature = 'schedule:be-health';
public function handle(): int
{
$this->info('CRON schedule is running properly.');
return Command::SUCCESS;
}
}

View File

@@ -0,0 +1,58 @@
<?php
namespace Common\Logging\Schedule;
use Common\Core\BaseController;
use Common\Database\Datasource\Datasource;
use Illuminate\Support\Facades\Artisan;
class ScheduleLogController extends BaseController
{
public function __construct()
{
$this->middleware('isAdmin');
}
public function index(): mixed
{
$params = request()->all();
if (!isset($params['orderBy'])) {
$params['orderBy'] = 'ran_at';
}
$pagination = (new Datasource(
ScheduleLogItem::query(),
$params,
))->paginate();
return $this->success([
'pagination' => $pagination,
]);
}
public function download()
{
$log = json_encode(
ScheduleLogItem::limit(1000)->get(),
JSON_PRETTY_PRINT,
);
return response($log)
->header('Content-Type', 'application/json')
->header(
'Content-Disposition',
'attachment; filename="schedule-log.json"',
);
}
public function rerun(int $id): mixed
{
$logItem = ScheduleLogItem::findOrFail($id);
Artisan::call($logItem->command);
$logItem->increment('count_in_last_hour');
return $this->success();
}
}

View File

@@ -0,0 +1,61 @@
<?php
namespace Common\Logging\Schedule;
use Common\Core\BaseModel;
class ScheduleLogItem extends BaseModel
{
const MODEL_TYPE = 'schedule_log_item';
protected $table = 'schedule_log';
protected $guarded = ['id'];
protected $casts = [
'id' => 'integer',
'ran_at' => 'datetime',
'duration' => 'integer',
'count_in_last_hour' => 'integer',
'exit_code' => 'integer',
];
public $timestamps = false;
public static function scheduleRanInLast30Minutes(): bool
{
return (new self())
->where('command', ScheduleHealthCommand::class)
->whereBetween('ran_at', [now()->subMinutes(30), now()])
->exists();
}
public static function filterableFields(): array
{
return ['id', 'ran_at', 'duration', 'count_in_last_hour', 'exit_code'];
}
public function toNormalizedArray(): array
{
return [
'id' => $this->id,
'name' => $this->command,
'description' => $this->output,
'model_type' => self::MODEL_TYPE,
];
}
public function toSearchableArray(): array
{
return [
'id' => $this->id,
'command' => $this->command,
'ran_at' => $this->ran_at->timestamp ?? '_null',
];
}
public static function getModelTypeAttribute(): string
{
return self::MODEL_TYPE;
}
}