3 Commits

Author SHA1 Message Date
7ff537a005 Exclude Log Levels 2025-10-23 12:26:28 +02:00
0b8e72fb7b Exclude Log Levels 2025-10-23 12:21:31 +02:00
5c1e81d86a Updating README.md 2025-09-18 13:12:59 +02:00
3 changed files with 66 additions and 2 deletions

View File

@@ -10,10 +10,18 @@ This Laravel 12 package captures every log record emitted by your application in
## Installation
Assuming the package is required locally via Composer path repository (as in this project):
Adding the private Git Repository to your composer.json:
```bash
composer require herrleinit/loghandler:@dev
"require": {
"herrleinit/loghandler": "^1.0"
},
"repositories": [
{
"type": "vcs",
"url": "https://git.hacker.schule/herrleinIT/loghandler.git"
}
]
```
Laravel will auto-discover the `LogHandlerServiceProvider`.
@@ -34,6 +42,7 @@ Laravel will auto-discover the `LogHandlerServiceProvider`.
LOGHANDLER_TIMEOUT_MS=3000
LOGHANDLER_RETRY_TIMES=0
LOGHANDLER_INCLUDE_TRACE=false
LOGHANDLER_EXCLUDE_LEVELS=debug,info
```
3. **Logging stack** ensure the `loghandler` channel participates in your default stack:

View File

@@ -8,4 +8,8 @@ return [
'timeout_ms' => env('LOGHANDLER_TIMEOUT_MS', 3000),
'retry_times' => env('LOGHANDLER_RETRY_TIMES', 0),
'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')))
))),
];

View File

@@ -6,8 +6,10 @@ use ErrorException;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
use Illuminate\Contracts\Config\Repository;
use Monolog\Level;
use Monolog\LogRecord;
use Throwable;
use ValueError;
class LogForwarder
{
@@ -24,6 +26,12 @@ class LogForwarder
return;
}
$excludedLevels = $this->resolveExcludedLevels($settings);
if ($this->shouldExcludeLevel($record->level, $excludedLevels)) {
return;
}
$endpoint = (string) ($settings['endpoint'] ?? '');
$token = (string) ($settings['token'] ?? '');
@@ -141,4 +149,47 @@ class LogForwarder
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;
}
}