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

65 lines
No EOL
1.8 KiB
PHP

<?php
namespace App\Services\Log;
use App\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,
]);
}
}