Exclude Log Levels
This commit is contained in:
@@ -8,4 +8,8 @@ return [
|
|||||||
'timeout_ms' => env('LOGHANDLER_TIMEOUT_MS', 3000),
|
'timeout_ms' => env('LOGHANDLER_TIMEOUT_MS', 3000),
|
||||||
'retry_times' => env('LOGHANDLER_RETRY_TIMES', 0),
|
'retry_times' => env('LOGHANDLER_RETRY_TIMES', 0),
|
||||||
'include_trace' => env('LOGHANDLER_INCLUDE_TRACE', false),
|
'include_trace' => env('LOGHANDLER_INCLUDE_TRACE', false),
|
||||||
|
'excluded_levels' => array_values(array_filter(array_map(
|
||||||
|
static fn (string $level): string => strtolower($level),
|
||||||
|
array_map('trim', explode(',', (string) env('LOGHANDLER_EXCLUDE_LEVELS', 'debug,info')))
|
||||||
|
))),
|
||||||
];
|
];
|
||||||
|
|||||||
@@ -6,8 +6,10 @@ use ErrorException;
|
|||||||
use GuzzleHttp\ClientInterface;
|
use GuzzleHttp\ClientInterface;
|
||||||
use GuzzleHttp\Exception\GuzzleException;
|
use GuzzleHttp\Exception\GuzzleException;
|
||||||
use Illuminate\Contracts\Config\Repository;
|
use Illuminate\Contracts\Config\Repository;
|
||||||
|
use Monolog\Level;
|
||||||
use Monolog\LogRecord;
|
use Monolog\LogRecord;
|
||||||
use Throwable;
|
use Throwable;
|
||||||
|
use ValueError;
|
||||||
|
|
||||||
class LogForwarder
|
class LogForwarder
|
||||||
{
|
{
|
||||||
@@ -24,6 +26,12 @@ class LogForwarder
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
$excludedLevels = $this->resolveExcludedLevels($settings);
|
||||||
|
|
||||||
|
if ($this->shouldExcludeLevel($record->level, $excludedLevels)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
$endpoint = (string) ($settings['endpoint'] ?? '');
|
$endpoint = (string) ($settings['endpoint'] ?? '');
|
||||||
$token = (string) ($settings['token'] ?? '');
|
$token = (string) ($settings['token'] ?? '');
|
||||||
|
|
||||||
@@ -141,4 +149,47 @@ class LogForwarder
|
|||||||
|
|
||||||
return (string) ($record->context['trace'] ?? '');
|
return (string) ($record->context['trace'] ?? '');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param array<string, mixed> $settings
|
||||||
|
* @return Level[]
|
||||||
|
*/
|
||||||
|
private function resolveExcludedLevels(array $settings): array
|
||||||
|
{
|
||||||
|
$levels = $settings['excluded_levels'] ?? [];
|
||||||
|
|
||||||
|
if (! is_array($levels)) {
|
||||||
|
$levels = [$levels];
|
||||||
|
}
|
||||||
|
|
||||||
|
$resolved = [];
|
||||||
|
|
||||||
|
foreach ($levels as $level) {
|
||||||
|
if (! is_string($level) || $level === '') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
$resolved[] = Level::fromName($level);
|
||||||
|
} catch (ValueError) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return $resolved;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Level[] $excludedLevels
|
||||||
|
*/
|
||||||
|
private function shouldExcludeLevel(Level $recordLevel, array $excludedLevels): bool
|
||||||
|
{
|
||||||
|
foreach ($excludedLevels as $level) {
|
||||||
|
if ($recordLevel === $level) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user