First init
This commit is contained in:
24
src/Handlers/LogForwardingHandler.php
Normal file
24
src/Handlers/LogForwardingHandler.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace HerrleinIT\LogHandler\Handlers;
|
||||
|
||||
use HerrleinIT\LogHandler\LogForwarder;
|
||||
use Monolog\Handler\AbstractProcessingHandler;
|
||||
use Monolog\Level;
|
||||
use Monolog\LogRecord;
|
||||
|
||||
class LogForwardingHandler extends AbstractProcessingHandler
|
||||
{
|
||||
public function __construct(
|
||||
private readonly LogForwarder $forwarder,
|
||||
Level $level = Level::Debug,
|
||||
bool $bubble = true,
|
||||
) {
|
||||
parent::__construct($level, $bubble);
|
||||
}
|
||||
|
||||
protected function write(LogRecord $record): void
|
||||
{
|
||||
$this->forwarder->forward($record);
|
||||
}
|
||||
}
|
||||
144
src/LogForwarder.php
Normal file
144
src/LogForwarder.php
Normal file
@@ -0,0 +1,144 @@
|
||||
<?php
|
||||
|
||||
namespace HerrleinIT\LogHandler;
|
||||
|
||||
use ErrorException;
|
||||
use GuzzleHttp\ClientInterface;
|
||||
use GuzzleHttp\Exception\GuzzleException;
|
||||
use Illuminate\Contracts\Config\Repository;
|
||||
use Monolog\LogRecord;
|
||||
use Throwable;
|
||||
|
||||
class LogForwarder
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Repository $config,
|
||||
private readonly ClientInterface $httpClient,
|
||||
) {}
|
||||
|
||||
public function forward(LogRecord $record): void
|
||||
{
|
||||
$settings = $this->config->get('loghandler', []);
|
||||
|
||||
if (! ($settings['enabled'] ?? false)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$endpoint = (string) ($settings['endpoint'] ?? '');
|
||||
$token = (string) ($settings['token'] ?? '');
|
||||
|
||||
if ($endpoint === '' || $token === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$payload = $this->buildPayload($record, $settings);
|
||||
$retryTimes = (int) ($settings['retry_times'] ?? 0);
|
||||
|
||||
for ($attempt = 0; $attempt <= $retryTimes; $attempt++) {
|
||||
try {
|
||||
$this->httpClient->request('POST', $endpoint, [
|
||||
'headers' => [
|
||||
'Accept' => 'application/json',
|
||||
'Authorization' => 'Bearer '.$token,
|
||||
'Content-Type' => 'application/json',
|
||||
],
|
||||
'json' => $payload,
|
||||
]);
|
||||
|
||||
break;
|
||||
} catch (GuzzleException|Throwable) {
|
||||
if ($attempt === $retryTimes) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function buildPayload(LogRecord $record, array $settings): array
|
||||
{
|
||||
$exception = $this->resolveThrowable($record);
|
||||
|
||||
return [
|
||||
'source' => (string) ($settings['source'] ?? 'Laravel'),
|
||||
'error' => $exception instanceof Throwable ? $exception->getMessage() : (string) $record->message,
|
||||
'type' => $this->resolveType($record, $exception),
|
||||
'file' => $this->resolveFile($exception, $record),
|
||||
'line' => $this->resolveLine($exception, $record),
|
||||
'trace' => $this->resolveTrace($exception, $record, (bool) ($settings['include_trace'] ?? false)),
|
||||
];
|
||||
}
|
||||
|
||||
private function resolveThrowable(LogRecord $record): ?Throwable
|
||||
{
|
||||
$context = $record->context;
|
||||
$exception = $context['exception'] ?? null;
|
||||
|
||||
return $exception instanceof Throwable ? $exception : null;
|
||||
}
|
||||
|
||||
private const ERROR_LEVEL_MAP = [
|
||||
E_ERROR => 'E_ERROR',
|
||||
E_WARNING => 'E_WARNING',
|
||||
E_PARSE => 'E_PARSE',
|
||||
E_NOTICE => 'E_NOTICE',
|
||||
E_CORE_ERROR => 'E_CORE_ERROR',
|
||||
E_CORE_WARNING => 'E_CORE_WARNING',
|
||||
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
|
||||
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
|
||||
E_USER_ERROR => 'E_USER_ERROR',
|
||||
E_USER_WARNING => 'E_USER_WARNING',
|
||||
E_USER_NOTICE => 'E_USER_NOTICE',
|
||||
E_STRICT => 'E_STRICT',
|
||||
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
|
||||
E_DEPRECATED => 'E_DEPRECATED',
|
||||
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
|
||||
];
|
||||
|
||||
private function resolveType(LogRecord $record, ?Throwable $exception): string
|
||||
{
|
||||
if ($exception instanceof ErrorException) {
|
||||
return self::ERROR_LEVEL_MAP[$exception->getSeverity()] ?? 'ErrorException';
|
||||
}
|
||||
|
||||
if ($exception instanceof Throwable) {
|
||||
return $exception::class;
|
||||
}
|
||||
|
||||
return 'E_'.strtoupper($record->level->getName());
|
||||
}
|
||||
|
||||
private function resolveFile(?Throwable $exception, LogRecord $record): string
|
||||
{
|
||||
if ($exception instanceof Throwable) {
|
||||
return $exception->getFile();
|
||||
}
|
||||
|
||||
return (string) ($record->context['file'] ?? 'unknown');
|
||||
}
|
||||
|
||||
private function resolveLine(?Throwable $exception, LogRecord $record): string
|
||||
{
|
||||
if ($exception instanceof Throwable) {
|
||||
return (string) $exception->getLine();
|
||||
}
|
||||
|
||||
if (isset($record->context['line'])) {
|
||||
return (string) $record->context['line'];
|
||||
}
|
||||
|
||||
return '0';
|
||||
}
|
||||
|
||||
private function resolveTrace(?Throwable $exception, LogRecord $record, bool $includeTrace): string
|
||||
{
|
||||
if (! $includeTrace) {
|
||||
return '';
|
||||
}
|
||||
|
||||
if ($exception instanceof Throwable) {
|
||||
return $exception->getTraceAsString();
|
||||
}
|
||||
|
||||
return (string) ($record->context['trace'] ?? '');
|
||||
}
|
||||
}
|
||||
63
src/LogHandlerServiceProvider.php
Normal file
63
src/LogHandlerServiceProvider.php
Normal file
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
|
||||
namespace HerrleinIT\LogHandler;
|
||||
|
||||
use GuzzleHttp\Client;
|
||||
use HerrleinIT\LogHandler\Handlers\LogForwardingHandler;
|
||||
use Illuminate\Contracts\Foundation\Application;
|
||||
use Illuminate\Support\Facades\Log;
|
||||
use Illuminate\Support\ServiceProvider;
|
||||
use Monolog\Level;
|
||||
use Monolog\Logger;
|
||||
use Throwable;
|
||||
|
||||
class LogHandlerServiceProvider extends ServiceProvider
|
||||
{
|
||||
public function register(): void
|
||||
{
|
||||
$this->mergeConfigFrom(__DIR__.'/../config/loghandler.php', 'loghandler');
|
||||
|
||||
$this->app->singleton(LogForwarder::class, function (Application $app): LogForwarder {
|
||||
$settings = $app['config']->get('loghandler', []);
|
||||
$timeoutSeconds = max(0.0, ((int) ($settings['timeout_ms'] ?? 3000)) / 1000);
|
||||
|
||||
$client = new Client([
|
||||
'timeout' => $timeoutSeconds,
|
||||
'connect_timeout' => $timeoutSeconds,
|
||||
'http_errors' => false,
|
||||
]);
|
||||
|
||||
return new LogForwarder($app['config'], $client);
|
||||
});
|
||||
}
|
||||
|
||||
public function boot(): void
|
||||
{
|
||||
if ($this->app->runningInConsole()) {
|
||||
$this->publishes([
|
||||
__DIR__.'/../config/loghandler.php' => $this->app->configPath('loghandler.php'),
|
||||
], 'loghandler-config');
|
||||
}
|
||||
|
||||
Log::extend('loghandler', function (Application $app, array $config = []): Logger {
|
||||
$logger = new Logger($config['name'] ?? 'loghandler');
|
||||
$levelOption = $config['level'] ?? null;
|
||||
|
||||
try {
|
||||
$level = $levelOption === null
|
||||
? Level::Debug
|
||||
: Level::fromName(strtoupper((string) $levelOption));
|
||||
} catch (Throwable) {
|
||||
$level = Level::Debug;
|
||||
}
|
||||
|
||||
$logger->pushHandler(new LogForwardingHandler(
|
||||
$app->make(LogForwarder::class),
|
||||
$level,
|
||||
! isset($config['bubble']) || (bool) $config['bubble'],
|
||||
));
|
||||
|
||||
return $logger;
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user