50 lines
1.5 KiB
PHP
50 lines
1.5 KiB
PHP
<?php
|
|
|
|
namespace App\Logging;
|
|
|
|
use Monolog\Logger;
|
|
|
|
class DiscordLogger
|
|
{
|
|
public function __invoke(array $config): Logger
|
|
{
|
|
$logger = new Logger((string) ($config['name'] ?? 'discord-errors'));
|
|
|
|
$logger->pushHandler(new DiscordWebhookHandler(
|
|
webhookUrl: $this->nullableString($config['url'] ?? null),
|
|
level: $config['level'] ?? 'error',
|
|
bubble: (bool) ($config['bubble'] ?? true),
|
|
mention: $this->nullableString($config['mention'] ?? null),
|
|
title: (string) ($config['title'] ?? '## Laravel error'),
|
|
timeout: $this->floatValue($config['timeout'] ?? 3),
|
|
connectTimeout: $this->floatValue($config['connect_timeout'] ?? 3),
|
|
maxContentLength: $this->intValue($config['max_content_length'] ?? 2000, 2000),
|
|
maxTraceLines: $this->intValue($config['max_trace_lines'] ?? 12, 12),
|
|
maxContextLength: $this->intValue($config['max_context_length'] ?? 1200, 1200),
|
|
));
|
|
|
|
return $logger;
|
|
}
|
|
|
|
private function nullableString(mixed $value): ?string
|
|
{
|
|
if (! is_string($value)) {
|
|
return null;
|
|
}
|
|
|
|
$value = trim($value);
|
|
|
|
return $value === '' ? null : $value;
|
|
}
|
|
|
|
private function floatValue(mixed $value): float
|
|
{
|
|
return is_numeric($value) && (float) $value > 0 ? (float) $value : 3.0;
|
|
}
|
|
|
|
private function intValue(mixed $value, int $fallback): int
|
|
{
|
|
return is_numeric($value) && (int) $value > 0 ? (int) $value : $fallback;
|
|
}
|
|
}
|