fedi-feed-router/app/Services/Log/LogSaver.php

66 lines
1.8 KiB
PHP
Raw Normal View History

2025-07-05 18:26:04 +02:00
<?php
namespace App\Services\Log;
2025-08-05 21:15:17 +02:00
use App\Enums\LogLevelEnum;
2025-07-05 18:26:04 +02:00
use App\Models\Log;
use App\Models\PlatformChannel;
class LogSaver
{
2025-07-07 00:51:32 +02:00
/**
* @param array<string, mixed> $context
*/
2025-08-15 02:50:42 +02:00
public function info(string $message, ?PlatformChannel $channel = null, array $context = []): void
2025-07-05 18:26:04 +02:00
{
2025-08-15 02:50:42 +02:00
$this->log(LogLevelEnum::INFO, $message, $channel, $context);
2025-07-05 18:26:04 +02:00
}
2025-07-07 00:51:32 +02:00
/**
* @param array<string, mixed> $context
*/
2025-08-15 02:50:42 +02:00
public function error(string $message, ?PlatformChannel $channel = null, array $context = []): void
2025-07-05 18:26:04 +02:00
{
2025-08-15 02:50:42 +02:00
$this->log(LogLevelEnum::ERROR, $message, $channel, $context);
2025-07-05 18:26:04 +02:00
}
2025-07-07 00:51:32 +02:00
/**
* @param array<string, mixed> $context
*/
2025-08-15 02:50:42 +02:00
public function warning(string $message, ?PlatformChannel $channel = null, array $context = []): void
2025-07-05 18:26:04 +02:00
{
2025-08-15 02:50:42 +02:00
$this->log(LogLevelEnum::WARNING, $message, $channel, $context);
2025-07-05 18:26:04 +02:00
}
2025-07-07 00:51:32 +02:00
/**
* @param array<string, mixed> $context
*/
2025-08-15 02:50:42 +02:00
public function debug(string $message, ?PlatformChannel $channel = null, array $context = []): void
2025-07-05 18:26:04 +02:00
{
2025-08-15 02:50:42 +02:00
$this->log(LogLevelEnum::DEBUG, $message, $channel, $context);
2025-07-05 18:26:04 +02:00
}
2025-07-07 00:51:32 +02:00
/**
* @param array<string, mixed> $context
*/
2025-08-15 02:50:42 +02:00
private function log(LogLevelEnum $level, string $message, ?PlatformChannel $channel = null, array $context = []): void
2025-07-05 18:26:04 +02:00
{
$logContext = $context;
if ($channel) {
$logContext = array_merge($logContext, [
'channel_id' => $channel->id,
'channel_name' => $channel->name,
'platform' => $channel->platformInstance->platform->value,
'instance_url' => $channel->platformInstance->url,
]);
}
Log::create([
'level' => $level,
'message' => $message,
'context' => $logContext,
]);
}
2025-08-05 21:15:17 +02:00
}