fedi-feed-router/backend/app/Services/Log/LogSaver.php
myrmidex d2416a3ae2 Fix tests
+ Move LogLevelEnum
2025-08-05 21:27:42 +02:00

65 lines
1.8 KiB
PHP

<?php
namespace App\Services\Log;
use App\Enums\LogLevelEnum;
use App\Models\Log;
use App\Models\PlatformChannel;
class LogSaver
{
/**
* @param array<string, mixed> $context
*/
public static function info(string $message, ?PlatformChannel $channel = null, array $context = []): void
{
self::log(LogLevelEnum::INFO, $message, $channel, $context);
}
/**
* @param array<string, mixed> $context
*/
public static function error(string $message, ?PlatformChannel $channel = null, array $context = []): void
{
self::log(LogLevelEnum::ERROR, $message, $channel, $context);
}
/**
* @param array<string, mixed> $context
*/
public static function warning(string $message, ?PlatformChannel $channel = null, array $context = []): void
{
self::log(LogLevelEnum::WARNING, $message, $channel, $context);
}
/**
* @param array<string, mixed> $context
*/
public static function debug(string $message, ?PlatformChannel $channel = null, array $context = []): void
{
self::log(LogLevelEnum::DEBUG, $message, $channel, $context);
}
/**
* @param array<string, mixed> $context
*/
private static function log(LogLevelEnum $level, string $message, ?PlatformChannel $channel = null, array $context = []): void
{
$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,
]);
}
}